Need to generate PDFs from HTML templates? Whether you're creating invoices, reports, certificates, or any document — converting HTML to PDF programmatically is one of the most common developer needs.
In this guide, we'll cover how to convert HTML to PDF using a simple REST API, with code examples in Python, JavaScript, and cURL.
You could install wkhtmltopdf or Puppeteer on your server, but there are good reasons to use an API instead:
Here's the simplest possible example using SnapAPI:
curl -X POST https://api.usesnapapi.com/v1/pdf \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"html": "<h1>Invoice #001</h1><p>Amount: $500</p>"}' \
--output invoice.pdf
That's it. Send HTML, get a PDF back. No setup, no dependencies.
curl -X POST https://api.usesnapapi.com/v1/keys \
-H "Content-Type: application/json" \
-d '{"email": "you@example.com"}'
Free tier includes 500 PDF generations per month — enough for most projects.
import requests
API_KEY = "your_api_key"
html_content = """
<html>
<head>
<style>
body { font-family: Arial; padding: 40px; }
.invoice-header { border-bottom: 2px solid #333; padding-bottom: 20px; }
table { width: 100%; border-collapse: collapse; margin-top: 20px; }
th, td { padding: 10px; text-align: left; border-bottom: 1px solid #ddd; }
</style>
</head>
<body>
<div class="invoice-header">
<h1>Invoice #2026-001</h1>
<p>Date: March 23, 2026</p>
</div>
<table>
<tr><th>Item</th><th>Amount</th></tr>
<tr><td>Web Development</td><td>€2,500</td></tr>
<tr><td>Hosting (12 months)</td><td>€300</td></tr>
<tr><td><strong>Total</strong></td><td><strong>€2,800</strong></td></tr>
</table>
</body>
</html>
"""
response = requests.post(
"https://api.usesnapapi.com/v1/pdf",
headers={
"Authorization": f"Bearer {API_KEY}",
"Content-Type": "application/json"
},
json={
"html": html_content,
"format": "A4",
"margins": {"top": "20px", "bottom": "20px", "left": "20px", "right": "20px"}
}
)
with open("invoice.pdf", "wb") as f:
f.write(response.content)
print("PDF generated: invoice.pdf")
const fs = require('fs');
async function generatePDF() {
const response = await fetch('https://api.usesnapapi.com/v1/pdf', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
html: '<h1>Monthly Report</h1><p>Generated automatically</p>',
format: 'A4',
landscape: false
})
});
const buffer = await response.arrayBuffer();
fs.writeFileSync('report.pdf', Buffer.from(buffer));
console.log('PDF saved as report.pdf');
}
generatePDF();
You can also convert any live webpage to PDF:
curl -X POST https://api.usesnapapi.com/v1/pdf \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"url": "https://example.com", "format": "A4", "landscape": true}' \
--output page.pdf
| Parameter | Default | Description |
|---|---|---|
| format | A4 | Page size: A4, Letter, Legal, A3 |
| landscape | false | Landscape orientation |
| margins | 20px all | Top, bottom, left, right margins |
| delay | 0 | Wait ms before capture (for JS rendering) |
| Service | Free Tier | Starting Price |
|---|---|---|
| SnapAPI | 500/month | $9/month |
| PDFShift | 50/month | $10/month |
| DocRaptor | 5/month | $15/month |
| html2pdf.app | 100/month | $14/month |
| Restpack | 100/month | $29/month |
500 free PDFs per month. No credit card required.