Class: Gimli::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/gimli/converter.rb

Overview

The class that communicates with PDFKit

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Converter

Initialize the converter with a File

Parameters:



16
17
18
# File 'lib/gimli/converter.rb', line 16

def initialize(file)
  @file = file
end

Instance Method Details

#convert!Object

Convert the file and save it as a PDF file



21
22
23
24
25
26
27
28
# File 'lib/gimli/converter.rb', line 21

def convert!
  markup = Markup.new @file
  html = markup.render

  kit = pdf_kit(html)

  kit.to_file(output_file)
end

#load_stylesheets(kit) ⇒ Object

Load the stylesheets to pdfkit loads the default and the user selected if any

Parameters:

  • kit (PDFKit)


43
44
45
46
47
48
49
50
51
# File 'lib/gimli/converter.rb', line 43

def load_stylesheets(kit)
  # Load standard stylesheet
  style = ::File.expand_path("../../../config/style.css", __FILE__)
  kit.stylesheets << style

  stylesheet

  kit.stylesheets << stylesheet if ::File.exists?(stylesheet)
end

#output_dirString

Returns the directory where to save the output. Defaults to ./

Returns:

  • (String)


63
64
65
66
67
68
# File 'lib/gimli/converter.rb', line 63

def output_dir
  output_dir = Dir.getwd
  output_dir = ARGV.flags.outputdir if ARGV.flags.outputdir?
  FileUtils.mkdir_p(output_dir) unless ::File.directory?(output_dir)
  output_dir
end

#output_fileString

Generate the name of the output file

Returns:

  • (String)


72
73
74
# File 'lib/gimli/converter.rb', line 72

def output_file
  ::File.join(output_dir, "#{@file.name}.pdf")
end

#pdf_kit(html) ⇒ PDFKit

Load the pdfkit with html

Parameters:

  • html (String)

Returns:

  • (PDFKit)


33
34
35
36
37
38
39
# File 'lib/gimli/converter.rb', line 33

def pdf_kit(html)
  kit = PDFKit.new(html)

  load_stylesheets kit

  kit
end

#stylesheetString

Returns the selected stylesheet. Defaults to ./gimli.css

Returns:

  • (String)


55
56
57
58
59
# File 'lib/gimli/converter.rb', line 55

def stylesheet
  stylesheet = 'gimli.css'
  stylesheet = ARGV.flags.stylesheet if ARGV.flags.stylesheet?
  stylesheet
end