HTML to PDF in Python

Python has good local options like WeasyPrint and wkhtmltopdf, but both mean installing and maintaining native dependencies — and wkhtmltopdf is no longer maintained. pdfkitt skips all of that: one requests call renders your HTML with Chromium and returns a PDF.

One requests call

import os
import requests

resp = requests.post(
    "https://api.pdfkitt.dev/v1/convert",
    headers={"Authorization": "Bearer " + os.environ["PDFKITT_API_KEY"]},
    json={"html": "<h1>Invoice</h1>", "options": {"page_size": "A4"}},
)
resp.raise_for_status()

with open("invoice.pdf", "wb") as f:
    f.write(resp.content)

Generating PDF invoices

A common use case is invoices: render an HTML template with Jinja2, then post the result to pdfkitt instead of running a local renderer. Because pdfkitt uses Chromium, your invoice looks the same in the PDF as it does in the browser — including modern CSS and web fonts. For the full request and response shape, see the API docs, and compare plans on pricing.

Try pdfkitt free — 1,000 PDFs/month, no credit card

Get an API key and send your first PDF in minutes.