PDF4.dev
Quickstart

PHP

Prerequisites

1. Create a template

Go to the dashboard and click New template. Note the template ID or slug.

2. Render a PDF

render.php
<?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";
Run
php render.php

3. Stream to browser (Laravel / plain PHP)

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