Skip to content

Instantly share code, notes, and snippets.

@bopm
Created February 17, 2024 14:00
Show Gist options
  • Save bopm/5581b212d7dcec5bdfaee0345974346d to your computer and use it in GitHub Desktop.
Save bopm/5581b212d7dcec5bdfaee0345974346d 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
@yshmarov
Copy link

love it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment