Convert HTML to PDF with an API — The Developer's Guide

Published March 23, 2026 · 6 min read

Need to generate PDFs from HTML templates? Whether you're creating invoices, reports, certificates, or any document — converting HTML to PDF programmatically is one of the most common developer needs.

In this guide, we'll cover how to convert HTML to PDF using a simple REST API, with code examples in Python, JavaScript, and cURL.

Why Use an API for HTML to PDF?

You could install wkhtmltopdf or Puppeteer on your server, but there are good reasons to use an API instead:

Quick Start: HTML to PDF in 30 Seconds

Here's the simplest possible example using SnapAPI:

curl -X POST https://api.usesnapapi.com/v1/pdf \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"html": "<h1>Invoice #001</h1><p>Amount: $500</p>"}' \
  --output invoice.pdf

That's it. Send HTML, get a PDF back. No setup, no dependencies.

Get a Free API Key

curl -X POST https://api.usesnapapi.com/v1/keys \
  -H "Content-Type: application/json" \
  -d '{"email": "you@example.com"}'

Free tier includes 500 PDF generations per month — enough for most projects.

Code Examples

Python

import requests

API_KEY = "your_api_key"
html_content = """
<html>
<head>
  <style>
    body { font-family: Arial; padding: 40px; }
    .invoice-header { border-bottom: 2px solid #333; padding-bottom: 20px; }
    table { width: 100%; border-collapse: collapse; margin-top: 20px; }
    th, td { padding: 10px; text-align: left; border-bottom: 1px solid #ddd; }
  </style>
</head>
<body>
  <div class="invoice-header">
    <h1>Invoice #2026-001</h1>
    <p>Date: March 23, 2026</p>
  </div>
  <table>
    <tr><th>Item</th><th>Amount</th></tr>
    <tr><td>Web Development</td><td>€2,500</td></tr>
    <tr><td>Hosting (12 months)</td><td>€300</td></tr>
    <tr><td><strong>Total</strong></td><td><strong>€2,800</strong></td></tr>
  </table>
</body>
</html>
"""

response = requests.post(
    "https://api.usesnapapi.com/v1/pdf",
    headers={
        "Authorization": f"Bearer {API_KEY}",
        "Content-Type": "application/json"
    },
    json={
        "html": html_content,
        "format": "A4",
        "margins": {"top": "20px", "bottom": "20px", "left": "20px", "right": "20px"}
    }
)

with open("invoice.pdf", "wb") as f:
    f.write(response.content)

print("PDF generated: invoice.pdf")

JavaScript (Node.js)

const fs = require('fs');

async function generatePDF() {
  const response = await fetch('https://api.usesnapapi.com/v1/pdf', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer YOUR_API_KEY',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      html: '<h1>Monthly Report</h1><p>Generated automatically</p>',
      format: 'A4',
      landscape: false
    })
  });

  const buffer = await response.arrayBuffer();
  fs.writeFileSync('report.pdf', Buffer.from(buffer));
  console.log('PDF saved as report.pdf');
}

generatePDF();

URL to PDF

You can also convert any live webpage to PDF:

curl -X POST https://api.usesnapapi.com/v1/pdf \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com", "format": "A4", "landscape": true}' \
  --output page.pdf

PDF Options

ParameterDefaultDescription
formatA4Page size: A4, Letter, Legal, A3
landscapefalseLandscape orientation
margins20px allTop, bottom, left, right margins
delay0Wait ms before capture (for JS rendering)

Common Use Cases

  1. Invoices & receipts — Generate from your billing system templates
  2. Reports — Weekly/monthly automated reports as PDF attachments
  3. Contracts — Pre-filled agreement documents
  4. Certificates — Course completion, event attendance
  5. Proposals — Client-facing documents with dynamic data

Comparison: HTML to PDF APIs

ServiceFree TierStarting Price
SnapAPI500/month$9/month
PDFShift50/month$10/month
DocRaptor5/month$15/month
html2pdf.app100/month$14/month
Restpack100/month$29/month

Start Converting HTML to PDF for Free

500 free PDFs per month. No credit card required.

Get your free API key →

Related