Class: Bidi2pdfRails::Services::PdfRenderer

Inherits:
Object
  • Object
show all
Defined in:
lib/bidi2pdf_rails/services/pdf_renderer.rb

Overview

PdfRenderer is responsible for rendering a Rails view into a PDF using headless Chrome.

It delegates option parsing and controller-aware defaults to an options handler object, which splits responsibilities across HTML rendering, PDF configuration, and optional URL rendering.

Examples:

Basic usage

options_handler = Bidi2pdfRails::Services::RenderOptionsHandler.new(options, controller)
renderer = Bidi2pdfRails::Services::PdfRenderer.new(options_handler)
renderer.render_pdf

Constant Summary collapse

PDF_OPTIONS =
%i[
  print_options
  asset_host
  wait_for_network_idle
  wait_for_page_loaded
  wait_for_page_check_script
  html
  callbacks
].freeze
%i[
  url
  auth
  headers
  cookies
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options_handler) ⇒ PdfRenderer

Returns a new instance of PdfRenderer.



42
43
44
# File 'lib/bidi2pdf_rails/services/pdf_renderer.rb', line 42

def initialize(options_handler)
  @options_handler = options_handler
end

Instance Attribute Details

#options_handlerObject (readonly)

Returns the value of attribute options_handler.



40
41
42
# File 'lib/bidi2pdf_rails/services/pdf_renderer.rb', line 40

def options_handler
  @options_handler
end

Instance Method Details

#render_pdfObject



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
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/bidi2pdf_rails/services/pdf_renderer.rb', line 46

def render_pdf
  ActiveSupport::Notifications.instrument("handle_printing.bidi2pdf_rails") do
    if options_handler.url.url?
      headers = options_handler.url.headers
      cookies = options_handler.url.cookies
      auth = options_handler.url.auth

      UrlToPdfConverter.new(options_handler.url.url,
                            headers: headers,
                            cookies: cookies,
                            auth: auth,
                            print_options: options_handler.pdf.print_options,
                            wait_for_network_idle: options_handler.pdf.wait_for_network_idle,
                            wait_for_page_loaded: options_handler.pdf.wait_for_page_loaded,
                            wait_for_page_check_script: options_handler.pdf.wait_for_page_check_script,
                            custom_css: options_handler.pdf.custom_css,
                            custom_css_url: options_handler.pdf.custom_css_url,
                            custom_js: options_handler.pdf.custom_js,
                            custom_js_url: options_handler.pdf.custom_js_url,
                            callbacks: options_handler.pdf.callbacks
      ).generate
    else
      HtmlToPdfConverter.new(options_handler.html.html,
                             print_options: options_handler.pdf.print_options,
                             wait_for_network_idle: options_handler.pdf.wait_for_network_idle,
                             wait_for_page_loaded: options_handler.pdf.wait_for_page_loaded,
                             wait_for_page_check_script: options_handler.pdf.wait_for_page_check_script,
                             custom_css: options_handler.pdf.custom_css,
                             custom_css_url: options_handler.pdf.custom_css_url,
                             custom_js: options_handler.pdf.custom_js,
                             custom_js_url: options_handler.pdf.custom_js_url,
                             callbacks: options_handler.pdf.callbacks
      ).generate
    end
  end
end