Loading...
 
Skip to main content

Features / Usability


How to use the wkhtmltopdf PDF converter

posts: 210

Hi,

I tried to enable PDF output on my 6.7 LTS, using Webkit. So I downloaded the binary version 0.11, wkhtmltopdf-i386. I uploaded it to my webspace and gave it a chmod a+x to be sure it can be executed.

I enabled PDF output in tiki, switched it to Webkit, and entered the entire path to the binary, /homepages/41/myaccountnumber/htdocs/tiki/lib/wkhtmltopdf-i386

But when I hit the PDF button on my wiki pages, it gives me either output of 0 bytes or some 12 KByte of binary garbage.

So I wanted to find out whether I can invoke a binary on my webspace, and wrote a fourliner in PHP to do shell_exec on exactly the same path, with only one parameter, --version.

And it correctly prints out its version, so it can be invoked using PHP on this webspace. So what could be wrong that it does not work in tiki? PHP is not in safe mode, and shell_exec is not prohibited by php.ini (which would also have affected the above test...).

Thanks alot
Oliver

posts: 210
Sorry, it's of course a 6.12, not 6.7 LTS...

posts: 210

I could solve the basic startup problems. I will write a more detailed description later on. Key issues:

a) The static version of wkhtmltopdf isn't overly static. It needs some few libraries of X that have to be downloaded _somewhere else_.
b) The current "latest" version 0.11 is dysfunctional (dies) for many people. Also the direct predecessor 0.10rc2. Go back to 0.99, that does the trick.
c) Do not start wkhtmltopdf directly from TikiWiki via the setup on the Admin panel. You must use a small wrapper shell script that sets the path to the above mentiones X libraries and set the parameter --quiet (otherwise some stdout output will ruin the pdf).
d) Do not forget to set execution rights to wkhtmltopdf, all libraries and the wrapper script...

So, now I can generate PDFs. But not from within my wiki, only from the start page - as the inner wiki only visibly after login, and it looks like wkhtmltopdf loads pages over http/https without user credentials (should be better loading them locally, that could be handled by the wrapper shell, but I don't have time to write the parsing code right now).

But I saw somewhere a small text that addressed the login issue somehow.


posts: 210
Okay, to fix the problem that only the login screen gets converted to PDF if you don't have public access (which I don't) all you have to do is go to the security page on the Admin panel and enable token access. Now the PDF converter works on all pages. I will test drive it further, and write a better documentation on how setup wkhtmltopdf.

posts: 210

Okay, here is the complete guide to setting up PDF export with wkhtmltopdf so that it works (almost, but I'll come to that later).

First, make sure you can upload and run binaries to your webspace. On cheaper offers binaries might not be allowed. But you do not need full-blown shell access. My Business 5.0 from 1&1 also offers no shell access, but it allows binaries. Without binaries wkhtmltopdf is no solution for you.

a) Determine the architecture of your web server by looking into it's documentation. Most common are i386 and AMD64.

b) Download wkhtmltopdf at http://code.google.com/p/wkhtmltopdf/ but do NOT use the latest version 0.11rc1 or 0.10rc2, use 0.99. Make sure you get the "static" version that fits to your server's architecture. And don't accidentally load wkhtmltox or wkhtmltoimage...

c) Unpack it (bz2, this is best done on a Linux client, but Windows software that can handle this does exist). If you want, you can rename it, but that is not necessary.

d) Upload to your webspace where you like to have it. If you want, into a separate directory.

e) Set execution rights. If you cannot use chmod on a shell, like me, your provider may have a web tool to set a file's rights. On 1&1 that's the webspace explorer on the webspace config site.

f) Create a directory preferrably named lib for the libraries, in the directory where wkhtmltopdf sits.

g) Go to http://www.debian.org/distrib/packages#search_contents and search for these _package contents_ and download the packages, for the architecture of your server. I used packages from Wheezy:
libfontconfig.so.1
libfreetype.so.6
libX11.so.6
libXau.so.6
libxcb.so.1
libXdmcp.so.6
libXext.so.6

h) Extract the libraries out of the archives (there are always two files with almost the same name in data.tar.gz/usr/lib/yourarchitecture), the larger one is the actual library. The other one is just a symlink, and possibly you cannot symlink on your webspace. I simply renamed the large files to what the smaller was named, but that's optional).

i) Upload all libaries to your newly created library folder.

j) Set execution rights to all libraries.

k) Determine the absolute path to your webspace by your documentation.

l) Create a wrapper shell script, maybe as wrapper.sh with these commands. The last line is commented out. If you want to debug, comment the other line out. For debugging you can also eliminate the --quiet command.

  1. !/bin/sh

export HOME="/absolute/path/to/your/wkhtml-directory"
export LD_LIBRARY_PATH="/absolute/path/to/your/wkhtml-library-directory"
exec /absolute/path/to/your/wkhtml-directory/wkhtmltopdf --quiet $@ 2>/dev/null

  1. exec /absolute/path/to/your/wkhtml-directory/wkhtmltopdf --quiet $@ 2>&1 # debug mode


m) Upload the wrapper script.

n) Set execution rights to script.

o) Enable PDF output by wkhtmltopdf on the Admin page / General

p) Do NOT enter the path to wkhtmltopdf, but instead the absolute and full path to the wrapper script, containing the name of your script.

q) Check on the PHP info page that nothing blocks shell_exec and that you do not run in safe mode.

r) If necessary find and edit your php.ini to not do the above. Maybe you have to set this on the config page of your webspace.

s) Well - that's it. This did the trick for me, after many frustrating experiments.

Oliver


Thanks to these docs on the net:
http://stackoverflow.com/questions/2273534/how-to-install-wkhtmltopdf-on-a-linux-based-web-server
https://drupal.org/node/870058
the discussion:
http://code.google.com/p/wkhtmltopdf/issues/detail?id=730
and of course thanks to Jakob Truelsen for writing wkhtmltopdf !

Note 1: So far, due to a repackaging, I was unable to locate library libxcb-xlib.so.0 but it seems to run without. If anyone knows more about it, please drop a note.

Note 2: Sometimes small images don't convert correctly, but otherwise the above scenario does work for me. At the moment I can live with that.


posts: 210

Oops, s) should have been:

s) If your wiki is not entirely readable by anonymous, enable token access on the security page, otherwise you will only see the login screen in your PDFs.