Class: Palapala::Pdf
- Inherits:
-
Object
- Object
- Palapala::Pdf
- Defined in:
- lib/palapala/pdf.rb
Overview
Page class to generate PDF from HTML content using Chrome in headless mode in a thread-safe way
Instance Method Summary collapse
-
#binary_data ⇒ String
Render the PDF content to a binary string.
- #hf_template(from:) ⇒ Object
-
#initialize(content, footer_template: nil, footer: nil, generate_tagged_pdf: nil, header_template: nil, header: nil, landscape: nil, margin_bottom: nil, margin_left: nil, margin_right: nil, margin_top: nil, page_ranges: nil, paper_height: nil, paper_width: nil, prefer_css_page_size: nil, print_background: nil, scale: nil, watermark: nil) ⇒ Pdf
constructor
Initialize the PDF object with the HTML content and optional parameters.
-
#save(path) ⇒ void
Save the PDF content to a file.
- #watermark(watermark, angle: "-15deg", color: "rgba(25,25,25,0.25)") ⇒ Object
Constructor Details
#initialize(content, footer_template: nil, footer: nil, generate_tagged_pdf: nil, header_template: nil, header: nil, landscape: nil, margin_bottom: nil, margin_left: nil, margin_right: nil, margin_top: nil, page_ranges: nil, paper_height: nil, paper_width: nil, prefer_css_page_size: nil, print_background: nil, scale: nil, watermark: nil) ⇒ Pdf
Initialize the PDF object with the HTML content and optional parameters.
The options are passed to the renderer when generating the PDF. The options are the snakified version of the options from the Chrome DevTools Protocol to respect the Ruby conventions. (see chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF)
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/palapala/pdf.rb', line 30 def initialize(content, footer_template: nil, footer: nil, generate_tagged_pdf: nil, header_template: nil, header: nil, landscape: nil, margin_bottom: nil, margin_left: nil, margin_right: nil, margin_top: nil, page_ranges: nil, paper_height: nil, paper_width: nil, prefer_css_page_size: nil, print_background: nil, scale: nil, watermark: nil) @content = content || raise(ArgumentError, "Content is required and can't be nil") @opts = {} raise(ArgumentError, "Either footer or footer_template is expected") if !.nil? && !.nil? raise(ArgumentError, "Either header or header_template is expected") if !header_template.nil? && !header.nil? @opts[:headerTemplate] = header_template || hf_template(from: header) || Palapala.defaults[:header_template] @opts[:footerTemplate] = || hf_template(from: ) || Palapala.defaults[:footer_template] @opts[:pageRanges] = page_ranges || Palapala.defaults[:page_ranges] @opts[:generateTaggedPDF] = generate_tagged_pdf || Palapala.defaults[:generate_tagged_pdf] @opts[:paperWidth] = paper_width || Palapala.defaults[:paper_width] @opts[:paperHeight] = paper_height || Palapala.defaults[:paper_height] @opts[:landscape] = landscape || Palapala.defaults[:landscape] @opts[:marginTop] = margin_top || Palapala.defaults[:margin_top] @opts[:marginLeft] = margin_left || Palapala.defaults[:margin_left] @opts[:marginBottom] = margin_bottom || Palapala.defaults[:margin_bottom] @opts[:marginRight] = margin_right || Palapala.defaults[:margin_right] @opts[:preferCSSPageSize] = prefer_css_page_size || Palapala.defaults[:prefer_css_page_size] @opts[:printBackground] = print_background || Palapala.defaults[:print_background] @opts[:scale] = scale || Palapala.defaults[:scale] @opts[:headerTemplate] = (@opts[:headerTemplate].to_s + watermark(watermark)) if watermark @opts[:displayHeaderFooter] = (@opts[:headerTemplate] || @opts[:footerTemplate]) ? true : false @opts[:encoding] = :binary @opts.compact! end |
Instance Method Details
#binary_data ⇒ String
Render the PDF content to a binary string.
The params from the initializer are converted to the expected casing and merged with the options passed to this method. The options passed here are passed unchanged to the renderer and get priority over the options from the initializer. Chrome DevTools Protocol expects the options to be camelCase, see chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF.
111 112 113 114 115 116 117 118 |
# File 'lib/palapala/pdf.rb', line 111 def binary_data puts "Rendering PDF with params: #{@opts}" if Palapala.debug Renderer.html_to_pdf(@content, params: @opts) rescue StandardError => e puts "Error rendering PDF: #{e.}" Renderer.reset raise end |
#hf_template(from:) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/palapala/pdf.rb', line 89 def hf_template(from:) return if from.nil? style = <<~HTML.freeze <style> #header, #footer { font-size: 10pt; display: flex; justify-content: center; } </style> HTML style + from end |
#save(path) ⇒ void
This method returns an undefined value.
Save the PDF content to a file
124 125 126 |
# File 'lib/palapala/pdf.rb', line 124 def save(path) File.binwrite(path, binary_data) end |
#watermark(watermark, angle: "-15deg", color: "rgba(25,25,25,0.25)") ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/palapala/pdf.rb', line 72 def watermark(watermark, angle: "-15deg", color: "rgba(25,25,25,0.25)") <<~HTML <style> .palapala_pdf_watermark { position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%) rotate(#{angle}); font-size: 8em; color: #{color}; z-index: 9999; } </style> <span class="palapala_pdf_watermark">#{watermark}</span> HTML end |