#Pdfkit rails how to#
You can read up on how to do that in their comprehensive Readme. This ensures the stylesheet_link_tag and font-url helpers output the absolute path including your domain name rather than just the relative path.ĭepending on the complexity of your requirements, you might want to set up Grover as a middleware. The easiest way to enable this is to set config.asset_host in your app configuration. Since Grover uses Chromium which runs external of your Rails app, you need to reference all your assets with absolute paths instead of relative paths. This will also allow you to reuse CSS from your app rather than having to write specific CSS just for your PDF exports.
So your PDF will look exactly how your page looks in Google Chrome's print preview. It uses Puppeteer and Chromium to "print" an HTML page into a PDF. They're good libraries but the underlying wkhtmltopdf doesn't support modern CSS features such as custom properties or grid so you might find yourself unable to use any of the existing CSS in your app to style your PDF export. I'd highly advise against using both those gems. They both use a command line utility called wkhtmltopdf under the hood which uses WebKit to render a PDF from HTML. If you are on Windows, want to point PDFKit to a different binary, or are having trouble with getting PDFKit to find your binary, please manually configure the wkhtmltopdf location. However with the right setup, it's possible to take the pain out of it!Ī couple of popular gems to convert HTML to PDF in Rails are PDFKit and WickedPDF. PDFKit will try to intelligently guess at the location of wkhtmltopdf by running the command which wkhtmltopdf. Exporting to PDF from HTML can be a bit of a can of worms, especially with CSS not quite working the way it does in a web browser.