As a Ruby on Rails developer, in your carrier you’ve probably faced a problem of generating a pdf file. Most probably, as a open source zealot you browsed the web for some appropriate solution. If you’ve done that recently you might have found Prawn or… a different approach – PDFkit, a Ruby HTML-to-PDF library powered by wkhtmltopdf.
“Which one is for me?” you might ask yourself.
As, the author of Prawn wrote once, HTML to PDF solution for Ruby on Rails app is for you when:
“
- You already need to write HTML+CSS, and are pretty good at it
- You don’t have too much dynamic content
- You don’t need any fine grained control over measurement calculations. In Prawn, you have all the tools necessary to know obscenely obscure things, like the dimensions and boundaries of arbitrary characters.
- You don’t need hack anything about your renderer (or you know how to code Java/ObjC/C/C++ or whatever the underlying html2pdf engine you’re running is)
- You don’t mind interfacing with binary executables
- Either when you load up the browser and hit “Print” it looks good, or you know how to do print in CSS.
- You don’t need super fine grained control over document flow.
”
PDFkit is totally different way to solve the issue of generating pdf files in your Ruby on Rails project and it’s genre was the Prawn’s “super fine grained control over document” approach to generating pdf documents in Ruby on Rails applications. These four lines below sums it up:
doc = PDFKit.new(" <h1>Hello!</h1> ") doc.stylesheets << '/path/to/pdf.css' doc.to_pdf # inline PDF doc.to_file('/path/to/save/pdf')
It’s nice to have two different solutions for Ruby on Rails pdf file generation, so you as the Ruby on Rails developer are free to choose the one that suits you best.
![]()