HTML to PDF API – Pixel-Perfect Conversion
Convert HTML to PDF with perfect rendering. Full CSS support, custom headers and footers, no broken layouts or missing fonts.
curl -X POST "https://api.xspdf.com/v1/convert/html-to-pdf" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"html": "<html><body><h1>Invoice #1234</h1><p>Amount: $500</p></body></html>",
"options": {
"format": "A4",
"margin": {"top": "20mm", "bottom": "20mm"},
"header_html": "<div style=text-align:center>xspdf</div>"
}
}' HTML to PDF Conversion Is Painfully Hard
Broken Layouts
CSS grids break. Flexbox overflows. Images disappear. Fonts render wrong. Every library has different quirks. Your invoices look perfect in Chrome but garbage in PDFs.
Headless Browser Hell
Puppeteer crashes randomly. wkhtmltopdf is abandoned. WeasyPrint doesn't support modern CSS. Managing Chromium versions and dependencies is a full-time job.
Slow Performance
Spinning up headless Chrome takes seconds. Memory usage spikes. Concurrent requests crash servers. Your invoice generation endpoint times out during peak hours.
Hidden Cost
Developers spend weeks tweaking CSS print media queries. Support tickets flood in about broken PDFs. Customers complain about invoice formatting. Your team rewrites the same conversion logic for every new report template.
Send HTML. Get Perfect PDF.
Send HTML String
Pass raw HTML or a public URL. Use any CSS framework: Bootstrap, Tailwind, custom styles. We render it exactly as you designed.
Configure Layout
Set page size, margins, headers, footers, orientation, and scale. Add page numbers and custom watermarks. Full control over output.
Get Pixel-Perfect PDF
Receive a production-ready PDF with perfect layout, embedded fonts, sharp images, and crisp text. No broken formatting.
response = requests.post(
"https://api.xspdf.com/v1/convert/html-to-pdf",
headers={"Authorization": f"Bearer {API_KEY}"},
json={
"html": "<html><body><h1>Invoice #1234</h1></body></html>",
"options": {"format": "A4", "margin": {"top": "20mm", "bottom": "20mm"}}
}
)
pdf_url = response.json()["output_url"] Modern Web Standards, Perfect PDFs
Full CSS3 Support
Flexbox, Grid, transforms, animations, custom fonts, media queries. Modern CSS just works. No need to downgrade your HTML to legacy standards.
Perfect Image Handling
Embeds PNG, JPEG, SVG, WebP. Maintains aspect ratios. Respects max-width constraints. No blurry images or missing graphics.
Headers & Footers
Add custom HTML headers and footers to every page. Insert page numbers, dates, company names. Full CSS styling supported.
Lightning Fast
380ms median response time. No cold starts. No Chromium startup delays. Render thousands of invoices per hour without slowdowns.
Flexible Layouts
Choose A4, Letter, Legal, or custom sizes. Portrait or landscape. Set margins in mm, cm, or inches. Control page breaks with CSS.
Production Ready
99.95% uptime SLA. Multi-region infrastructure. Automatic retries. Rate limiting protection. Built for enterprise scale.
Frequently Asked Questions
What HTML/CSS features are supported?
All modern CSS3: Flexbox, Grid, custom fonts, transforms, gradients, shadows, media queries. We use a Chromium-based renderer that matches Chrome 120+ behavior. JavaScript execution is optional.
Can I use external CSS and images?
Yes. Link external stylesheets and images with full URLs. We fetch and embed them automatically. For best performance, use inline styles or base64-encoded assets.
How do I control page breaks?
Use standard CSS print media queries: page-break-before, page-break-after, page-break-inside. Set break-inside: avoid on elements to prevent splitting.
Can I add page numbers?
Yes. Use header_html or footer_html options with special variables like page, topage, and date. Example: "Page page of topage" becomes "Page 1 of 5".
What's the maximum HTML size?
Standard plans support up to 5MB of HTML input. Enterprise plans support up to 20MB. Output PDFs can be any size. Typical conversion takes 200-500ms depending on complexity.
Generate Your First PDF in Under 5 Minutes
Join 8,700+ teams using xspdf for invoice and report generation. Free API key. No credit card required.
Related APIs