Quickstart
PHP
Prerequisites
- A PDF4.dev account — sign up free
- An API key from Settings
- PHP 8.1+ with cURL enabled
1. Create a template
Go to the dashboard and click New template. Note the template ID or slug.
2. Render a PDF
<?php
$payload = json_encode([
'template_id' => 'invoice',
'data' => [
'company_name' => 'Acme Corp',
'invoice_number' => 'INV-2025-001',
'total' => '$4,500.00',
],
]);
$ch = curl_init('https://pdf4.dev/api/v1/render');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer p4_live_your_key_here',
'Content-Type: application/json',
],
]);
$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($status !== 200) {
$error = json_decode($response, true);
throw new \RuntimeException($error['error']['message']);
}
file_put_contents('invoice.pdf', $response);
echo "PDF saved to invoice.pdf\n";php render.php3. Stream to browser (Laravel / plain PHP)
public function downloadInvoice(): \Symfony\Component\HttpFoundation\Response
{
$response = Http::withToken(config('services.pdf4.key'))
->post('https://pdf4.dev/api/v1/render', [
'template_id' => 'invoice',
'data' => ['company_name' => 'Acme Corp', 'total' => '$4,500'],
]);
return response($response->body(), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="invoice.pdf"',
]);
}Next steps
- PDF format — page size, margins, custom dimensions
- API reference — full render endpoint docs