Skip to content

Instantly share code, notes, and snippets.

@yshmarov
Forked from bopm/ferrum.rb
Created February 20, 2024 20:32
Show Gist options
  • Save yshmarov/65662f96feebb4d7d0eded7f2da86510 to your computer and use it in GitHub Desktop.
Save yshmarov/65662f96feebb4d7d0eded7f2da86510 to your computer and use it in GitHub Desktop.
format.pdf do
html = ApplicationController.new.render_to_string(
template: 'pdfs/test',
formats: [:pdf],
layout: 'layouts/pdf',
assigns: { user: @user },
encoding: 'UTF-8'
)
Ferrum::Browser.new(timeout: 7).tap do |browser|
browser.content = html
begin
browser.evaluate_async(%(addEventListener('load', arguments[0]);), browser.timeout)
rescue StandardError
nil
end
browser.network.wait_for_idle
pdf = browser.pdf(
format: :letter,
encoding: :binary,
margin_left: 0,
margin_right: 0,
margin_top: 0,
margin_bottom: 0,
print_background: true
)
browser&.reset
browser&.quit
send_data(pdf, filename: 'test.pdf', type: 'application/pdf', disposition: 'inline')
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment