Class: WickedPdf::Document

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

Instance Method Summary collapse

Constructor Details

#initialize(command = Command.new) ⇒ Document

Returns a new instance of Document.



3
4
5
# File 'lib/wicked_pdf/document.rb', line 3

def initialize(command = Command.new)
  @command = command
end

Instance Method Details

#pdf_from_html_file(filepath, options = {}) ⇒ Object



7
8
9
# File 'lib/wicked_pdf/document.rb', line 7

def pdf_from_html_file(filepath, options = {})
  pdf_from_url("file:///#{filepath}", options)
end

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



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/wicked_pdf/document.rb', line 11

def pdf_from_string(string, options = {})
  options = options.dup
  options.merge!(WickedPdf.config.default_options) { |_key, option, _config| option }
  string_file = WickedPdf::Tempfile.new('wicked_pdf.html', options[:temp_path])
  string_file.write_in_chunks(string)

  pdf_from_html_file(string_file.path, options)
rescue Errno::EINVAL => e
  Rails.logger.error '[wicked_pdf] The HTML file is too large! Try reducing the size or using the return_file option instead.'
  raise e
ensure
  string_file.close! if string_file
end

#pdf_from_url(url, options = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/wicked_pdf/document.rb', line 58

def pdf_from_url(url, options = {})
  # merge in global config options
  options.merge!(WickedPdf.config.default_options) { |_key, option, _config| option }
  generated_pdf_file = WickedPdf::Tempfile.new('wicked_pdf_generated_file.pdf', options[:temp_path])
  return_file = options.delete(:return_file)

  err = @command.execute(options, url, generated_pdf_file.path.to_s)

  return generated_pdf_file if return_file

  pdf = generated_pdf_file.read_in_chunks

  raise GenerationError, "PDF could not be generated!\n Command Error: #{err}" if pdf && pdf.rstrip.empty?

  pdf
rescue Errno::EINVAL => e
  Rails.logger.error '[wicked_pdf] The PDF file is too large! Try reducing the size or using the return_file option instead.'
  raise e
ensure
  generated_pdf_file.close! if generated_pdf_file && !return_file
end

#pdfs_from_strings(items, concurrency: 1) ⇒ Object

Renders many HTML strings via Command#execute_batch (shared wkhtmltopdf processes instead of one boot per document). Items are Hashes of { html: String, options: Hash }; each item's options apply to that conversion only. Returns the PDF strings in item order.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/wicked_pdf/document.rb', line 29

def pdfs_from_strings(items, concurrency: 1)
  tempfiles = []

  jobs = items.map do |item|
    options = (item[:options] || {}).dup
    options.merge!(WickedPdf.config.default_options) { |_key, option, _config| option }

    input = WickedPdf::Tempfile.new('wicked_pdf_batch.html', options[:temp_path])
    tempfiles << input
    input.write_in_chunks(item.fetch(:html))

    output = WickedPdf::Tempfile.new('wicked_pdf_batch.pdf', options[:temp_path])
    tempfiles << output

    { options: options, url: "file:///#{input.path}", output_path: output.path, output_file: output }
  end

  @command.execute_batch(jobs, concurrency: concurrency)

  jobs.map do |job|
    pdf = job[:output_file].read_in_chunks
    raise GenerationError, "PDF could not be generated!\n Output: #{job[:output_path]}" if pdf.rstrip.empty?

    pdf
  end
ensure
  tempfiles.each(&:close!)
end