Guides · Python · requests

Generate a fillable invoice PDF from a web page in Python with requests

The usual invoice-PDF pipeline is a template engine, a PDF library, and an afternoon of coordinate arithmetic. If your app already renders the invoice as an HTML page with input fields (PO number, notes, approver name — whatever the recipient fills in), you can skip all of it: render that page with pdf_forms and the returned invoice keeps those inputs as real, typeable PDF fields.

Below is a complete, runnable Python program using requests (pip install requests). It reads your API key from the SNAPDOK_KEY environment variable — free keys take about thirty seconds and need no card.

import os
import requests

resp = requests.post(
    "https://snapdok.io/v1/render",
    headers={"Authorization": "Bearer " + os.environ["SNAPDOK_KEY"]},
    json={
        "url": "https://your-app.com/invoices/1042/edit",
        "format": "pdf",
        "pdf_forms": True,
        "pdf_form_only": True,
    },
    timeout=90,
)
resp.raise_for_status()

with open("invoice-1042.pdf", "wb") as f:
    f.write(resp.content)
print(resp.headers.get("X-Form-Fields"), "# fields the recipient can type into")

Two practical tips from rendering real invoice pages. First, pre-filled values carry over: render the edit view of the invoice with amounts and line items already populated, and the PDF arrives pre-filled, with only the recipient's fields left blank. Second, add "pdf_form_only": true when the invoice page lives inside your app's UI — it strips the sidebar and account chrome so the customer sees an invoice, not a screenshot of your dashboard. If extraction is unsure it falls back to the full page (check X-Form-Extracted), so the worst case is cosmetic, never a missing field.

Renders are cached for 24 hours (X-Cache: HIT responses are free), which suits invoices well: re-sending the same invoice email does not burn quota. When you regenerate after an amount changes, the body differs — new URL or changed page content means a fresh render, so you never serve a stale total from cache unless the URL and page are byte-identical.

Notes for requests

Three requests-specific details the sample already handles. resp.content is the binary body — resp.text would decode PDF bytes as text and corrupt them. raise_for_status() turns a JSON error response into an exception instead of letting it reach the open(..., "wb") call. And requests has no default timeout — without the explicit timeout= a hung connection blocks forever, which matters for a call that drives a real browser and legitimately takes seconds.

Response headers worth reading

HeaderMeaning
X-Form-FieldsTypeable fields in the returned invoice.
X-Form-ExtractedWhether page chrome was stripped.
X-CacheHIT = served from the 24h cache, not charged.

Full parameter reference: the docs. Hard numbers on caps and timeouts: limits.

Related guides

Same task, other stacks

More with Python + requests

In the wild