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.

Start Free — 200 captures/moView Docs

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.

PDF Headers, Footers, and Page Numbers

Professional PDF documents typically include page numbers, document titles, and date stamps in the header or footer of every page. SnapAPI supports HTML-based header and footer templates that are rendered on each page of the PDF. Pass HTML strings in the header_template and footer_template parameters:

{
  "url": "https://example.com/report",
  "page_size": "A4",
  "margin_top": "25mm",
  "margin_bottom": "25mm",
  "header_template": "<div style='font-size:10px;text-align:right;width:100%;padding-right:10mm'>Confidential</div>",
  "footer_template": "<div style='font-size:10px;text-align:center;width:100%'>Page <span class='pageNumber'></span> of <span class='totalPages'></span></div>",
  "print_background": true
}

The special class names pageNumber and totalPages are replaced with the current page number and total page count respectively. Header and footer templates use their own rendering context — styles from the main document do not apply. Set font size, color, and layout explicitly within the template strings. Increase margin_top and margin_bottom to match the height of your header and footer to prevent content from overlapping.

Page Break Control with CSS

Control how content splits across PDF pages using CSS print properties. Add page-break-before: always to force a new page before a section, or page-break-inside: avoid to prevent a table row or code block from being split mid-page:

<style>
  @media print {
    .section { page-break-before: always; }
    table, pre, .invoice-item { page-break-inside: avoid; }
    .no-print { display: none; }
  }
</style>

The @media print block applies only when Chromium renders the page in print mode for PDF output. Use it to hide navigation menus, sticky headers, chat widgets, and other elements that should not appear in the PDF version of a page. This lets you maintain a single HTML template that renders correctly both in the browser and in PDF output.

HTML to PDF vs wkhtmltopdf, Puppeteer, and WeasyPrint

Teams evaluating PDF generation tools typically compare several options. Each has different trade-offs around setup complexity, CSS fidelity, and operational overhead.

wkhtmltopdf uses an old WebKit rendering engine that does not support modern CSS — flexbox, grid, and CSS custom properties are unreliable or unsupported. It is also difficult to install in containerised environments because it requires X11 dependencies. Maintenance has effectively stalled, making it unsuitable for new projects.

Puppeteer or Playwright in-process gives you full Chromium rendering and modern CSS support, but requires you to run and maintain browser binaries in your deployment. Memory usage per browser instance is high (200-400 MB), and Chromium version management adds complexity to your CI/CD pipeline. For teams that need PDF generation at scale, the infrastructure cost can be significant.

WeasyPrint is a Python-based PDF generator with good CSS support, but it does not execute JavaScript, making it unsuitable for pages that load content dynamically or rely on client-side rendering.

SnapAPI uses a managed Chromium fleet — full modern CSS support, JavaScript execution, and no infrastructure to run. You call an HTTP endpoint and receive a PDF. Pricing is predictable per-capture, with no compute cost for idle capacity. For teams generating PDFs on-demand rather than continuously, this model is significantly cheaper than running your own browser pool.

Start generating PDFs at snapapi.pics — 200 free captures per month, no credit card required. The PDF endpoint is available on all plans and accepts both URL and raw HTML input.

Serving PDFs from a Web Application

When a user requests a PDF download, generate it on the fly and stream it directly in the HTTP response. Set the response headers to trigger a browser download rather than inline display. In Express.js, this looks like: set Content-Type to application/pdf and Content-Disposition to attachment; filename=report.pdf. Pipe the SnapAPI response body directly to the client response to avoid buffering the entire PDF in server memory. This pattern works efficiently for PDFs up to several hundred pages without requiring temporary file storage on the server.

For cached PDFs — invoices that do not change after generation — store them in S3 on first request and serve directly from CloudFront on subsequent requests. This eliminates redundant API calls for the same document and dramatically reduces latency for repeat downloads.