Skip to content

Add renderer#64

Merged
excid3 merged 5 commits intomainfrom
add-renderer
Jul 18, 2025
Merged

Add renderer#64
excid3 merged 5 commits intomainfrom
add-renderer

Conversation

@excid3
Copy link
Owner

@excid3 excid3 commented Jul 17, 2025

I never liked injecting the render_pdf and render_screenshot methods into ActionController. We even had a config option to disable it. This got me thinking, what if we could just add a renderer instead?

This turned out to be a much cleaner approach and also handles the send_data call for users. Plus, it won't conflict with any user code.

Renderer options are passed directly to FerrumPDF and the other options are used for render_to_string and send_data.

With the new renderer:

format.pdf { render ferrum_pdf: {}, disposition: :inline, filename: "example.pdf" }
format.png { render ferrum_screenshot: {}, disposition: :inline, filename: "example.png" }

More advanced example using Ferrum options, template and layout for render_to_string and options for send_data.

format.pdf { render ferrum_pdf: { display_header_footer: true }, template: "pdf", layout: "pdf", disposition: :inline, filename: "example.pdf" }

This also cleans up the AssetHelper so the classes aren't included in the main application.

Since this removes render_pdf and render_screenshot methods from controllers, we'll want to cut this as a new major release.

@excid3 excid3 self-assigned this Jul 17, 2025
@excid3 excid3 added the enhancement New feature or request label Jul 17, 2025
@excid3
Copy link
Owner Author

excid3 commented Jul 17, 2025

@brandoncc this is what I mentioned earlier in an email.

header_template: FerrumPdf::DEFAULT_HEADER_TEMPLATE,
footer_template: FerrumPdf::DEFAULT_FOOTER_TEMPLATE
}
) do |browser, page|
Copy link
Owner Author

@excid3 excid3 Jul 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block is still possible, but only with FerrumPdf.render_pdf() calls directly.

Copy link
Contributor

@brandoncc brandoncc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found some typos, but mostly good. I remember using a custom renderer many years ago when I hadn't been using Rails very long. It was completely magical, and it was awesome to be able to break that magic down and understand it now!

Comment on lines +29 to +36
class PdfsController < ApplicationController
def show
respond_to do |format|
format.pdf { render ferrum_pdf: {}, disposition: :inline, filename: "example.pdf" }
format.png { render ferrum_screenshot: {}, disposition: :inline, filename: "example.png" }
end
end
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So good!

Comment on lines +13 to +17
send_data_options = options.extract!(:disposition, :filename, :status)
url = pdf_options.delete(:url)
html = render_to_string(**options.with_defaults(formats: [ :html ])) if url.blank?
pdf = FerrumPdf.render_pdf(html: html, base_url: request.base_url, url: url, pdf_options: pdf_options)
send_data(pdf, **send_data_options.with_defaults(type: :pdf))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It took me some time to figure this out, but it really is pretty simple. Nice work.

excid3 and others added 2 commits July 18, 2025 07:28
Co-authored-by: Brandon Conway <brandoncc@gmail.com>
Co-authored-by: Brandon Conway <brandoncc@gmail.com>
@excid3 excid3 merged commit ade9c94 into main Jul 18, 2025
2 checks passed
@excid3 excid3 deleted the add-renderer branch July 18, 2025 12:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants