-
-
Notifications
You must be signed in to change notification settings - Fork 29
Add renderer #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add renderer #64
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,13 +2,29 @@ module FerrumPdf | |
| class Railtie < ::Rails::Railtie | ||
| initializer "ferrum_pdf.assets_helper" do | ||
| ActiveSupport.on_load(:action_view) do | ||
| include FerrumPdf::AssetsHelper if FerrumPdf.include_assets_helper_module | ||
| include FerrumPdf::AssetsHelper | ||
| end | ||
| end | ||
|
|
||
| initializer "ferrum_pdf.controller" do | ||
| ActiveSupport.on_load(:action_controller) do | ||
| include FerrumPdf::Controller if FerrumPdf.include_controller_module | ||
| # render ferrum_pdf: { pdf options }, template: "whatever", disposition: :inline, filename: "example.pdf" | ||
| ActionController.add_renderer :ferrum_pdf do |pdf_options, options| | ||
| 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)) | ||
|
Comment on lines
+13
to
+17
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| end | ||
|
|
||
| # render ferrum_pdf: { pdf options }, template: "whatever", disposition: :inline, filename: "example.png" | ||
excid3 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ActionController.add_renderer :ferrum_screenshot do |screenshot_options, options| | ||
| send_data_options = options.extract!(:disposition, :filename, :status) | ||
| url = screenshot_options.delete(:url) | ||
| html = render_to_string(**options.with_defaults(formats: [ :html ])) if url.blank? | ||
| screenshot = FerrumPdf.render_screenshot(url: url, html: html, base_url: request.base_url, screenshot_options: screenshot_options) | ||
| send_data(screenshot, **send_data_options.with_defaults(type: screenshot_options.fetch(:format, :png))) | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,34 +6,22 @@ def show | |
| respond_to do |format| | ||
| format.html | ||
| format.pdf { | ||
| pdf = render_pdf( | ||
| layout: "pdf", | ||
| pdf_options: { | ||
| render ferrum_pdf: { | ||
| display_header_footer: true, | ||
| header_template: FerrumPdf::DEFAULT_HEADER_TEMPLATE, | ||
| footer_template: FerrumPdf::DEFAULT_FOOTER_TEMPLATE | ||
| } | ||
| ) do |browser, page| | ||
|
Owner
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block is still possible, but only with |
||
| Rails.logger.debug "FerrumPdf Chrome PID: #{browser.process.pid}" | ||
| end | ||
| send_data pdf, disposition: :inline, filename: "example.pdf" | ||
| }, | ||
| disposition: :inline, | ||
| filename: "example.pdf" | ||
| } | ||
| format.png { send_data render_screenshot, disposition: :inline, filename: "example.png" } | ||
| format.png { render ferrum_screenshot: {}, disposition: :inline, filename: "example.png" } | ||
| end | ||
| end | ||
|
|
||
| def url | ||
| respond_to do |format| | ||
| format.pdf { | ||
| pdf = FerrumPdf.render_pdf(url: params[:url]) do |browser, page| | ||
| Rails.logger.debug "FerrumPdf Chrome PID: #{browser.process.pid}" | ||
| end | ||
| send_data pdf, disposition: :inline, filename: "example.pdf" | ||
| } | ||
| format.png { | ||
| screenshot = FerrumPdf.render_screenshot(url: params[:url], screenshot_options: { full: params[:full] }) | ||
| send_data screenshot, disposition: :inline, filename: "example.png" | ||
| } | ||
| format.pdf { render ferrum_pdf: { url: params[:url] }, disposition: :inline, filename: "example.pdf" } | ||
| format.png { render ferrum_screenshot: { url: params[:url], full: params[:full] }, disposition: :inline, filename: "example.png" } | ||
| end | ||
| end | ||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So good!