Total Blog Views: 177
Blog Status: publish
Created By: swaz_ahmed Created at: 08-02-2024
Tags: ror rails 7 wicked pdf gem template issue grover gem wicked to grover gem haml template haml.html
Generating PDFs in Rails applications is a common requirement, whether it's for creating invoices, reports, or downloadable content. wicked_pdf has been a popular choice for this task, but with the release of Rails 7, some developers have encountered compatibility issues. As a result, many are switching to the grover gem, which offers a modern and efficient solution for PDF generation.
Why Switch to Grover?
While wicked_pdf has served us well over the years, it can struggle with the latest versions of Rails due to its dependencies and the way it handles PDF generation. In contrast, grover leverages the power of modern web technologies like Headless Chrome, providing better support for CSS3, JavaScript, and overall rendering quality.
Modern Web Standards: Better handling of CSS, JavaScript, and web fonts.
Performance: Faster and more efficient PDF generation.
Ease of Use: Simplified configuration and usage.
Setting Up Grover
To get started with grover in your Rails 7 application, add it to your Gemfile:
gem 'grover'
npm install puppeteer
#Grover.new accepts a URL or inline HTML and optional parameters for Puppeter grover = Grover.new('https://google.com', format: 'A4') # Get an inline PDF pdf = grover.to_pdf # Get a screenshot png = grover.to_png jpeg = grover.to_jpeg # Get the HTML content (including DOCTYPE) html = grover.to_html # Options can be provided through meta tags Grover.new('<html><head><meta name="grover-page_ranges" content="1-3"') Grover.new('<html><head><meta name="grover-margin-top" content="10px"')
It's easy to render a normal Rails view template as a PDF, using Rails' render_to_string:
html = MyController.new.render_to_string({ template: 'controller/view', layout: 'my_layout', locals: { :@instance_var => ... } }) pdf = Grover.new(html, **grover_options).to_pdf
# config/initializers/grover.rb config.options = { format: 'A4', margin: { top: '5px', bottom: '10cm' }, user_agent: 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Geck o/20100101 Firefox/47.0', viewport: { width: 640, height: 480 }, prefer_css_page_size: true, emulate_media: 'screen', bypass_csp: true, media_features: [{ name: 'prefers-color-scheme', value: 'dark' }], timezone: 'Australia/Sydney', vision_deficiency: 'deuteranopia', extra_http_headers: { 'Accept-Language': 'en-US' }, geolocation: { latitude: 59.95, longitude: 30.31667 }, focus: '#some-element', hover: '#another-element', cache: false, timeout: 0, # Timeout in ms. A value of `0` means 'no timeout' request_timeout: 1000, # Timeout when fetching the content (overloads the `timeout` option) convert_timeout: 2000, # Timeout when converting the content (overloads the `timeout` option, only applies to PDF conversion) launch_args: ['--font-render-hinting=medium'], wait_until: 'domcontentloaded' } end
Generating PDFs with Grover
To generate a PDF from a view or HTML template, you can use grover in a controller action. Here's an example:
class ReportsController < ApplicationController def show @report = Report.find(params[:id]) html = render_to_string(template: 'reports/show', layout: 'pdf') pdf = Grover.new(html).to_pdf send_data pdf, filename: "report-#{@report.id}.pdf", type: 'application/pdf', disposition: 'inline' end end
we have the “Get things executed” lifestyle at our place of work. There are not any excuses, no if’s or however’s in our dictionary. committed to navigating the ship of creativity to create cell answers, we resolve the real-lifestyles troubles of our clients and their clients. Our passion for work has won us many awards, year after 12 months.
© Copyright Shadbox. All Rights Reserved
Rate Blog :
Share on :
Do you have any blog suggestion? please click on the link