Class: Gimli::Converter
- Inherits:
-
Object
- Object
- Gimli::Converter
- Defined in:
- lib/gimli/converter.rb
Overview
The class that communicates with PDFKit
Instance Method Summary collapse
-
#convert! ⇒ Object
Convert the file and save it as a PDF file.
-
#initialize(file) ⇒ Converter
constructor
Initialize the converter with a File.
-
#load_stylesheets(kit) ⇒ Object
Load the stylesheets to pdfkit loads the default and the user selected if any.
-
#output_dir ⇒ String
Returns the directory where to save the output.
-
#output_file ⇒ String
Generate the name of the output file.
-
#pdf_kit(html) ⇒ PDFKit
Load the pdfkit with html.
-
#stylesheet ⇒ String
Returns the selected stylesheet.
Constructor Details
#initialize(file) ⇒ Converter
Initialize the converter with a File
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
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.("../../../config/style.css", __FILE__) kit.stylesheets << style stylesheet kit.stylesheets << stylesheet if ::File.exists?(stylesheet) end |
#output_dir ⇒ String
Returns the directory where to save the output. Defaults to ./
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_file ⇒ String
Generate the name of the output file
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
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 |
#stylesheet ⇒ String
Returns the selected stylesheet. Defaults to ./gimli.css
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 |