Not Logged In

pdfkit 0.6.1

Wkhtmltopdf python wrapper to convert html to pdf using the webkit rendering engine and qt

Python 2 and 3 wrapper for wkhtmltopdf utility to convert HTML to PDF using Webkit.

This is adapted version of ruby PDFKit library, so big thanks to them!

Installation

Install python-pdfkit:

$ pip install pdfkit

Install wkhtmltopdf:

Debian/Ubuntu:

$ sudo apt-get install wkhtmltopdf

Warning! Version in debian/ubuntu repos have reduced functionality (because it compiled without the wkhtmltopdf QT patches), such as adding outlines, headers, footers, TOC etc. To use this options you should install static binary from wkhtmltopdf site or you can use this script.

Windows and other options: check wkhtmltopdf homepage for binary installers

You can specify all wkhtmltopdf options. You can drop ‘–’ in option name. If option without value, use None, False or ” for dict value:. For repeatable options (incl. allow, cookie, custom-header, post, postfile, run-script, replace) you may use a list or a tuple. With option that need multiple values (e.g. –custom-header Authorization secret) we may use a 2-tuple (see example below).

Configuration

Each API call takes an optional configuration paramater. This should be an instance of pdfkit.configuration() API call. It takes the configuration options as initial paramaters. The available options are:

wkhtmltopdf - the location of the wkhtmltopdf binary. By default pdfkit will attempt to locate this using which (on UNIX type systems) or where (on Windows).

meta_tag_prefix - the prefix for pdfkit specific meta tags - by default this is pdfkit-

Troubleshooting

IOError: 'No wkhtmltopdf executable found':

Make sure that you have wkhtmltopdf in your $PATH or set via custom configuration (see preceding section). where wkhtmltopdf in Windows or which wkhtmltopdf on Linux should return actual path to binary.

IOError: 'Command Failed'

This error means that PDFKit was unable to process an input. You can try to directly run a command from error message and see what error caused failure (on some wkhtmltopdf versions this can be cause by segmentation faults)