Migration guides · Python

Replace wkhtmltopdf with pdfkiln in pdfkit

python-pdfkit runs wkhtmltopdf for from_url, from_file and from_string, found in the PATH or in the configuration you pass.

1. Install pdfkiln

On Debian and Ubuntu, amd64 and arm64: the signed APT repository installs /usr/bin/wkhtmltopdf in place of the wkhtmltox package, and brings Chromium’s fixes with apt upgrade.

curl -fsS https://apt.pdfkiln.org/pdfkiln-archive-keyring.asc | sudo tee /usr/share/keyrings/pdfkiln-archive-keyring.asc > /dev/null
printf 'Types: deb\nURIs: https://apt.pdfkiln.org\nSuites: stable\nComponents: main\nSigned-By: /usr/share/keyrings/pdfkiln-archive-keyring.asc\n' \
  | sudo tee /etc/apt/sources.list.d/pdfkiln.sources
sudo apt update && sudo apt install pdfkiln

Elsewhere: the Docker image, the archives and the Windows installer.

2. Point pdfkit at it

import pdfkit

# Nothing to do if wkhtmltopdf is in the PATH: the Debian package puts it in /usr/bin
pdfkit.from_url('https://example.com', 'out.pdf')

# Otherwise, give its path
config = pdfkit.configuration(wkhtmltopdf='/usr/bin/wkhtmltopdf')
pdfkit.from_string(html, 'out.pdf', configuration=config)

On Windows, pdfkiln installs in C:\Program Files\wkhtmltopdf, the path pdfkit’s guides use: pdfkit.configuration(wkhtmltopdf=r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe') keeps working.

3. Check

wkhtmltopdf --version
# wkhtmltopdf 0.12.6 (with patched qt), pdfkiln 1.0.4, Chromium 156.0.8078.80

The answer starts as wkhtmltopdf 0.12.6’s does: libraries that read the version keep working.

What pdfkit expects, and what pdfkiln does

  • pdfkit sends the page on standard input and reads the PDF on standard output (- on both sides, with --quiet): pdfkiln handles both.
  • On a non-zero exit code, pdfkit keeps the PDF if the second-to-last line of the error output is Done: pdfkiln writes that line, outside --quiet, as wkhtmltopdf did.
  • Local images and style sheets need options={'enable-local-file-access': ''} or --allow: by default, as in wkhtmltopdf 0.12.6, a page reads no other local file.

Tested

pdfkit 1.0.0’s own test suite passes with pdfkiln in place of wkhtmltopdf (Ubuntu, CI job libraries).

Something renders differently? Open an issue with the HTML and both PDFs: github.com/Jamalofski/pdfkiln/issues. The known differences are listed in the README.