Class: Gimli::Wkhtmltopdf

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

Overview

The class that communicates with wkhtmltopdf

Instance Method Summary collapse

Constructor Details

#initialize(parameters = nil) ⇒ Wkhtmltopdf

Set up options for wkhtmltopdf

Parameters:

  • parameters (String) (defaults to: nil)


8
9
10
# File 'lib/gimli/wkhtmltopdf.rb', line 8

def initialize(parameters = nil)
  @parameters = parameters
end

Instance Method Details

#binString

Find the wkhtmltopdf binary

Returns:

  • (String)

    the path to the binary



35
36
37
# File 'lib/gimli/wkhtmltopdf.rb', line 35

def bin
  @bin ||= "\"#{(`which wkhtmltopdf`).chomp}\""
end

#command(filename) ⇒ Array

Assemble the command to run

Parameters:

  • filename (String)

    the outputed pdf’s filename

Returns:

  • (Array)

    a list of strings that make out the call to wkhtmltopdf



29
30
31
# File 'lib/gimli/wkhtmltopdf.rb', line 29

def command(filename)
  [bin, @parameters, '-q', '-', "\"#{filename}\""].compact
end

#output_pdf(html, filename) ⇒ Object

Convert the html to pdf and write it to file

Parameters:

  • html (String)

    the html input

  • filename (String)

    the name of the output file



15
16
17
18
19
20
21
22
23
24
# File 'lib/gimli/wkhtmltopdf.rb', line 15

def output_pdf(html, filename)
  args = command(filename)
  invoke = args.join(' ')

  IO.popen(invoke, "wb+") do |pdf|
    pdf.puts(html)
    pdf.close_write
    pdf.gets(nil)
  end
end