Wkhtmltopdf and PDFkit

I was following along with Ryan B.’ 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

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

Hi David,

Thanks you so much for your reply. My development environment is snow
leopard and then I deploy to a ubuntu server. To be honest I’m just
trialling stuff at moment but strangely ran into problems getting
things working on snow leopard. I actually figured out what was going
on but I will definitely be looking at your advice when it comes to
deploying to ubuntu.

Basically I figured out that the problem was due where/how the gem is
installed under RVM. I checked where the gem was installed which was

/Users/conor/.rvm/gems/ruby-1.9.2-p0/bundler/gems/PDFKit-652209d

I then ran the command recommended in the POST_INSTALL readme in tis
directory

export TO=which pdfkit | sed 's:/pdfkit:/wkhtmltopdf:' && pdfkit
–install-wkhtmltopdf

which installed wkhtmltopdf to

/Users/conor/.rvm/gems/ruby-1.9.2-p0/bin/wkhtmltopdf

I then set the configuration for PDFKit to point to this path in my
application.rb

#pdfkit
config.middleware.use “PDFKit::Middleware”
PDFKit.configure do |config|
config.wkhtmltopdf =
‘/Users/conor/.rvm/gems/ruby-1.9.2-p0/bin/wkhtmltopdf’
end

Everything seems to be working so far

Great! Really like wkhtmltopdf, the only thing which I really hope they
improve soon is pagination as I found that my tr’s were getting cut off
mid
row if they coincided with the bottom of a page, ended up writing manual
logic to handle pagination.

David

Yeah, I’m really looking forward to playing around with it and thanks
again for your help. Ryan B. mentions a css solution to the problem
you have described in his railscast. Basically involves using

page-break-before: always;

for the items you don’t want split

Here is the ascii version of Ryan’s cast. I hope it might help

http://asciicasts.com/episodes/220-pdfkit

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:

  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.

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.

On Tue, Oct 5, 2010 at 3:37 PM, Conor N. [email protected]
wrote:

installed under RVM. I checked where the gem was installed which was

end

Everything seems to be working so far

Conor - this is an old post and wanted to let you know that what you
found
and describe here completely saved my day as I just rebuilt my mac and
am
now using rvm… this will teach me to think more like you and look for
the
post install readme when I hit problems!

Hi David,

I’m really glad the post helped! Thank you for all your advise too!

Yeah, that does work, but if you do it with the tr, it will do it on
every
tr — so you get a lot of pages! I think what I did was in my logic is
judiciously add the page break css when I am approaching a certain # of
lines. Guess it just depends on what you are trying to do.

Best!

David

Conor N. wrote in post #963078:

Hi David,

I’m really glad the post helped! Thank you for all your advise too!

David, Conor,

I tried to install via the method mentioned by Conor above and when I
generate a PDF, it comes up blank. I also try running from the command
line and I also get a blank PDF. It’s really strange, when I installed
the first time (into the default location) and I ran from command line,
I got it to work with “wkhtmltopdf www.google.com goof.pdf” but it
hasn’t worked since. I have a new MacBook, running rvm with ruby 1.8.7
and rails 3.

Any thoughts you have would be greatly appreciated. I’m wondering if I
somehow need to do a complete delete after the various attempts at
installation, but I don’t see anything else to delete aside from the
wkhtmltopdf file in use/local/bin or the rvm directory. Have either of
you had to install the 64 bit version?

Thanks,
Nick

Hi Nick,

I think that you are right and the problem lies with your installation
of wkhtmltopdf given your command line problems. Maybe try uninstalling
wkhtmltopdf

apt-get remove wkhtmltopdf

and then installing via PDFKit

pdfkit --install-wkhtmltopdf

or manually

Sorry, I know this isn’t much help!

Nick B. wrote in post #964978:

Conor N. wrote in post #963078:

Hi David,

I’m really glad the post helped! Thank you for all your advise too!

David, Conor,

I tried to install via the method mentioned by Conor above and when I
generate a PDF, it comes up blank. I also try running from the command
line and I also get a blank PDF. It’s really strange, when I installed
the first time (into the default location) and I ran from command line,
I got it to work with “wkhtmltopdf www.google.com goof.pdf” but it
hasn’t worked since. I have a new MacBook, running rvm with ruby 1.8.7
and rails 3.

Any thoughts you have would be greatly appreciated. I’m wondering if I
somehow need to do a complete delete after the various attempts at
installation, but I don’t see anything else to delete aside from the
wkhtmltopdf file in use/local/bin or the rvm directory. Have either of
you had to install the 64 bit version?

Thanks,
Nick

On Tue, Nov 30, 2010 at 3:25 AM, Conor N. [email protected]
wrote:

pdfkit --install-wkhtmltopdf

or manually

Sorry, I know this isn’t much help!

Right… also, when you say your pdf is blank — does the file actually
open or is it empty? I had a situation where I was getting empty files,
which of course would not open. I’ll try to remember what the solution
was
if you let me know what your situation is.

Ok, here’s one for you. I ran “wkhtmltopdf -H www.google.com
google.pdf” and got a blank document but it had the header. Then I
loaded an html file that I had on my local computer and it worked…
good stuff. It appears that my command line is just having trouble
reaching the internet. I’m a hotel this week, so perhaps it’s something
to do with the way their wireless connection is set up. Now I just have
to figure out why I’m getting an error when using PDFKIT… thanks for
your help

On Tue, Nov 30, 2010 at 12:13 PM, Nick B. [email protected]
wrote:

stated above. It runs from the command line, but it still creates a
blank pdf. It’s funny, the pdfs open but there is just no content. I’m
stumped.

Probably goes without saying but have you verified that your input file
contains data?

Also, if you just enter ‘wkhtmltopdf’ in the terminal, what do you get?
(you
should get some help info on wkhtmlto pdf)

Also, try this – esp if you are not getting wkhtml to respond:

strace wkhtmltopdf #{path_html} #{path_pdf}
(obviously setting the files to real files, not variables as I have it)

Also,

:slight_smile: Yep… it should go without saying… but that seems to be exactly
what my problem is. Go figure. I’m still having an error issue inside
Rails, but I think that has something to do with the configuration.

David K. wrote in post #965093:

On Tue, Nov 30, 2010 at 3:25 AM, Conor N. [email protected]
wrote:

pdfkit --install-wkhtmltopdf

or manually

Sorry, I know this isn’t much help!

Right… also, when you say your pdf is blank — does the file actually
open or is it empty? I had a situation where I was getting empty files,
which of course would not open. I’ll try to remember what the solution
was
if you let me know what your situation is.

Thanks for replying guys,

I tried to revert back to my system Ruby (out of RVM) and installing
pdfkit via Gem install. Then in installed wkhtmltopdf with pdfkit as
stated above. It runs from the command line, but it still creates a
blank pdf. It’s funny, the pdfs open but there is just no content. I’m
stumped.

Nick

Again- I’m not sure I’m really helping here at all but I had to add the
following to application.rb

#pdfkit
config.middleware.use “PDFKit::Middleware”, :print_media_type =>
true
PDFKit.configure do |config|
config.wkhtmltopdf =
‘/Users/conor/.rvm/gems/ruby-1.9.2-p0/bin/wkhtmltopdf’
end

This if you want to use PDFKit as rack middleware

Nick B. wrote in post #965213:

:slight_smile: Yep… it should go without saying… but that seems to be exactly
what my problem is. Go figure. I’m still having an error issue inside
Rails, but I think that has something to do with the configuration.

I just got the wkhtmltopdf installed on my Mac Snow Leopard and tried to
get the pdfkit to work with Rails 3.0.3

In my application.rb file I have put this:

config.middleware.use PDFKit::Middleware
PDFKit.configure do |config|
  config.wkhtmltopdf = '/usr/local/bin/wkhtmltopdf'
end

Now this is SUPPOSED to allow me to add a .pdf extension to my URL’s to
get a PDF version of the page, however, when I do that, from that point
on I get ALL pages as pdf. ie. if I then click on a link in the PDF, it
generates another pdf file of the next page, etc.

If I try and just go to a page without the PDF, it continues to generate
PDFs until I stop the server.

Anyone else have this problem?

These are the relevant gems installed:
wkhtmltopdf-binary (0.9.5.1)
pdfkit (0.5.0)
wkhtmltopdf 0.9.9

On Tue, Nov 30, 2010 at 1:13 PM, Nick B. [email protected]
wrote:

:slight_smile: Yep… it should go without saying… but that seems to be exactly
what my problem is. Go figure. I’m still having an error issue inside
Rails, but I think that has something to do with the configuration.

Hmmm… well as far as the config, if you can hit wkhtmltopdf from the
command line then you should not have to do anything else, unless you
are
using a gem like pdfkit

I use wkthmltopdf straight like:

# tell wkhtmltopdf to convert html file to pdf
if Rails.env=="heroku"
  %x[vendor/wkhtmltopdf/wkhtmltopdf-amd64 #{html_path} 

#{new_pdf_path}]
else
%x[wkhtmltopdf #{html_path} #{new_pdf_path}]
end