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 pdfkitVerified import paths — ran on the pinned version, not inferred.
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.
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.
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')`.
For full functionality, download a static binary from the official `wkhtmltopdf.org` website instead of using `apt-get`.
Ensure your HTML content explicitly includes `<meta charset="utf-8">` within the `<head>` section. Also, specify `encoding='UTF-8'` in `pdfkit` options if necessary.
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.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.
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.
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.
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.