pdfkit

1.0.0 · maintenance · verified Thu Apr 09

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

Install

Imports

Quickstart

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')

view raw JSON →