Module: Html2Pdf::Rails::Helper

Defined in:
lib/html2pdf/rails/helper.rb

Instance Method Summary collapse

Instance Method Details

#html2pdf_base_tag(host: nil, protocol: nil) ⇒ Object

Resolves the base URL in the same order as Rails’ ‘url_for`, with one gem-specific addition (HTTP_X_ORIGINAL_HOST). See `_html2pdf_default_url_options` for the source of `options`.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/html2pdf/rails/helper.rb', line 9

def html2pdf_base_tag(host: nil, protocol: nil)
  req = _html2pdf_request
  options = _html2pdf_default_url_options

  # HTTP_X_ORIGINAL_HOST overrides options because dev tunneling (Ngrok) sets
  # this header to the externally-visible host that puppeteer must reach,
  # which doesn't match request.host or any configured default.
  host ||= req && req.headers['HTTP_X_ORIGINAL_HOST']
  host ||= options[:host]
  if host.blank?
    raise ArgumentError,
          'html2pdf_base_tag: host is not available. Pass `host:` or configure default_url_options (e.g. config.action_mailer.default_url_options).'
  end

  # options[:protocol] from controller's url_options is "https://" (with trailing
  # "://" — request.protocol convention), while config and mailer values are
  # bare "https". Normalize.
  protocol ||= options[:protocol]&.to_s&.delete_suffix('://')
  protocol ||= 'https'

  tag.base href: "#{protocol}://#{host}"
end