Class: WickedPdf

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

Constant Summary collapse

@@config =
{}

Instance Method Summary collapse

Constructor Details

#initialize(wkhtmltopdf_binary_path = nil) ⇒ WickedPdf

Returns a new instance of WickedPdf.



15
16
17
18
19
20
21
22
# File 'lib/wicked_pdf.rb', line 15

def initialize(wkhtmltopdf_binary_path = nil)
  @exe_path = wkhtmltopdf_binary_path
  @exe_path ||= WickedPdf.config[:exe_path] unless WickedPdf.config.empty?
  @exe_path ||= `which wkhtmltopdf`.chomp
  raise "Location of wkhtmltopdf unknown" if @exe_path.empty?
  raise "Bad wkhtmltopdf's path" unless File.exists?(@exe_path)
  raise "Wkhtmltopdf is not executable" unless File.executable?(@exe_path)
end

Instance Method Details

#pdf_from_string(string, options = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/wicked_pdf.rb', line 24

def pdf_from_string(string, options={})
  command_for_stdin_stdout = "#{@exe_path} #{parse_options(options)} -q - - " # -q for no errors on stdout
  p "*"*15 + command_for_stdin_stdout + "*"*15 unless defined?(Rails) and Rails.env != 'development'
  pdf, err = begin
    Open3.popen3(command_for_stdin_stdout) do |stdin, stdout, stderr|
      stdin.write(string)
      stdin.close
      [stdout.read, stderr.read]
    end
  rescue Exception => e
    raise "Failed to execute #{@exe_path}: #{e}"
  end
  raise "PDF could not be generated!\n#{err}" if pdf and pdf.length == 0
  pdf
end