DEV Community

ShotaTanikawa
ShotaTanikawa

Posted on

Convert Any HTML or URL to PDF with a Simple API Call

Convert Any HTML or URL to PDF with a Simple API Call

Need to generate PDFs from your web app? Invoices, reports, receipts, or just saving a webpage as PDF? The HTML to PDF Converter API does it in one API call — no Puppeteer setup, no server management, no headless browser configuration.

Quick Start

Convert HTML to PDF

curl -X POST https://pdf-api-mu.vercel.app/api/convert \
  -H "Content-Type: application/json" \
  -d '{
    "html": "<h1>Invoice #1234</h1><p>Amount: $49.99</p><p>Date: 2026-03-20</p>",
    "format": "A4",
    "margin": {"top": "25mm", "right": "20mm", "bottom": "25mm", "left": "20mm"}
  }' \
  --output invoice.pdf
Enter fullscreen mode Exit fullscreen mode

Convert a URL to PDF

curl -X POST https://pdf-api-mu.vercel.app/api/convert \
  -H "Content-Type: application/json" \
  -d '{"url": "https://example.com"}' \
  --output page.pdf
Enter fullscreen mode Exit fullscreen mode

That's it. You get a production-quality PDF back.

Options

Parameter Type Default Description
html string Raw HTML to convert
url string URL to convert
format string "A4" A0-A6, Letter, Legal, Tabloid, Ledger
landscape boolean false Landscape orientation
margin object 20mm all Top, right, bottom, left margins
printBackground boolean true Include background colors/images

Provide either html or url, not both.

Python Example

import requests

# Generate an invoice PDF
response = requests.post(
    "https://pdf-api-mu.vercel.app/api/convert",
    json={
        "html": """
        <style>
            body { font-family: Arial, sans-serif; }
            .header { border-bottom: 2px solid #333; padding-bottom: 10px; }
            .total { font-size: 24px; font-weight: bold; margin-top: 20px; }
        </style>
        <div class="header">
            <h1>Invoice #2024-001</h1>
            <p>Date: March 20, 2026</p>
        </div>
        <table>
            <tr><td>Web Development</td><td>$2,000</td></tr>
            <tr><td>API Integration</td><td>$500</td></tr>
        </table>
        <div class="total">Total: $2,500</div>
        """,
        "format": "A4",
    },
)

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

print("Invoice saved!")
Enter fullscreen mode Exit fullscreen mode

Node.js Example

const response = await fetch("https://pdf-api-mu.vercel.app/api/convert", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({
    url: "https://github.com/ShotaTanikawa",
    format: "A4",
    printBackground: true,
  }),
});

const buffer = await response.arrayBuffer();
const fs = await import("fs");
fs.writeFileSync("github-profile.pdf", Buffer.from(buffer));
console.log("PDF saved!");
Enter fullscreen mode Exit fullscreen mode

Use Cases

  • Invoice generation: HTML template → professional PDF invoice
  • Report exports: Dashboard data → downloadable PDF report
  • Receipt generation: Order confirmation → PDF receipt for email
  • Web archiving: Save any webpage as a PDF document
  • Resume/CV: HTML resume → PDF for job applications
  • Legal documents: Terms of service → formatted PDF

Why Not Just Use Puppeteer Directly?

You could. But then you need:

  • A server to run headless Chromium (1GB+ RAM)
  • Chromium binary management and updates
  • Timeout handling, memory leak prevention
  • Scaling for concurrent requests

Or you can make one API call and get a PDF back. Your choice.

Try It

Available on RapidAPI with a free tier (100 PDFs/month).


Built by @ShotaTanikawa. Feedback welcome!

Top comments (0)