PS, I am copying the text of my blog below… may not be pretty but in
there
is some info on installing the static binary:
title: Using wkhtmltopdf with Ruby and Rails
date: 2010-09-20
description: Pdf generation with wkhtmltopdf on Rails
I recently had the requirement of serving a certain report from a Rails
based system in pdf format. I went through some trial and error and
research
in several areas before I came up with what ended up working for us,
which
is wkhtmltopdf (Web Kit Html to Pdf). Hope this helps someone else out
there.
To digress for a moment, this is the site for wkthmltopdf:
http://code.google.com/p/wkhtmltopdf/
The author, Jakob Truelsen (
http://www.daimi.au.dk/~jakobt/#about)
was very helpful and responsive when I emailed him directly about a
problem
to which I could not find an answer.
Back to the main event now, there was the decision to use a DSL such as
Prawn or to find an html to pdf converter. In the past I have been happy
enough with the latter, but on first glance what was available for Rails
was
either not ready for prime time or costly (Prince). I recommend someone
making this decision to visit John McCaffreys site and watch his
screencast.
In it he describes three methods of generating pdfs and the pros/cons to
each. It was this resource which led me to look at wkhtmltopdf:
http://prawn.heroku.com/
Once I began playing with wkhtmltopdf I found that there were two
Ruby/Rails
gems/libraries available to help with this:
wicked_pdf:
http://github.com/mileszs/wicked_pdf
PdfKit:
http://github.com/jdpace/PDFKit
I am sure there are additional differences, but I found two important
differences for myself between these gems:
-
PdfKit has an optional middleware layer which allows any url called
with the .pdf extension to output its content in pdf format without
modification to the controller. If you don’t use it, you can generate
pdfs
explicitly on a given controller.
-
PdfKit has an option to automatically install the wkhtmltopdf
component. This sold me on PdfKit as I was having trouble installing
wkhtmltopdf.
Everything went well using PdfKit until I noticed a fatal issue: the
links in the pdf were not being made active.
Since PdfKit had installed wkhtmltopdf, I decided to try a direct
conversion
using wkhtmltopdf directly (see below), and found that in using
wkhtmltopdf
directly did make the links active. From this point forward, I bypassed
the
Ruby gems and used wkthmltopdf directly.
This is the basic command to generate a pdf with wkhtmltopdf:
wkhtmltopdf path_html path_pdf
Or from within Ruby code:
%x[wkhtmltopdf #{path_html} #{path_pdf}]
(where path_html is either a url or file path to an html document, and
path_pdf is the path and name of your output document)
Going forward on my development environment (Mac Leopard), there were no
issues, except that I did have to manually handle pagination for table
based data (tables were getting cut-off mid ‘tr’). Hopefully wkhtmltopdf
will handle this in the future.
My next article
is
about installing wkhtmltopdf on Ubuntu Server 10.04
title: Installing wkhtmltopdf on Ubuntu Server
date: 2010-09-20
description: Pdf generation with wkhtmltopdf on Rails
In a previous post
I
wrote about using wkhtmltopdf for html to pdf conversion with Ruby and
Rails.
As can by typical with components, moving them to production you hope
will
be straight forward but is often not. Such was the case for me on
getting
wkthmltopdf working on my Ubuntu Server 10.04 server from my Mac Leopard
dev
environment.
The first question was how to install wkhtmltopdf on the server. Since
I
had not been successful installing it on my own on my Mac (I used the
PdfKit ruby gem to install it), it was not clear if I would succeed
here.
I ended up finding that there is a package for wkhtmltopdf for Ubuntu:
sudo apt-get install wkthmltopdf
This package did its work and it installed. It seemed too easy. And it
was.
While wkhtmltopdf (v0.9.9) did install, I was soon getting the following
and
dreaded error:
"Cannot connect to X server"
The reason for this error is that the current incarnation of Web Kit
requires a GUI. Hopefully this will change in the future.
After some research I found what looked like a solution:
Use Xvfb (‘X Virtual Frame Buffer’). Xvfb promised to create a
lightweight,
temporary situation that would trick wkhtmltopdf into running. Please
excuse
my un-technical and probably inaccurate description of what Xvfb does,
but
you get the point.
So I did:
sudo apt-get install xvfb
And in the terminal I now could run wkhtmltopdf and see an output:
xvfb-run -a -s "-screen 0 640x480x16" wkhtmltopdf path_html path_pdf
It worked also from my Rails app:
%x[xvfb-run -a -s "-screen 0 640x480x16" wkhtmltopdf #{path_html}
#{path_pdf}]
It worked great! At least until… I discovered that those darned links
in
the converted document were not active. I could not find an answer for
this,
so kept googling. My sense was that this problem must have something to
do
with running wkhtmltopdf through xvfb.
I ended up being right, and the solution was to use a patched QT in lieu
of
xvfb.
So I decided to try installing the static binary of wkhtmltopdf as
follows:
1) Uninstall the wkhtmltopdf package: apt-get remove wkhtmltopdf
2) (in usr/local/bin) sudo curl -C - -O
http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-amd64.tar.bz2
3) (in usr/local/bin) sudo tar -xvjf
wkhtmltopdf-0.9.9-static-amd64.tar.bz2
I originally was trying to install the wrong wkhtmltopdf static binary
for
my machine. I have 64bit Linux and it was not obvious to me that I
should
use the binary labelled ‘amd’. I thank Michael S. on the Rails
Google
Group for his insight. So make sure you have the right one for your
machine.
Initially when I had the wrong binary installed, when I would run it
from
the terminal, it looked like it executed (no error), but with no output.
Also thanks to Michael, I ran:
strace wkhtmltopdf #{path_html} #{path_pdf}
This showed me that a certain linux source file was missing and led to
resolving the problem. In short, if one of the binaries does not work,
try
the other.
Once I got this installed everything worked, links were rendering and
the
client happy.