5 different ways to Generate PDF Charts in Rails 6

5 different ways to Generate PDF Charts in Rails 6

I was doing a task related to data representations and it needed lots of charts rendering stuff and converting them to PDF. Since the application is running on multiple devices, Ctrl + P option didn’t helped much. So, I started exploring each possible way out there. I am listing below, 5 best ways to generate PDF charts in rails which I have tried personally, with pros and cons.

  • Wicked PDF (The popular one)
  • PDFKit
  • PhantomPDF
  • Grover
  • Dhalang

I am quite sure you haven’t even heard of last 2 at the time of writing this post. Let’s start:

Wicked PDF:

A popular gem used highly for rendering of traditional HTML elements to PDF but fails miserably while dealing with charts, graph (i.e. canvas and SVG). Graphs distortion is very common behavior, sometimes not even render them. Libraries like chartJS, d3, highcharts, c3 etc have their own patches but use them if you really want to bump your head in dark because they are version specific and might break your application on upgrade. Moreover it doesn’t supports latest Bootstrap 4 because of autoprefixer as mentioned here at Bootstrap 4 repo . You can check those library specific patch here.

PDFKIt:

Not a fan of it but again a popular gem for server side PDF rendering. You need to write separate HTML and CSS layout and don’t forget to provide absolute path for assets. Unluckily the underlying technology is same as WickedPDF so you will face similar issues while rendering charts or graphs as of Wicked PDF . The CSS distortion is quite common.

WickedPDf and PDFKit both are ruby wrapper of Wkhtmltopdf , which is a C++ executable used for PDF / Reports generation.  The WK in wkhtmltopdf stands for webkit (Qt) browser engine that is used to render HTML and java-script Chrome uses this same engine. Here is the catch, Qt WebKit, which is an older Webkit engine & is no longer supported by Browserslist 3.0 hence not fully supporting new HTML5 elements. So clearly not a good option for rendering charts. These are best suited for simple documents where you can afford to have some inconsistencies or failures. Use these if you do not want precise control over positioning.

Phantompdf:

Unmaintained gem that use quite old and deprecated headless browser called PhantomJS. But its not being maintained, so there is no surety of future dependencies. You need to write your own HTML CSS in this case too.

Grover:

Something different this time. Now take a hold when i say snapshot. Means YOU DON’T WRITE YOUR separate CSS and HTML layout. It uses browser’s functionality to capture view port and convert them to PDF / PNG/ JPEG and is fast than Wkhtmltopdf. The only problem I faced is runtime error it gave because of some chartJS configuration.

Dhalang:

Bet you never heard. Dhalang is a ruby gem that works just awesome. Similar to Grover it takes snapshot of your view port and convert them to PDF/ PNG / JPEG . Since there is no UI creation, your original HTML – CSS layout remains intact, but you might face some blurry charts. (Sample Image below)

To resolve that, you must specify configuration for view-port. Another drawback is that, it doesn’t have proper documentation, so for any fix, you need to peep into source code for survival. If you want to see the live implementation follow my other article Rails 6 + ChartsJS PDF generation with Dhalang.

Grover and Dhalang are Ruby wrappers of Google Puppeteer , a Node library which has quite active release cycle. Node applications use this for automation, crawling, taking screenshots etc.

CONCLUSION: If you want quick crisp PDF charts in rails using JS library (ie. chartJS, highcharts etc) without bothering about browser support, try Dhalang.

Thanks.

One Reply to “5 different ways to Generate PDF Charts in Rails 6”

Leave a Reply