Generate PDFs in Django
Return a downloadable PDF straight from a Django view with a single requests call.
In Django, the simplest way to serve a PDF is a view that calls PDFgen and returns the bytes as an HttpResponse. No wkhtmltopdf binary on your servers, no xhtml2pdf quirks.
Render your Django template to HTML if you like, or pass Handlebars HTML and data straight through. The view below returns a downloadable invoice.
Install
pip install requests
Generate a PDF in Django
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 requestsfrom django.conf import settingsfrom django.http import HttpResponsedef invoice_pdf(request):res = requests.post("https://pdfgen.com/api/v1/generate",headers={"Authorization": f"Bearer {settings.PDFGEN_API_KEY}"},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"},},)response = HttpResponse(res.content, content_type="application/pdf")response["Content-Disposition"] = "attachment; filename=invoice.pdf"return response
Convert existing HTML to PDF in Django
Already have finished HTML? Skip templating — render a Django template to HTML, then convert it and send it with engine: "legacy", which tells the API your markup is final.
import requestsfrom django.conf import settingsfrom django.http import HttpResponsefrom django.template.loader import render_to_stringdef invoice_pdf(request):# Render an existing Django template to an HTML string.html = render_to_string("invoice.html", {"number": "INV-001"})res = requests.post("https://pdfgen.com/api/v1/generate",headers={"Authorization": f"Bearer {settings.PDFGEN_API_KEY}"},json={"html": html, "engine": "legacy", "format": "A4"},)response = HttpResponse(res.content, content_type="application/pdf")response["Content-Disposition"] = "attachment; filename=invoice.pdf"return response
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 Django.
Good to know
- Keep PDFGEN_API_KEY in settings, loaded from an environment variable.
- You can render an existing Django template with render_to_string() and pass that as the html field.
- Use content_type="application/pdf" so browsers handle the response as a file.
Frequently asked questions
- How do I return a PDF from a Django view?
- Call /api/v1/generate with requests, then return HttpResponse(res.content, content_type="application/pdf") with a Content-Disposition header.
- Can I use my existing Django templates?
- Yes — render the template to a string with render_to_string() and pass it as the html field (use engine "html" if you don’t need Handlebars).
- Do I need wkhtmltopdf or a system package?
- No. Rendering happens on PDFgen, so there’s nothing native to install on your Django servers.