How To: Install wkhtmltopdf on Amazon Linux

Learn how to install wkhtmltopdf on Amazon Linux to easily generate PDFs from HTML templates.

Posted in #how-to

The other day I was optimizing a reporting engine and needed a quick and easy way to generate PDFs from HTML templates. There are several options out there, but after some research, I decided to use wkhtmltopdf (http://wkhtmltopdf.org). It then took me a few hours of scouring the Internet to find the appropriate steps to install it on Amazon Linux. I kept running into a series of problems with the version, required libraries, and font kerning. Some trial and error, and help from some others, finally got me up and running. Here's what worked for me.

1. Download the Linux (CentOS 6) 64-bit version from http://wkhtmltopdf.org/downloads.html to your Amazon Linux instance (the /tmp directory is fine). Please note that the Linux (CentOS 7) version was not compatible with the latest Amazon Linux build.

wget http://download.gna.org/wkhtmltopdf/0.12/0.12.2.1/wkhtmltox-0.12.2.1_linux-centos6-amd64.rpm

2.  Install the newly download package.

sudo yum install wkhtmltox-0.12.2.1_linux-centos6-amd64.rpm

This may require the installation of some dependencies (e.g. xorg-x11-fonts-75dpi.noarch 0:7.2-9.1.5.amzn1). Just accept.

3. Try running wkhtmltopdf.

wkhtmltopdf http://www.jeremydaly.com jeremydaly.pdf

4. If you get the following: "error while loading shared libraries: libpng15.so.15: cannot open shared object file: No such file or directory", you may need to download and build the libpng library. I don't think you should get this with the CentOS 6 version, but if you do, ping me in the comments.

5. If you don't get an error, then you should have a nicely formatted PDF waiting for you. However, you'll probably notice that the kerning is off on some of the text.

For example:

Bad Kerning Example

Notice how the "ng" in "doing" and the "la" and "ly" in "lately" are too close to one another. There is a quick fix for this that I found on wkhtmltopdf's github page: https://github.com/wkhtmltopdf/wkhtmltopdf/issues/45. You kind of have to dig for it, so here it is:

sudo wget http://pastebin.com/raw.php?i=AmfYN3er -O /etc/fonts/conf.d/10-wkhtmltopdf.conf

So now when you run it you should get something like this:

Proper font kerning

This looks much better.

And that's it. Hope this saves you some time.

Update: The link to download the Linux (CentOS 6) 64-bit version has been updated thanks to Hannah and Andrew.

Comments are currently disabled, but they'll be back soon.