pdfkit
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.
Warnings
- breaking The 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.
- gotcha Common `IOError: 'No wkhtmltopdf executable found'` occurs if `wkhtmltopdf` is not installed or not in the system's PATH. `pdfkit` cannot function without it.
- gotcha Installing `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.
- gotcha Non-ASCII characters (e.g., special symbols, accented letters) in HTML can lead to rendering issues or blank output in PDFs.
- breaking Starting 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.
Install
-
pip install pdfkit -
sudo apt-get install wkhtmltopdf -
brew install homebrew/cask/wkhtmltopdf -
Download installer from wkhtmltopdf.org and add to PATH
Imports
- pdfkit
import pdfkit
Quickstart
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')