Converting HTML to PDF

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 ;)

About these ads

About Rishi Jain

I am a ruby on rails developer at Josh Software, Pune.
This entry was posted in Ruby and tagged , . Bookmark the permalink.

10 Responses to Converting HTML to PDF

  1. Hi,
    I am using wicked_pdf gem for generating pdf. But on amazon 64 bit ec2 machine i am facing lots of issues for the wkhtmltopdf version you mentioned above. is there any other way to make pdf generation fast and without any issue.
    What configuration needs to add in ngnix conf file for generating fast pdf

  2. Pratik Shah says:

    Hi sunny, I would be able to help you if you can give me what exactly problem you are facing.
    You can use PdfKit gem if pdf you are generating is simple. Both gems uses wkhtmltopdf at the backend. But pdfkit is less complex to integrate.

  3. shrikant says:

    Hi, i am shrikant,
    I am using pdfkit for pdf generation in rails project.After opening that pdf ,it is showing the error that “cannot extract the embedded font ‘ArialRegular’. Some characters may not display or print correctly” I have used below code in config/initializer/pdfkit.rb

    pdfkit.configure do |config|
    config.wkhtmltopdf = ‘C:/Program files/wkhtmltopdf/wkhtmltopdf.exe’
    config.default_options ={
    :page_size => ‘Legal’,
    :print_media_type => true
    }
    #config.root_url = “http://localhost”
    end
    but when i added this it gives me error at root_url so i hide it it worked well but it gives me above problem Please help me

  4. Zarfishan Zahid says:

    You can use online converter like Saaspose.PDF for converting HTML to PDF and because its a cloud API you don’t have to download it just upload your HTML file and it will be converted to PDF.

    • Gautam Rege says:

      Interesting! I would still have to save the html locally before sending it for PDF conversion right? Don’t think we can send just a URL (with session management) to convert to PDF. Can we?

  5. Binh says:

    shrikant!
    You can’t edit the pdf file if it’s converted from Windows, but that tool could be done in Ubunto/Linux. I think the tool caould not read the right fonts on windows.

  6. Kiran Gupta says:

    Great information…you can use http://www.html-to-pdf.net
    I am using thsi and it converts your files in a minutes without any hassle.

  7. Rishi says:

    Reblogged this on jainrishi37 and commented:
    There are many ways to convert html to pdf for rails applications. But using pdfkit is one of the most efficient way of doing it.

  8. Pingback: Converting HTML to PDF | jainrishi37

  9. Pingback: Converting HTML to PDF | Ruby on Rails

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s