Skip to content
agelwarg edited this page Aug 1, 2012 · 12 revisions

Welcome to the PDFKit wiki!

Installing

gem install pdfkit

Install WKHTMLTOPDF and WKHTMLTOIMAGE

See https://github.com/pdfkit/pdfkit/wiki/Installing-WKHTMLTOPDF-and-WKHTMLTOIMAGE

Troubleshooting

Unknown long argument --disable-smart-shrinking

This can happen when You use wkhtmltopdf in oder version then You should (for example from Ubuntu repository), to fix this You must use version from http://code.google.com/p/wkhtmltopdf/downloads/list or this from PDFkit/bin

Use on Engine Yard AppCloud

The Unix packages x11-libs/libXext and x11-libs/libXrender are dependencies needed to use PDFKit. Here are the instructions to get pdfkit up and running in the AppCloud.

  1. On Engine Yard's AppCloud, they use gentoo. Let's install the libs. Type into your console:

     sudo emerge libXext libXrender
    
  2. After you install the Unix packages, complete the following

     cd /usr/local/bin/
    

If you have ran the pdfkit --install-wkthmlpdf already and it isn't working do:

  sudo rm wkhtmltopdf
  1. Lastly type into console:

For 32-bit instance:

    curl http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.10.0_beta5-static-i386.tar.lzma | lzcat - | sudo tar xfv -
    sudo mv wkhtmltopdf-i386 wkhtmltopdf

For 64-bit instance:

    curl http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.10.0_rc2-static-amd64.tar.bz2 | bunzip2 - | sudo tar xvf -
    sudo mv wkhtmltopdf-amd64 wkhtmltopdf

How to cache the PDF using Rails page caching and PDFKit

  1. Disable the PDFKit middleware so that your controller handles the generation of the PDF.

  2. Register PDF as a mime-type in environment.rb Mime::Type.register "application/pdf", :pdf

  3. In your controller, cache pages just like normal with caches_page :show

  4. create a format.pdf call in your respond_to block and init a new PDFKit object, pass in the url to the current action and call to_pdf on it.

     respond_to do |format|
       format.html # show.html.erb
       format.pdf { render :text => PDFKit.new( post_url(@post) ).to_pdf }
     end
    

Now in your cache directory you'll create two cache files - one .html and one .pdf.

Optimizations

PDFKit does some rather expensive operations to keep the API simple, like parsing your html to extract stylesheets.
If you need to speed it up see this gist https://gist.github.com/1330326