Generate PDFs with PHP

Complete PHP code examples for the PixDoc API

Install

Terminal
# Uses PHP's built-in cURL extension

Generate a PDF

Send HTML to the /api/v1/pdf endpoint and save the binary response.

PHP
$ch = curl_init("https://pixdoc.dev/api/v1/pdf");
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer YOUR_API_KEY",
        "Content-Type: application/json",
    ],
    CURLOPT_POSTFIELDS => json_encode(["html" => "<h1>Hello World</h1>"]),
    CURLOPT_RETURNTRANSFER => true,
]);
$pdf = curl_exec($ch);
file_put_contents("output.pdf", $pdf);

Take a Screenshot

The /api/v1/screenshot endpoint returns a PNG image.

PHP
$ch = curl_init("https://pixdoc.dev/api/v1/screenshot");
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer YOUR_API_KEY",
        "Content-Type: application/json",
    ],
    CURLOPT_POSTFIELDS => json_encode(["html" => "<h1>Hello World</h1>"]),
    CURLOPT_RETURNTRANSFER => true,
]);
$png = curl_exec($ch);
file_put_contents("screenshot.png", $png);

Use a Template

Pass a template_id and variables instead of raw HTML.

PHP
$ch = curl_init("https://pixdoc.dev/api/v1/pdf");
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer YOUR_API_KEY",
        "Content-Type: application/json",
    ],
    CURLOPT_POSTFIELDS => json_encode([
        "template_id" => "invoice",
        "variables" => [
            "company" => "Acme Corp",
            "total" => "$1,250.00",
        ],
    ]),
    CURLOPT_RETURNTRANSFER => true,
]);
$pdf = curl_exec($ch);
file_put_contents("invoice.pdf", $pdf);

Next Steps