HTML to PDF API: Convert URLs and HTML to PDF
Convert any web page or HTML template to a pixel-perfect PDF with a single API call. Custom page sizes, margins, headers, footers, and background printing. No Puppeteer or wkhtmltopdf to maintain.
HTML to PDF in One API Call
Generating a PDF from a URL requires a single POST request. Pass the target URL, your preferred page settings, and receive a binary PDF in the response body:
curl -X POST "https://api.snapapi.pics/v1/pdf" \
-H "X-Api-Key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"url": "https://example.com/invoice/123",
"page_size": "A4",
"margin_top": "15mm",
"margin_bottom": "15mm",
"margin_left": "10mm",
"margin_right": "10mm",
"print_background": true
}' \
--output invoice.pdf
The response is raw PDF binary data. Stream it directly to a file, upload to S3, attach to an email, or return it inline in an HTTP response with Content-Type: application/pdf. No base64 decoding required.
PDF from Raw HTML
For server-generated content, pass raw HTML instead of a URL. This is ideal for invoice templates, report generators, and certificate issuers where the content is assembled dynamically:
const response = await fetch('https://api.snapapi.pics/v1/pdf', {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'X-Api-Key': process.env.SNAPAPI_KEY },
body: JSON.stringify({
html: `<!DOCTYPE html>
<html><head>
<style>body { font-family: Arial, sans-serif; padding: 40px; }</style>
</head>
<body>
<h1>Invoice #${invoiceId}</h1>
<p>Due: ${dueDate}</p>
<table>...</table>
</body></html>`,
page_size: 'Letter',
print_background: true
})
});
const pdf = Buffer.from(await response.arrayBuffer());
fs.writeFileSync('invoice.pdf', pdf);
The HTML is rendered in a full Chromium browser engine, so all modern CSS is supported: flexbox, grid, custom fonts via Google Fonts or self-hosted, CSS variables, and print media queries. The print_background flag preserves background colors and images that would otherwise be stripped by Chromium's print mode.
PDF Parameters Reference
SnapAPI's PDF endpoint supports the following parameters for controlling output format and layout.
page_size accepts standard paper sizes: A4, Letter, Legal, Tabloid, and A3. Alternatively, pass width and height in millimeters to use a custom page size — useful for generating long-form reports that should be a single continuous page rather than paginated.
margin_top, margin_bottom, margin_left, and margin_right accept values in millimeters, centimeters, or inches. Default margins are 10mm on all sides. For invoice templates that extend to the page edge, set all margins to zero and control padding within the HTML itself.
print_background set to true includes background colors and background images in the PDF output. Defaults to false to match browser print behavior. Enable it for branded documents where background styling is part of the design.
header_template and footer_template accept HTML strings that are injected into the header and footer of every page. Use the special class names pageNumber, totalPages, title, and url to insert dynamic values. Font size inside header and footer templates must be set explicitly in CSS since they inherit no styles from the main page.
scale controls the rendering scale factor. Default is 1.0. Use 0.8 to shrink wide content to fit on a standard page without horizontal overflow, or 1.5 for high-density print output. landscape set to true rotates the page orientation to landscape mode — useful for wide tables or charts.
HTML to PDF Use Cases
PDF generation from HTML covers a wide range of business document automation tasks that previously required complex tooling like wkhtmltopdf, Puppeteer, or expensive commercial PDF libraries.
Invoice and Receipt Generation
Design your invoice template as a standard HTML page with CSS styling. When a payment is completed, render the template server-side with order data, pass the HTML to SnapAPI, and store the resulting PDF alongside the transaction record. The PDF is guaranteed to match what the customer sees in their browser, eliminating the layout discrepancies that plague wkhtmltopdf-based generators.
Report and Dashboard PDF Export
Analytics dashboards and reporting tools can export any view as a PDF by capturing the page URL with authentication cookies. The browser engine renders charts, SVG graphics, and interactive components into static PDF output that preserves the visual fidelity of the original. Add a print-specific CSS stylesheet to control page breaks and hide navigation elements in the PDF version.
Certificate and Contract Generation
Course completion certificates, contract documents, and compliance reports are natural fits for HTML-to-PDF generation. Build templates with placeholders for recipient name, date, and other dynamic fields, populate them server-side, and capture the result. The output is a production-quality PDF suitable for download, email attachment, or long-term archival storage.
Start generating PDFs at snapapi.pics — 200 free captures per month, no credit card required. Full API reference with parameters, examples, and response formats is at snapapi.pics/docs.html.