DEV Community

screenshotapis
screenshotapis

Posted on

How to Convert HTML to PDF with a Simple API Call

Need to generate PDFs from HTML? Invoices, reports, certificates — whatever it is, you probably don't want to set up a headless browser just to render some HTML to a file.

Here's how to do it with a single API call.

The Hard Way

The typical approach involves:

  • Installing Puppeteer or Playwright
  • Launching a headless Chromium instance
  • Loading your HTML into a browser page
  • Calling page.pdf()
  • Managing browser lifecycle, memory, timeouts, and crashes

That's a lot of moving parts for "turn this HTML into a PDF."

The Easy Way

ScreenshotAPIs handles all of that behind a REST endpoint.

Python

import requests

html = """
<html>
<head>
  <style>
    body { font-family: Arial, sans-serif; padding: 40px; }
    h1 { color: #2563eb; }
    table { width: 100%; border-collapse: collapse; margin-top: 20px; }
    th, td { border: 1px solid #e5e7eb; padding: 10px; text-align: left; }
    th { background: #f9fafb; }
    .total { font-weight: bold; font-size: 1.2em; }
  </style>
</head>
<body>
  <h1>Invoice #1042</h1>
  <p>Date: April 2, 2026</p>
  <p>Bill to: Acme Corp</p>
  <table>
    <tr><th>Item</th><th>Qty</th><th>Price</th></tr>
    <tr><td>API Access - Growth Plan</td><td>1</td><td>$49.00</td></tr>
    <tr><td>Credit Pack (2000)</td><td>2</td><td>$58.00</td></tr>
    <tr><td colspan="2" class="total">Total</td><td class="total">$107.00</td></tr>
  </table>
</body>
</html>
"""

response = requests.post(
    "https://screenshotapis.org/v1/pdf",
    headers={"X-API-Key": "your_api_key"},
    json={"html": html, "format": "A4"}
)

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

That's it — a styled HTML invoice rendered to a clean A4 PDF.

Node.js

const html = `
<html>
<body style="font-family: Arial; padding: 40px;">
  <h1 style="color: #2563eb;">Invoice #1042</h1>
  <p>Your invoice content here...</p>
</body>
</html>`;

const response = await fetch("https://screenshotapis.org/v1/pdf", {
  method: "POST",
  headers: {
    "X-API-Key": "your_api_key",
    "Content-Type": "application/json"
  },
  body: JSON.stringify({ html, format: "A4" })
});

const buffer = await response.arrayBuffer();
require("fs").writeFileSync("invoice.pdf", Buffer.from(buffer));
Enter fullscreen mode Exit fullscreen mode

PDF Options

You can customize the output:

response = requests.post(
    "https://screenshotapis.org/v1/pdf",
    headers={"X-API-Key": "your_api_key"},
    json={
        "html": html,
        "format": "A4",        # A4, Letter, Legal, A3, Tabloid
        "landscape": True,      # Landscape orientation
        "print_background": True # Include background colors/images
    }
)
Enter fullscreen mode Exit fullscreen mode

From a URL

Don't want to pass HTML? Just pass a URL and the API will render that page to PDF:

response = requests.post(
    "https://screenshotapis.org/v1/pdf",
    headers={"X-API-Key": "your_api_key"},
    json={"url": "https://example.com", "format": "Letter"}
)

with open("page.pdf", "wb") as f:
    f.write(response.content)
Enter fullscreen mode Exit fullscreen mode

Real-World Use Cases

Invoices and receipts — Generate branded PDF invoices from your HTML template. No need for a PDF library — just style it with CSS like a web page.

Reports — Render dashboards or data tables to PDF for email digests or downloads.

Certificates — Create course completion certificates or award documents dynamically.

Contracts — Generate pre-filled agreements from templates.

Tickets — Event tickets, boarding passes, or confirmation documents.

Why Not Just Use a PDF Library?

Libraries like WeasyPrint, wkhtmltopdf, or ReportLab work — but they have trade-offs:

  • WeasyPrint — good CSS support but struggles with complex layouts and JS
  • wkhtmltopdf — deprecated, uses an old WebKit engine
  • ReportLab — low-level, you build PDFs element by element instead of using HTML/CSS

A browser-based renderer (which is what ScreenshotAPIs uses under the hood) gives you the most accurate HTML/CSS rendering because it's an actual browser.

Getting Started

  1. Sign up at screenshotapis.org — free tier, 100 renders/month
  2. Grab your API key from the dashboard
  3. Start generating PDFs

Check it out at screenshotapis.org.

Top comments (0)