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/](http://code.google.com/p/wkhtmltopdf/)

The author, Jakob Truelsen ([
http://www.daimi.au.dk/~jakobt/#about](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/](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](http://github.com/mileszs/wicked_pdf)

**PdfKit:** [
http://github.com/jdpace/PDFKit](http://github.com/jdpace/PDFKit)

I am sure there are additional differences, but I found two important
differences for myself between these gems:

  1) 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.

  2) PdfKit has an option to automatically install the wkhtmltopdf
component. This sold me on PdfKit as I was having trouble installing
wkhtmltopdf.
  <br>

*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)
  <br>

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](/2010/09/20/installing-wkhtmltopdf-on-ubuntu-server/) 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](/2010/09/20/using-wkhtmltopdf-with-ruby-and-rails/) 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 Schuerig 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.


On Tue, Oct 5, 2010 at 2:08 PM, David Kahn <[email protected]>wrote:

> Hi Conor - I just went through this and glad to help. I wrote a couple blog
> posts but it seems that my blog is having some issues so I cant give you the
> links.
>
> What OS are you trying to install on?
>
> What I recommend is installing the static binary. This worked for me ---
> just make sure it is in your path. If you are on 64 bit system use the AMD
> distro. Also, if you are putting it on a non-gui server, you will need the
> static binary or do some other hacky stuff (I used the binary).
>
> Let me know through the group if you get stuck and I can probably get you
> moving.
>
> David
>
>
> On Tue, Oct 5, 2010 at 12:48 PM, Conor Nugent <[email protected]>wrote:
>
>> I was following along with Ryan Bates'  really great railscast on PDFkit
>> when I ran into a few problems installing wkhtmltopdf ( I also posted
>> about my problem in the episode comments). I was wondering if anyone
>> else has experienced similar problems or has any clues as to what is
>> going wrong. It is a rails 3 app, 1.9.2 running on snow leopard. I use
>> rvm
>>
>> I added PDFkit to my gemfile and the appropriate config options to my
>> application.rb. I then tried to see if a could get a pdf version of one
>> of the pages in my app and I got the following action controller
>> exception
>>
>> No wkhtmltopdf executable found at /usr/local/bin/wkhtmltopdf
>> >> Install wkhtmltopdf by hand or try running `pdfkit
>> --install-wkhtmltopdf`
>>
>> I tried installing using the command
>>
>> pdfkit --install-wkhtmltopdf
>>
>> this didn't go smoothly and I had to add the directory and change some
>> permissions but I got it installed. I'm still getting a permission
>> denied exception but as far as I can tell the file is rwx for all
>>
>> It kind of feels like I have drifted down the wrong path completely with
>> this. If anyone has any pointers they would be greatly appreciated
>> --
>> Posted via http://www.ruby-forum.com/.
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Ruby on Rails: Talk" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected]<rubyonrails-talk%[email protected]>
>> .
>> For more options, visit this group at
>> http://groups.google.com/group/rubyonrails-talk?hl=en.
>>
>>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to