Generate PDFs in Python
Render PDFs from any Python script or service with requests — no native PDF libraries to install.
Generating PDFs in Python often means wrestling with wkhtmltopdf binaries or WeasyPrint’s system dependencies. PDFgen needs none of that: POST your HTML and data with requests and write the returned bytes to a file.
The same call works from a script, a Celery task or a web request. Loop over a queryset to render one PDF per record.
Install
pip install requests
Generate a PDF in Python
POST your HTML and data to /api/v1/generate with your API key, then save the PDF that comes back. Swap in your own HTML or a saved template_id and loop over records to render at scale.
import requestsres = requests.post("https://pdfgen.com/api/v1/generate",headers={"Authorization": "Bearer pdfg_live_xxx"},json={"html": "<h1>Invoice {{number}}</h1><p>Billed to {{client}} — total {{total}}</p>","engine": "handlebars","format": "A4","data": {"number": "INV-001", "client": "Acme Corp", "total": "$2,400.00"},},)# Default response is the PDF binary. Pass "export_type": "url" for a hosted link.with open("invoice.pdf", "wb") as f:f.write(res.content)
Convert existing HTML to PDF in Python
Already have finished HTML? Skip templating — read an .html file and POST it with requests and send it with engine: "legacy", which tells the API your markup is final.
import requests# Your existing, already-styled HTML file.with open("invoice.html") as f:html = f.read()res = requests.post("https://pdfgen.com/api/v1/generate",headers={"Authorization": "Bearer pdfg_live_xxx"},# engine "legacy" = treat the HTML as final, no templating.json={"html": html, "engine": "legacy", "format": "A4"},)with open("invoice.pdf", "wb") as f:f.write(res.content)
How it works
- 1
Send your HTML + data
POST an HTML template (with optional Handlebars tokens) and a data object to the API.
- 2
We render the PDF
PDFgen renders it server-side — no headless browser for you to run or scale.
- 3
Get the PDF back
Receive the PDF binary (or a hosted link with export_type:"url") and serve it from Python.
Good to know
- No system binaries — unlike wkhtmltopdf or WeasyPrint, there’s nothing native to install.
- Store your API key in an environment variable and read it with os.environ.
- res.content is the raw PDF; send "export_type": "url" to get a hosted, expiring link instead.
Frequently asked questions
- What’s the easiest way to make a PDF in Python?
- POST your HTML and data to /api/v1/generate with the requests library and write res.content to a file — no native PDF engine to install.
- Is this better than wkhtmltopdf or WeasyPrint?
- It removes the system-dependency and binary-version headaches: rendering runs on PDFgen, so your Python service just makes an HTTP call.
- Can I use it inside Django or Flask?
- Yes — see the Django guide, or make the same requests call from a Flask view and return the bytes as an application/pdf response.