Registry / web-framework / pdfkit

pdfkit

JSON →
library1.0.0pypypi✓ verified 24d ago

pdfkit is a Python wrapper for `wkhtmltopdf`, a command-line tool that renders HTML content into PDF documents using the WebKit engine. It provides an easy-to-use API to convert HTML from URLs, local files, or raw strings into PDF. While the `pdfkit` library itself is at version 1.0.0, its core dependency, `wkhtmltopdf`, was archived in January 2023, meaning `pdfkit` relies on an unmaintained external tool.

pip install pdfkit
INSTALL
IMPORT
SIG · PDFKIT
P
pdfkit
web-frameworkpythonv1.0.0
Install
1.5s avg
Import
16ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.016s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.016s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

pdfkit
import pdfkit

This quickstart demonstrates converting an HTML string to a PDF file. Ensure `wkhtmltopdf` is installed and its executable is discoverable in your system's PATH. For URLs or local files, use `pdfkit.from_url` or `pdfkit.from_file` respectively.

import pdfkit import os # Ensure wkhtmltopdf is in your PATH or specify its path # config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf') # For simplicity in quickstart, assumes wkhtmltopdf is in PATH. html_content = """ <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>My PDF Document</title> <style> body { font-family: sans-serif; } h1 { color: #333; } </style> </head> <body> <h1>Hello from PDFKit!</h1> <p>This is a simple HTML string converted to a PDF.</p> <p>Current directory: {}</p> </body> </html> """.format(os.getcwd()) output_filename = 'example.pdf' pdfkit.from_string(html_content, output_filename) print(f"PDF generated: {output_filename}") # Example from URL # pdfkit.from_url('http://google.com', 'google.pdf') # Example from file # with open('input.html', 'w') as f: # f.write('<h1>Test File</h1>') # pdfkit.from_file('input.html', 'file.pdf')
Debug
Known issues
breakingThe underlying `wkhtmltopdf` project, which `pdfkit` wraps, was archived in January 2023 and is no longer maintained. While `pdfkit` may still function with existing `wkhtmltopdf` binaries, this poses long-term risks for security, compatibility, and new feature development.
fix
Be aware of the upstream status. For new projects, consider actively maintained alternatives like Playwright, WeasyPrint, or dedicated PDF generation APIs if long-term support is critical.
affects: All versions of pdfkit relying on wkhtmltopdf
gotchaCommon `IOError: 'No wkhtmltopdf executable found'` occurs if `wkhtmltopdf` is not installed or not in the system's PATH. `pdfkit` cannot function without it.
fix
Install `wkhtmltopdf` separately (e.g., via `brew`, `apt-get`, or direct download for Windows) and ensure its executable's directory is included in your system's PATH. Alternatively, specify the full path to the `wkhtmltopdf` binary using `pdfkit.configuration(wkhtmltopdf='/path/to/wkhtmltopdf_binary')`.
affects: All
gotchaInstalling `wkhtmltopdf` via `apt-get` on Debian/Ubuntu often provides a version with reduced functionality (compiled without QT patches). This version lacks features like outlines, headers, footers, and table of contents.
fix
For full functionality, download a static binary from the official `wkhtmltopdf.org` website instead of using `apt-get`.
affects: All
gotchaNon-ASCII characters (e.g., special symbols, accented letters) in HTML can lead to rendering issues or blank output in PDFs.
fix
Ensure your HTML content explicitly includes `<meta charset="utf-8">` within the `<head>` section. Also, specify `encoding='UTF-8'` in `pdfkit` options if necessary.
affects: All
breakingStarting with `pdfkit` version 1.0.0, the `--quiet` option is passed to `wkhtmltopdf` by default, suppressing its output to stdout/stderr. This can make debugging difficult.
fix
To view `wkhtmltopdf`'s output for debugging purposes, pass `options={'enable-javascript': True, 'no-stop-slow-scripts': True}` or `verbose=True` to the `pdfkit.from_*` calls to disable the quiet mode or get detailed output.
affects: 1.0.0 and later
Errors
Common errors & fixes
OSError: No wkhtmltopdf executable found: "b''"
This error occurs because `pdfkit` is a Python wrapper and requires the `wkhtmltopdf` command-line tool to be installed on your system and accessible via the system's PATH, or its path must be explicitly provided to `pdfkit`.
fix
First, install `wkhtmltopdf` on your operating system (e.g., `sudo apt-get install wkhtmltopdf` on Linux, or download an installer from `wkhtmltopdf.org` for Windows/macOS). If it's installed but still not found, add its executable directory to your system's PATH, or specify the path directly in your Python code: `config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf')` (adjust path as needed) and then pass `configuration=config` to `pdfkit` functions.
ModuleNotFoundError: No module named 'pdfkit'
This is a standard Python error indicating that the `pdfkit` library has not been installed in the Python environment currently being used, or there's an issue with the Python environment itself (e.g., multiple Python installations).
fix
Install the `pdfkit` library using pip: `pip install pdfkit`. If you have multiple Python versions, ensure you're installing it for the correct one (e.g., `pip3 install pdfkit`) and running your script with that same Python interpreter.
IOError: 'Command Failed' (or 'wkhtmltopdf exited with non-zero code X')
This error means that `pdfkit` successfully found and invoked `wkhtmltopdf`, but `wkhtmltopdf` itself failed to convert the HTML into a PDF. This can be due to malformed HTML, missing system dependencies for `wkhtmltopdf` (like Xvfb on headless Linux servers), or issues specific to the `wkhtmltopdf` version being used (e.g., outdated or 'reduced functionality' versions from OS package managers).
fix
To diagnose, pass `verbose=True` to your `pdfkit` call (e.g., `pdfkit.from_string(html, 'out.pdf', verbose=True)`) to see the raw `wkhtmltopdf` output. For headless Linux servers, ensure `xvfb` is installed and used (e.g., `xvfb-run wkhtmltopdf ...`). Consider installing a static binary release of `wkhtmltopdf` from its official website instead of relying on potentially outdated or feature-reduced versions from OS package repositories.
Package wkhtmltopdf is not available, but is referred to by another package.
This specific error often arises in containerized or cloud environments (like Streamlit Community Cloud) where the `wkhtmltopdf` system package might no longer be available in the default Debian repositories or due to repository issues.
fix
On platforms where `wkhtmltopdf` system packages are unavailable, you may need to find an alternative installation method, manually provide a static binary, or consider switching to a pure Python PDF generation library like WeasyPrint or ReportLab if `wkhtmltopdf` is not a strict requirement and direct system binary installation is constrained.
Upgrade
Version history
1.0.0latest on PyPI · released Nov 14, 2021
Audit
Dependencies
wkhtmltopdfrequiredExternal command-line tool required for HTML to PDF conversion. It must be installed separately and be accessible in the system's PATH.
Agent activity
10 hits · last 30 days
node
8
Resources
pdfkit — pip install pdfkit · libregistry