Guides · PHP · cURL extension

Generate a fillable invoice PDF from a web page in PHP with cURL extension

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 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/invoices/1042/edit",
    'format' => "pdf",
    'pdf_forms' => true,
    'pdf_form_only' => true,
]);

$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('invoice-1042.pdf', $bytes);
echo ($headers['x-form-fields'] ?? '?') . " // fields the recipient can type into\n";

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 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

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 PHP + cURL extension

In the wild