Please Note: pywkhtmltopdf is no longer maintained as of March 2018. It does not support the latest versions of Python 3.
Please instead try pdfkit as a replacement. It is much more feature complete and maintained.
pywkhtmltopdf is a small Python wrapper library around wkhtmltopdf, allowing HTML to PDF conversion from Python code.
It fully supports Python 2.7, with basic Python 3 support provided in version 0.1.1.
This library requires the wkhtmltopdf
binary be present on your system. A
dependency of this binary is Qt4. Most distributions provide a wkhtmltopdf
package, however it will be dynamically linked against vanilla Qt4 and will
only support very basic conversion options.
The creators of wkhtmltopdf have patched Qt4 to support the extra features such as headers and footers. To use this version, we must download a statically linked binary from their website, instead of using your distribution's package:
-
Go to the wkhtmltopdf downloads page
-
Pick the correct architecture of the Stable version and download it (we have tested with 0.12.3 released 2016-01-20)
-
Extract and move the binary to a location on your
$PATH
:$ tar -xf wkhtmltox-*.xz $ cp -p wkhtmltox/bin/wkhtmltopdf $BIN_PATH
-
Install the required dependencies from your system's package manager:
- libXrender
- libXext
- libX11
-
Verify the output of
wkhtmltopdf -V
contains(with patched qt)
A simple example to output some text to a PDF file on disk (example in Python 2.7):
>>> from StringIO import StringIO
>>> import pywkhtmltopdf as pdf
>>> html = StringIO('<p>Hello, world!</p>')
>>> c = pdf.HTMLToPDFConverter()
>>> output = c.convert(html)
>>> with open('test.pdf', 'wb') as fp:
... fp.write(output)
If you haven't installed the wkhtmltopdf
binary into a location on $PATH
,
you must specify the absolute path to the binary using the path_to_bin
keyword argument to the constructor, for example:
>>> c = pdf.HTMLToPDFConverter(path_to_bin='/home/user/wkhtmltopdf')
Other options may be given as keyword arguments to HTMLToPDFConverter.convert()
.
Full API documentation is available by running pydoc pywkhtmltopdf
, and
example scripts can be found under the samples/
directory of this repository.