Class: WickedPdf::Document
- Inherits:
-
Object
- Object
- WickedPdf::Document
- Defined in:
- lib/wicked_pdf/document.rb
Instance Method Summary collapse
-
#initialize(command = Command.new) ⇒ Document
constructor
A new instance of Document.
- #pdf_from_html_file(filepath, options = {}) ⇒ Object
- #pdf_from_string(string, options = {}) ⇒ Object
- #pdf_from_url(url, options = {}) ⇒ Object
-
#pdfs_from_strings(items, concurrency: 1) ⇒ Object
Renders many HTML strings via Command#execute_batch (shared wkhtmltopdf processes instead of one boot per document).
Constructor Details
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, = {}) pdf_from_url("file:///#{filepath}", ) 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, = {}) = .dup .merge!(WickedPdf.config.) { |_key, option, _config| option } string_file = WickedPdf::Tempfile.new('wicked_pdf.html', [:temp_path]) string_file.write_in_chunks(string) pdf_from_html_file(string_file.path, ) 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, = {}) # merge in global config options .merge!(WickedPdf.config.) { |_key, option, _config| option } generated_pdf_file = WickedPdf::Tempfile.new('wicked_pdf_generated_file.pdf', [:temp_path]) return_file = .delete(:return_file) err = @command.execute(, 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| = (item[:options] || {}).dup .merge!(WickedPdf.config.) { |_key, option, _config| option } input = WickedPdf::Tempfile.new('wicked_pdf_batch.html', [:temp_path]) tempfiles << input input.write_in_chunks(item.fetch(:html)) output = WickedPdf::Tempfile.new('wicked_pdf_batch.pdf', [:temp_path]) tempfiles << output { 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 |