I finally found some time to write my first blog post for my awesome company! Here we go…
For a recent project I decided to use PDFKit to generate PDF files in Ruby on Rails. I’m used to generating these with Prawn, but as we all know it can be very hard to get it styled correctly.
First things first, we have to install the gem and then the wkhtmltopdf binary.
Installing the gem is easy:
gem install pdfkit
Hey, installing the binary is as easy:
sudo pdfkit --install-wkhtmltopdf
Now we can use PDFKit on our system.
To generate a pdf from a HTML web page just create the kit instance:
And then convert it to a pdf file:
You can also pass an URL of a file to the PDFKit initializer method:
Some Rails magic
But here is some real magic. You can just attach the pdf format to your controller actions. When you go to http://test.be/people/jelle.pdf, the PDF will be downloaded (or your browser will open it in a window).
Just include the following lines of code in your environment.rb (for Rails 2.x) or application.rb (for Rails 3.x):
What happens now is that your show.html.erb file is rendered as usual but it is converted to the PDF format afterwards. So the PDf file gets the same style as the HTML page (with CSS styling included).
I used this for a project, and it works great!
Changing the filename
Now another handy way to change the filename for the PDF file. When you go to http://test.be/people/1.pdf, the downloaded filename will be 1.pdf. But what if I want the filename to be jelle-vandebeeck.pdf? Well, just include this line of code somewhere in your action:
For more information visit the Github page.
Sign up for our newsletter
Who’s got the mic?
Architecture & Design
Koen Van Der Auwera
Bob Van Landuyt
Nathan de Witte