RenderPDFs/Tutorials/Python
📘 Quickstart · 5 minutes

Generate PDFs in Python

Generate PDFs from HTML, Markdown, URLs or templates in Python — using the requests library and the RenderPDFs REST API.

1. Install

RenderPDFs uses a plain REST API — no SDK required. For Python, install the dependency below and grab your API key from renderpdfs.com/signup (free, 100 PDFs/month, no credit card).

Installbash
pip install requests
# or
uv pip install requests

2. Convert HTML to PDF

The simplest case: send an HTML string, get back a PDF binary. Anything Chromium renders works — Flexbox, Grid, web fonts, SVG, JavaScript.

HTML → PDFpython
import os
import requests

API_KEY = os.environ["RENDERPDFS_KEY"]

response = requests.post(
    "https://api.renderpdfs.com/v1/generate",
    headers={"X-API-Key": API_KEY, "Content-Type": "application/json"},
    json={"html": "<h1>Hello PDF</h1><p>Generated from Python</p>"},
    timeout=60,
)
response.raise_for_status()

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

3. Convert a URL to PDF

Pass url instead of html and RenderPDFs fetches the page, waits for JS, and snapshots it. Useful for archiving dashboards, public pages, or invoices served from your app.

URL → PDFpython
response = requests.post(
    "https://api.renderpdfs.com/v1/generate",
    headers={"X-API-Key": API_KEY},
    json={
        "url": "https://example.com",
        "options": {"format": "A4", "printBackground": True},
    },
)

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

4. Use a built-in template

Skip the design work. RenderPDFs ships six battle-tested templates — invoice, receipt, report, contract, certificate, offer_letter. Send JSON, get a styled PDF.

Built-in invoice templatepython
response = requests.post(
    "https://api.renderpdfs.com/v1/generate",
    headers={"X-API-Key": API_KEY},
    json={
        "template": "invoice",
        "data": {
            "company": "Acme Inc.",
            "invoice_number": "INV-001",
            "items": [
                {"description": "Consulting", "quantity": 10, "unit_price": 120},
            ],
        },
    },
)

5. Custom paper, margins, headers

Control page format, orientation, margins, and running headers/footers via the options object. All standard Chromium PDF settings are supported.

Custom paper size & marginspython
response = requests.post(
    "https://api.renderpdfs.com/v1/generate",
    headers={"X-API-Key": API_KEY},
    json={
        "html": "<h1>Report</h1>",
        "options": {
            "format": "Letter",
            "landscape": False,
            "margin": {"top": "20mm", "bottom": "20mm", "left": "15mm", "right": "15mm"},
            "printBackground": True,
        },
    },
)

6. Store the PDF and share a link

For emailable links or webhook payloads, set store: true in the body. The response becomes { url, expires_in } — the PDF is hosted on our CDN for 24 hours by default.

Store the PDF and get a URLpython
response = requests.post(
    "https://api.renderpdfs.com/v1/generate",
    headers={"X-API-Key": API_KEY},
    json={"html": "<h1>Shared report</h1>", "store": True},
)
result = response.json()
print(f"Download at {result['url']} (expires in {result['expires_in']}s)")

7. Notes & gotchas

Authentication

Every request needs an X-API-Key header. Grab a free key at renderpdfs.com/signup — 100 PDFs/month, no credit card. Treat the key like a password: keep it server-side, never expose it in browser code.

Response format

By default the endpoint streams back the raw PDF binary (Content-Type: application/pdf). Set `store: true` in the body and the response becomes { url, expires_in } — useful for emailing links or attaching to webhooks.

Handling errors

Non-2xx responses return JSON: { error: string }. The most common cases are 401 (bad API key), 402 (over plan quota), and 422 (invalid HTML or URL). Always parse the error body before retrying.

Other languages

Generate your first PDF in 60 seconds

100 free PDFs per month. No credit card. HTML, Markdown, URLs, templates, MCP — all included.