Guides · Python · httpx

Add page numbers and headers to a PDF in Python with httpx

A multi-page PDF that leaves the printer without page numbers gets shuffled exactly once before someone asks for them. pdf_footer (and its twin pdf_header) take a small HTML template that Chromium prints on every page of the PDF — and inside it, <span class="pageNumber"></span> and friends are substituted with live values at print time.

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

import os
import httpx

resp = httpx.post(
    "https://snapdok.io/v1/render",
    headers={"Authorization": "Bearer " + os.environ["SNAPDOK_KEY"]},
    json={
        "url": "https://your-app.com/report/42",
        "format": "pdf",
        "pdf_footer": "<div style=\"font-size:10px;width:100%;text-align:center;color:#555;\">Page <span class=\"pageNumber\"></span> of <span class=\"totalPages\"></span></div>",
    },
    timeout=90,
)
resp.raise_for_status()

with open("numbered.pdf", "wb") as f:
    f.write(resp.content)

Five class names do the substitution: pageNumber, totalPages, date, title (the page's <title>) and url. Two styling rules save an hour of confusion: set an explicit font-size, because the print engine's default is unreadably small — a template that "does not show up" is almost always just tiny — and set width:100% on the wrapper if you want centering or space-between layouts to work.

Setting a header or footer reserves a 60px margin on that edge for it; a request with neither keeps the exact zero-margin output PDFs have always had, so adding a footer to one report cannot shift the layout of any other. The templates are ignored for png/jpeg, and also when pdf_forms is on — the fillable-forms pipeline measures field positions against a zero-margin page, and a margin would misplace every field. Both templates change the cache fingerprint, so a numbered and an unnumbered render of the same URL are separate cache entries.

Same endpoint, one more trick: if the page you are rendering has a form on it, adding "pdf_forms": true to a PDF render brings it back with real, fillable AcroForm fields — a PDF people can type into, not a picture of one. How that works.

Notes for httpx

httpx looks like requests but differs where it bites: it ships a 5-second default timeout, which a render that drives a real browser will blow through on heavier pages — hence the explicit timeout=90. raise_for_status() exists and behaves the same. If you are on asyncio already, the same call works with httpx.AsyncClient and await client.post(...) — the request body and headers are identical.

Response headers worth reading

HeaderMeaning
X-CacheEach distinct header/footer template is its own cache entry.
X-Quota-RemainingMetered renders left this month.

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

Related guides

Same task, other stacks

More with Python + httpx