PDF Generation API: Convert HTML to PDF with a Single API Call
Generate pixel-perfect PDFs from HTML and CSS using a simple REST API. No wkhtmltopdf, no Puppeteer, no server-side rendering headaches.
Generating PDFs programmatically is one of those tasks that sounds simple until you actually try it. You need to render HTML, handle CSS layout, deal with page breaks, embed fonts, and produce a file that looks the same on every device. The library ecosystem is fragmented — wkhtmltopdf is abandoned, Puppeteer is heavyweight, and most PDF libraries produce ugly output from raw drawing commands.
A PDF generation API lets you skip all of that. Send HTML, get a PDF. The rendering engine runs server-side, handles the edge cases, and returns a production-ready document.
The Problem with Local PDF Generation
Developers typically reach for one of these approaches — and each has real drawbacks:
- wkhtmltopdf — uses an old WebKit engine, struggles with modern CSS (flexbox, grid), and the project is effectively unmaintained
- Puppeteer / Playwright — excellent rendering quality, but you're managing a full headless browser just to print a page
- PDFKit / jsPDF — low-level libraries that require you to manually position every element. No HTML/CSS support at all
- WeasyPrint — Python-only, requires system-level dependencies (Cairo, Pango), and breaks on some CSS properties
An API sidesteps these trade-offs entirely. You write standard HTML and CSS. The API renders it with a modern browser engine and returns a PDF.
Generating a PDF in One Request
The API Snap PDF endpoint accepts HTML content and returns a PDF file:
curl -X POST "https://api-snap.com/api/pdf" \
-H "Authorization: Bearer snp_your_api_key" \
-H "Content-Type: application/json" \
-d '{
"html": "<h1>Invoice #1042</h1><p>Amount: $250.00</p><p>Due: 2026-04-01</p>",
"format": "A4",
"margin": "20mm"
}' \
-o invoice.pdfThe output is a properly formatted A4 PDF with 20mm margins. You can use any HTML and CSS you want — the rendering engine supports modern layout properties including flexbox and grid.
Parameters
html— the HTML content to render (full documents or fragments)format— page size:A4,Letter,Legal, or custom dimensionsmargin— page margins (e.g.,20mm,1in)landscape— set totruefor landscape orientation
Real-World Use Cases
Invoices and Receipts
Build your invoice template in HTML and CSS — the same tools your frontend team already knows. Pass the rendered HTML to the API with dynamic data injected, and you get a professional PDF ready to email or store. No LaTeX, no report builder, no XML templates.
Reports and Dashboards
Export data visualizations, tables, and charts as PDF reports. Render your report page server-side, capture the HTML, and convert it. The API handles page breaks, headers, footers, and multi-page layouts.
Contracts and Legal Documents
Generate pre-filled contracts from templates. Inject signer names, dates, and terms into an HTML template, convert to PDF, and send for signature. The consistent rendering ensures the document looks identical regardless of the recipient's device.
Shipping Labels and Tickets
Create custom-sized PDFs for labels, tickets, or badges. Set the page format to match your physical media dimensions and the API outputs a print-ready file.
Integration Example: Python
import requests
html_content = """
<html>
<head><style>body { font-family: Helvetica; } table { width: 100%; border-collapse: collapse; } td, th { border: 1px solid #ddd; padding: 8px; }</style></head>
<body>
<h1>Monthly Report</h1>
<table>
<tr><th>Metric</th><th>Value</th></tr>
<tr><td>Revenue</td><td>$42,000</td></tr>
<tr><td>Users</td><td>1,250</td></tr>
</table>
</body>
</html>
"""
response = requests.post(
"https://api-snap.com/api/pdf",
headers={"Authorization": "Bearer snp_your_api_key"},
json={"html": html_content, "format": "A4", "margin": "15mm"}
)
with open("report.pdf", "wb") as f:
f.write(response.content)Tips for Better PDFs
- Use
page-break-beforeandpage-break-afterCSS properties to control where pages split - Inline your CSS or include it in a
<style>tag — external stylesheets won't be fetched - Use absolute URLs for images so the rendering engine can fetch them
- Test with
@media printstyles to fine-tune the printed output
Start generating PDFs now — create a free API Snap account and try the PDF endpoint in the playground.