Converting HTML to PDF – Josh Software

Converting HTML to PDF – Josh Software

Rishi Jain 2012-07-25 10:06:55

One of the common requirements we face across many Rails project is to convert HTML pages to PDF i.e. convert reports, tickets or any other page for that matter into the PDF format ‘as is’. What has made our job easier is the number of gems out there but every gem is different from another in some or the other.

The bottom line is that converting html to pdf  is no more rocket science. There are numbers of gems out there to convert html to pdf in rails like PDFKit and Prawn (among the  popular gems). I prefer using PDFKit over Prawn for couple of reasons.

  • PDFKit is very simple to understand and it gets your job done very quickly if you follow all the right steps, which of course I did not 🙂
  • Prawn restricts you to the old table, grids layout.

Installation and Basic setup

Update your Gemfile with gem ‘pdfkit’, ‘0.5.0’ and do bundle install or if your doing it from the command line do gem install pdfkit .  Thats it!

In config/application.rb, include following piece of code


require 'pdfkit'

config.middleware.use PDFKit::Middleware, :print_media_type => true

Since you want the output in pdf format, include this in the config/environment.rb

Mime::Type.register 'application/pdf', :pdf

Create a file config/initializers/pdfkit.rb with these contents

PDFKit.configure do |config|

  if ["development"].include?(Rails.env)
    #only if your are working on 32bit machine
    config.wkhtmltopdf = Rails.root.join('bin', 'wkhtmltopdf-i386').to_s
  else
    #if your site is hosted on heroku or any other hosting server which is 64bit
    config.wkhtmltopdf = Rails.root.join('bin', 'wkhtmltopdf-amd64').to_s
  end

  config.default_options = {
    :encoding=>"UTF-8",
    :page_size=>"A4",
    :margin_top=>"0.25in",
    :margin_right=>"0.1in",
    :margin_bottom=>"0.25in",
    :margin_left=>"0.1in",
    :disable_smart_shrinking=> false
  }
end

Download these 2 files: (I will explain the reason in the next section)
1. wkhtmltopdf-amd64 (from http://code.google.com/p/wkhtmltopdf/downloads/detail?name=wkhtmltopdf-0.9.9-static-amd64.tar.bz2&can=2&q=)
2. wkhtmltopdf-i386 (from http://code.google.com/p/wkhtmltopdf/downloads/detail?name=wkhtmltopdf-0.9.9-static-i386.tar.bz2&can=2&q=)

Put these 2 files in bin folder of the project (Create one, if it’s not there. You can create any other folder too but then remember to change the path in the initializers.)

How does HTML to PDF conversion takes place?

PDFKit uses wkhtmltopdf , a binary file, to convert html to pdf. For implementing it on 32 bit machine, use  “wkhtmltopdf-i386” and if you are working on 64 bit machine, use “wkhtmltopdf-amd64”.

Tip #1: wkhtmltopdf doesnt include javascripts or stylesheets, i.e if you are adding rows to a table using javascript or if you are filling up datatable using javascript, it wont come into your pdf file. It will only convert whatever is in your html file.

Although there is a way of including stylesheets in it:

kit.stylesheets << "#{Rails.root}/app/assets/stylesheets/application.css"

Tip #2: You have to give the whole path of your stylesheet and also the full path of images you mention in your stylesheets.

Tip #3: PDFKit handles the page break issue very well in case of images. It won`t show half image on page and the other half of the image on other. But when it comes to handling rows in a table, it doesnt quite handle it the same way.  But we can always use “page-break-before” property of css.

For converting an HTML page to PDF, we can use this sample code

  html = render_to_string(:action => "your_page.html.haml")
  kit = PDFKit.new(html)
  send_data(kit.to_pdf, :filename => 'report.pdf', :type => 'application/pdf', :disposition => 'inline')

There are number of gems out there to handle the binary file (for running on development, staging or production environment) like wkhtmltopdf-heroku, but you don’t really need them as you can handle it yourselves in pdfkit.rb file.

You can clone the project from git@github.com:rishijain/pdf-conversion.git or visit the git-repository at https://github.com/rishijain/pdf-conversion

As I have already said, it’s easy getting your pdf in rails as long as you follow the right steps 😉