Automated PDF Invoice Generation: Build It in 10 Minutes
Every SaaS needs invoices. The question is: do you spend a week integrating wkhtmltopdf, fighting Puppeteer in Docker, or wrestling with LaTeX templates? Or do you send one API call and get a PDF back?
The HTML template approach
The fastest way to generate invoices is to design them in HTML/CSS — tools you already know — then render to PDF via API. Here's a complete example.
Step 1: Build your invoice template
<div style="max-width:700px;margin:0 auto;padding:40px;font-family:sans-serif">
<div style="display:flex;justify-content:space-between;margin-bottom:40px">
<div>
<h1 style="margin:0;font-size:24px">INVOICE</h1>
<p style="color:#666;margin:4px 0">#INV-2026-042</p>
<p style="color:#666;margin:4px 0">April 11, 2026</p>
</div>
<div style="text-align:right">
<h2 style="margin:0;font-size:18px">Your Company</h2>
<p style="color:#666;margin:4px 0">[email protected]</p>
</div>
</div>
<table style="width:100%;border-collapse:collapse;margin-bottom:30px">
<thead>
<tr style="border-bottom:2px solid #333">
<th style="text-align:left;padding:8px 0">Item</th>
<th style="text-align:right;padding:8px 0">Amount</th>
</tr>
</thead>
<tbody>
<tr style="border-bottom:1px solid #eee">
<td style="padding:8px 0">Growth Plan — April 2026</td>
<td style="text-align:right;padding:8px 0">$49.00</td>
</tr>
<tr>
<td style="padding:8px 0;font-weight:bold">Total</td>
<td style="text-align:right;padding:8px 0;font-weight:bold">$49.00</td>
</tr>
</tbody>
</table>
<p style="color:#666;font-size:14px">Payment received. Thank you!</p>
</div>
Step 2: Render to PDF
import requests
invoice_html = build_invoice(customer, line_items) # your template function
resp = requests.post(
"https://screenshotapis.org/v1/pdf",
headers={"X-Api-Key": "sk_live_your_key"},
json={
"html": invoice_html,
"format": "A4",
"print_background": True,
},
)
with open(f"invoice-{invoice_id}.pdf", "wb") as f:
f.write(resp.content)
Step 3: Email it
Attach the PDF bytes to your transactional email. Works with SendGrid, Resend, Postmark, or any email provider that accepts attachments.
Why this beats the alternatives
- vs. wkhtmltopdf — wkhtmltopdf uses an ancient WebKit engine. CSS flexbox, grid, and modern fonts render incorrectly. Screenshot API uses current Chromium.
- vs. Puppeteer/Playwright — no browser to install, no Docker config, no memory management. Works in serverless functions out of the box.
- vs. LaTeX — you already know HTML/CSS. LaTeX has a learning curve and looks like it was designed in 1985 (because it was).
- vs. ReportLab/WeasyPrint — Python-only, limited CSS support, separate layout language. HTML gives you pixel-perfect control with tools you use every day.
Tips for production invoices
- Use
print_background: trueto include colored headers and brand elements - Set explicit widths in your HTML — PDF rendering uses a fixed viewport
- Include your logo as a base64 data URI so it renders without external requests
- Test with A4 and Letter formats if you have international customers
- Cache the rendered PDF — an invoice only needs to be generated once
Generate PDF invoices from HTML — no browser required
Get your API key — free