Guides · PHP · cURL extension
Add page numbers and headers to a PDF in PHP with cURL extension
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 PHP program using cURL extension, no third-party dependency needed. It reads your API key from the SNAPDOK_KEY environment variable — free keys take about thirty seconds and need no card.
<?php
$body = json_encode([
'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>",
]);
$ch = curl_init("https://snapdok.io/v1/render");
$headers = [];
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $body,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 90,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . getenv('SNAPDOK_KEY'),
'Content-Type: application/json',
],
CURLOPT_HEADERFUNCTION => function ($ch, $line) use (&$headers) {
$parts = explode(':', $line, 2);
if (count($parts) === 2) $headers[strtolower(trim($parts[0]))] = trim($parts[1]);
return strlen($line);
},
]);
$bytes = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);
if ($status !== 200) {
throw new RuntimeException("snapdok error $status: $bytes");
}
file_put_contents('numbered.pdf', $bytes);
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 cURL extension
Plain ext-curl, no Composer needed. The details that bite: CURLOPT_RETURNTRANSFER must be set or the bytes are echoed to stdout instead of returned; the HTTP status has to be read explicitly with curl_getinfo() because cURL happily "succeeds" on a 401; and response headers need a CURLOPT_HEADERFUNCTION callback if you want to check things like X-Form-Fields. file_put_contents is binary-safe in PHP — strings are byte arrays — so no "b" flag dance is needed.
Response headers worth reading
| Header | Meaning |
|---|---|
X-Cache | Each distinct header/footer template is its own cache entry. |
X-Quota-Remaining | Metered renders left this month. |
Full parameter reference: the docs. Hard numbers on caps and timeouts: limits.
Related guides
Same task, other stacks
- Add page numbers and headers to a PDF in Python with requests
- Add page numbers and headers to a PDF in Python with httpx
- Add page numbers and headers to a PDF in Node.js with built-in fetch
- Add page numbers and headers to a PDF in Node.js with axios
- Add page numbers and headers to a PDF in Ruby with Net::HTTP
More with PHP + cURL extension
- Convert an HTML form to a fillable PDF
- Get a fillable PDF of just the form — no nav, no footer
- Generate a fillable invoice PDF from a web page
- Turn an online registration form into a fillable PDF
- Wait for a JavaScript-rendered form, then make it fillable
- Verify a fillable PDF render without opening the file
- Convert a URL to PDF
- Take a full-page screenshot
- Capture very tall pages as numbered tiles
- Take a retina-quality (2x) screenshot
- Take a JPEG screenshot at a custom viewport size
- Use the 24-hour render cache to cut costs
- Take a screenshot of a URL
- Capture a JavaScript-heavy page after it settles
- Wait for a specific element before capturing