How to Generate PDF Reports from HTML Templates in Python
How to Generate PDF Reports from HTML Templates in Python You're building a web app. A user clicks "Download Invoice" and expects a professional PDF. You reach for wkhtmltopdf or weasyprint and... ...

Source: DEV Community
How to Generate PDF Reports from HTML Templates in Python You're building a web app. A user clicks "Download Invoice" and expects a professional PDF. You reach for wkhtmltopdf or weasyprint and... it works, but now you're managing a headless process. It crashes. It's slow. It ties up a worker thread. There's a simpler pattern: render HTML → send to API → get PDF back. Here's how to generate PDF reports from Jinja2 templates using a hosted PDF API. The Problem: Self-Hosted PDF Generation Is Heavy Self-hosted PDF libraries add complexity: # Self-hosted wkhtmltopdf: process management overhead from pdfkit import from_string html_string = render_template('invoice.html', data=invoice_data) pdf_bytes = from_string(html_string, False) # Spawns process, uses memory What this costs: Memory: 50–150MB per PDF generation Time: 2–4 seconds per render Complexity: Shell escaping, process cleanup, error handling Reliability: Process can crash or hang Scalability: Can't generate 100 PDFs in parallel wi