HTML to PDF API: A Developer's Guide to Generating PDFs Programmatically
Learn how to use the SublimePDF API to generate pixel-perfect PDFs from HTML, CSS, and templates. Code examples and best practices.
HTML to PDF API: A Developer's Guide to Generating PDFs Programmatically
Generating PDFs from HTML is one of the most common requirements in web development. Invoices, reports, certificates, contracts — if you can build it with HTML and CSS, you can turn it into a beautiful PDF with the SublimePDF API.
Why HTML to PDF?
HTML and CSS are the most flexible and widely-known layout technologies in the world. By using them to design your PDFs, you get:
- Full CSS3 support: Flexbox, Grid, custom properties, media queries
- Web fonts: Google Fonts, Adobe Fonts, or your own custom fonts
- Dynamic content: Inject data from your database into templates
- Version control: Store your templates in Git alongside your code
- Design tools: Use any web development tool to design your PDFs
Quick Start
1. Get Your API Key
Sign up at SublimePDF and generate an API key from your dashboard.
2. Make Your First Request
curl -X POST https://api.sublimepdf.com/v1/html-to-pdf \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"html": "<h1>Hello, World!</h1><p>My first PDF</p>",
"options": {
"format": "A4",
"margin": { "top": "20mm", "bottom": "20mm" }
}
}'
3. Use Templates
curl -X POST https://api.sublimepdf.com/v1/templates/invoice/generate \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"data": {
"invoiceNumber": "INV-2024-001",
"company": "Acme Corp",
"items": [
{ "name": "Widget A", "qty": 5, "price": 29.99 },
{ "name": "Widget B", "qty": 2, "price": 49.99 }
],
"total": 249.93
}
}'
Node.js Integration
const response = await fetch('https://api.sublimepdf.com/v1/html-to-pdf', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.SUBLIMEPDF_API_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
html: invoiceHTML,
options: { format: 'A4', printBackground: true }
}),
});
const pdfBuffer = await response.arrayBuffer();
Supported Features
| Feature | Support | |---------|---------| | CSS Flexbox | ✅ Full | | CSS Grid | ✅ Full | | Web Fonts | ✅ Full | | SVG | ✅ Full | | Images | ✅ Full | | Page breaks | ✅ Full | | Headers/Footers | ✅ Full | | Custom page sizes | ✅ Full | | Landscape/Portrait | ✅ Full | | JavaScript rendering | ✅ Full |
Best Practices
- Use semantic HTML for better structure and accessibility.
- Inline critical CSS for faster rendering and to avoid CORS issues.
- Use
@pageCSS rules for page-specific styling (margins, size, orientation). - Test with print media queries — add
@media printstyles for PDF-specific adjustments. - Use absolute URLs for images to ensure they resolve correctly.
Pricing
| Plan | API Calls/Month | Price | |------|----------------|-------| | Free | 100 | $0 | | Starter | 1,000 | $29/month | | Pro | 10,000 | $99/month | | Enterprise | Unlimited | Custom |
See full details on our Pricing page.
Conclusion
The SublimePDF API makes PDF generation simple and reliable. Design your documents with HTML and CSS — the tools you already know — and let our rendering engine handle the rest. Check our API documentation for complete reference and more examples.