Inkspan › Guides › How to generate a PDF from HTML in PHP

How to generate a PDF from HTML in PHP

Call the Inkspan /v1/generate/html endpoint from PHP to generate a PDF from HTML — no headless browser, font stack, or rendering infrastructure in your deploy. Keep your API key in the environment and POST.

Set CURLOPT_RETURNTRANSFER so the body comes back as a string rather than printing. Always check the HTTP status before writing the file.

PHP example

<?php
$ch = curl_init('https://api.inkspan.dev/v1/generate/html');
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        'X-API-Key: ' . getenv('INKSPAN_KEY'),
        'Content-Type: application/json',
    ],
    CURLOPT_POSTFIELDS => '{"html": "<h1>Invoice</h1>"}',
]);

$body = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code !== 200) { throw new Exception($body); }
file_put_contents('out.pdf', $body);

Request parameters

NameTypeRequiredDescription
htmlstringyesThe HTML document or fragment to render.
filenamestringnoFilename in the response's Content-Disposition. Defaults to document.pdf.

Response

Returns a PDF as a binary stream with Content-Type: application/pdf and Content-Disposition: attachment; filename=document.pdf. Write the response body straight to disk — do not parse it as JSON.

Production notes

Read the key with getenv() rather than hardcoding it, and keep it out of version control.

Errors

StatusMeaningFix
401Invalid or missing API keySend your key in the X-API-Key header.
402Monthly quota exceededThe response reports used/quota. Upgrade at inkspan.dev/#pricing.
422Missing or invalid parameterCheck required fields in the table above — the body names the offending field.

Failed requests do not count against your quota; only successful ones are metered.

Get a free API key →

Same operation in cURL · Python · Node.js · Other frameworks: Next.js · Django · Laravel · Ruby on Rails · All guides