Generating pdf is a common requirement in web applications, mostly to download reports. Users would like to download their data as pdf or excel and there are only few libraries available in Ruby to achieve this, but it depends on the requirement. There are some straight forward ways to download the data in PDF format, say directly converting the html to pdf, which is one of the simplest ways.
Available Solutions in Ruby
The list of libraries or gems that are available in Ruby
1) Wicked PDF
2) PDFKit
3) Prawn PDF
Why PRAWN PDF ?
Here am not going to explain about all 3 gems, but I am going to share my experience with Prawn PDF.
We got a requirement for a project to generate pdf for a report module, Initially without too much investigation we directly went with wicked PDF, which got a simple and straight forward solution for our basic need.
The coding stuff was done in the view part and converted the html to pdf. But when the requirement became more specific and the table started growing, html to pdf was not of help anymore, We din’t have much control over the document.
We looked for solution and found out few googling, like page breaks using css and few other stuff to achieve this, but it did not help us a lot.
Then we tried Prawn PDF, which I will say is really really cool and awesome 🙂, when it comes to more specific requirements(fixed header footer, repeat table header in all the pages, separate footer in the last screen), We will have more control on the pages, but we need to do some coding to achieve this. Finally we got a reliable and clean output.
PrawnPdf is one of the fastest PDf generators available for Ruby, refer ruby-toolbox
Code Samples
Prawn PDF has a very nice manual explaining each and every api methods.
Here i have shared the code to repeat the header in all pages, We would be able to define colors, add html content, set width, height, alignment, text color and so on.
Code to generate table, where we would be able to define, header, width of each and every cell in the table, style and color of the row.
PRAWN Pdf has lots of api methods to draw shapes like, circle, rectangle, move from one point to other point and measurements based on [:mm, :cm, :dm, :m, :in, :yd, :ft].This is possible since PrawnPDF works based on gird layout, which helps us to be more precise.
There are some limitations in PRWAN PDF, We cannot draw everything we wanted in PRAWN, say pie chart, which involves lots of code to be written, at this point it would be good to go with HTML to PDF convertor.
As I already stated, picking the gem is based on preference and project requirements. If you have a view and want to just convert to pdf, then gems like PDFkit will help, HTML to PDF can be simpler.
Conclusion
So when you want more control on the PDF document and if the PDF has multiple pages and involves more complexities, then PRAWN PDF would be the right gem, but when it comes to a single pager that involves lots of images/charts then PDFkit will be the right choice.