POST Media Operational

HTML-to-PDF

Renders HTML/URL to pixel-perfect PDF with headers, footers, page breaks.

POST /api/html-to-pdf
<2s avg response
API Key auth
7 paper formats

What is HTML-to-PDF?

A powerful API that converts HTML content or web URLs into high-quality PDF documents. Supports custom headers, footers, page breaks, and multiple paper formats. Built on PuppeteerSharp with Chromium for accurate rendering of modern web standards including CSS Grid, Flexbox, and custom fonts.

Overview

The HTML-to-PDF API provides a simple yet powerful way to generate professional PDF documents from HTML content or live web pages. Whether you need to create invoices, generate reports, or archive web content, this API delivers pixel-perfect results with full control over layout and styling.

Key Features

  • Dual Input Modes — Convert raw HTML strings or fetch and render live URLs
  • Custom Headers & Footers — Add page numbers, dates, logos, and custom text
  • Page Break Control — CSS page-break properties for precise pagination
  • Multiple Formats — A3, A4, A5, Letter, Legal, Tabloid, and Ledger sizes
  • Margin Control — Configurable margins for professional document layouts

Code Examples

curl -X POST "https://api.atomicapis.dev/api/html-to-pdf" \
  -H "X-RapidAPI-Proxy-Secret: YOUR_SECRET" \
  -H "Content-Type: application/json" \
  -d '{
    "html": "<html><body><h1>Invoice #1234</h1><p>Total: $99.99</p></body></html>",
    "outputFormat": "base64",
    "options": {
      "format": "A4",
      "headerTemplate": "<div style=\"font-size: 10px;\">Company Name</div>",
      "footerTemplate": "<div style=\"font-size: 10px;\">Page <span class=\"pageNumber\"></span> of <span class=\"totalPages\"></span></div>",
      "marginTop": "60px",
      "marginBottom": "60px",
      "marginLeft": "40px",
      "marginRight": "40px"
    }
  }'

Request Parameters

Parameter Type Required Description
html string Conditional* Raw HTML content to convert. Required if url not provided.
url string Conditional* URL to fetch and convert. Must be an absolute HTTP(S) URL. Required if html not provided.
outputFormat string No Set to "base64" to receive a JSON response with base64-encoded PDF. When omitted, returns raw PDF binary with Content-Type: application/pdf.
options object No PDF generation options (see nested properties below).
options.format string No Paper format: A3, A4, A5, Letter, Legal, Tabloid, Ledger. Default: A4
options.landscape boolean No Generate PDF in landscape orientation. Default: false
options.printBackground boolean No Include background graphics. Default: true
options.preferCssPageSize boolean No Prefer CSS-defined page size over format. Default: false
options.scale number No Scale of the webpage rendering. Range: 0.1 to 2.0. Default: 1.0
options.marginTop string No Top margin (CSS value, e.g. "60px" or "0.4in"). Default: "0.4in"
options.marginRight string No Right margin (CSS value). Default: "0.4in"
options.marginBottom string No Bottom margin (CSS value). Default: "0.4in"
options.marginLeft string No Left margin (CSS value). Default: "0.4in"
options.headerTemplate string No HTML template for page header. Supports <span class="pageNumber"> and <span class="totalPages">
options.footerTemplate string No HTML template for page footer. Supports page number placeholders.

* Either html or url must be provided, but not both.

Response Format

Default: Raw PDF Binary

When outputFormat is omitted, the API returns the PDF file directly as binary data with Content-Type: application/pdf and a suggested filename of output.pdf.

Base64 JSON Response

When outputFormat is set to "base64", the API returns a JSON object:

200 OK - Base64 JSON Response (outputFormat: "base64")
{
  "base64": "JVBERi0xLjQKMSAwIG9iago8PCAvVHlwZSAvQ2F0YWxvZyAv...",
  "mimeType": "application/pdf",
  "sizeBytes": 245760,
  "pages": 3
}
Field Type Description
base64 string Base64-encoded PDF data
mimeType string MIME type of the response (application/pdf)
sizeBytes integer File size in bytes
pages integer Number of pages in the generated PDF

Use Cases

Invoice Generation

Generate professional invoices with company branding, itemized lists, and automatic page numbering. Perfect for e-commerce and SaaS billing.

POST /api/html-to-pdf

Report Export

Export analytics dashboards, sales reports, and data visualizations as shareable PDF documents with consistent formatting.

format: A4, landscape: true

Receipt PDFs

Create transaction receipts, booking confirmations, and payment acknowledgments with QR codes and digital signatures.

format: A5 for compact receipts

Build Constraints

Technical Stack

  • PuppeteerSharp

    .NET wrapper for Puppeteer with full Chromium control

  • Chromium Browser

    Headless Chrome for accurate rendering

  • Modern CSS Support

    CSS Grid, Flexbox, custom fonts, web fonts

Resource Limits

  • 512MB Memory

    Per-request memory allocation for rendering

  • 30s Timeout

    Maximum rendering time per request

  • 10MB HTML Limit

    Maximum HTML content size

Supported Paper Formats

A3 (297 × 420mm) A4 (210 × 297mm) A5 (148 × 210mm) Letter (8.5 × 11in) Legal (8.5 × 14in) Tabloid (11 × 17in) Ledger (17 × 11in)

Error Codes

Code Status Description Resolution
400 Bad Request Invalid request parameters or missing required fields Check request body and ensure either html or url is provided
401 Unauthorized Invalid or missing API key Include a valid X-RapidAPI-Proxy-Secret header
413 Payload Too Large HTML content exceeds 10MB limit Reduce HTML size or use URL input instead
422 Unprocessable Invalid URL or unreachable resource Verify URL is accessible and returns valid HTML
429 Rate Limited Too many requests Wait before retrying or upgrade your plan
504 Timeout Rendering exceeded 30 second limit Simplify HTML or reduce external resource dependencies

Ready to generate PDFs?

Start converting HTML to pixel-perfect PDFs in minutes. Get your API key and make your first request.