Guides · PHP · cURL extension
Convert a URL to PDF in PHP with cURL extension
The baseline job: give the API a URL, get PDF bytes back. One POST, binary response, no callback dance — the body of the response is the file. Everything else on this page is about doing that reliably: auth, error handling, and not treating a JSON error as a PDF.
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",
]);
$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('page.pdf', $bytes);
The response is the document itself with Content-Type: application/pdf. Errors come back as JSON with an error code — which is why every sample here checks the status before writing bytes to disk: the classic integration bug in every language is saving {"error":"UNAUTHORIZED"...} as report.pdf and discovering it a week later.
PDF rendering uses the page's print stylesheet and reflows to paper width, so the width parameter does not apply to PDFs (it is a screenshot concept). If a page looks wrong as PDF, its @media print CSS is usually the culprit. Repeated identical requests inside 24 hours are served from cache — X-Cache: HIT — and not charged.
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 | HIT = served from the 24h cache, free. |
X-RateLimit-Remaining | Requests left in the current second. |
Full parameter reference: the docs. Hard numbers on caps and timeouts: limits.
Related guides
Same task, other stacks
- Convert a URL to PDF in Python with requests
- Convert a URL to PDF in Python with httpx
- Convert a URL to PDF in Node.js with built-in fetch
- Convert a URL to PDF in Node.js with axios
- Convert a URL to 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
- 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
- Add page numbers and headers to a PDF