Use PHP and cURL to generate PDFs from Laravel, Symfony, WordPress, or plain PHP backends.
Post JSON to the API, validate the HTTP response, and save or stream the generated PDF from PHP.
The API accepts a JSON request body. Send either a public URL or inline HTML in the required html parameter. For normal synchronous conversions, the response body is the generated PDF and should be saved as binary data.
Replace <your-api-key> or the environment variable with your html2pdf.app API key, then run the example from a trusted backend environment.
This example sends a URL to the API and writes the successful binary response to document.pdf.
<?php
$payload = [
'html' => 'https://www.example.com',
];
$ch = curl_init('https://api.html2pdf.app/v1/generate');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'X-API-Key: ' . getenv('HTML2PDF_API_KEY'),
],
]);
$pdf = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
$error = curl_error($ch);
curl_close($ch);
if ($pdf === false || $statusCode < 200 || $statusCode >= 300) {
throw new RuntimeException($error ?: 'PDF generation failed');
}
file_put_contents(__DIR__ . '/document.pdf', $pdf);These examples cover the next common production flows: generating from inline HTML with options and queuing a callback job.
Return the generated PDF to the browser from server-side PHP without exposing your API key.
<?php
$payload = [
'html' => '<h1>Receipt</h1><p>Thank you for your order.</p>',
'filename' => 'receipt.pdf',
'format' => 'A4',
'marginTop' => 32,
'marginBottom' => 32,
];
$ch = curl_init('https://api.html2pdf.app/v1/generate');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'X-API-Key: ' . getenv('HTML2PDF_API_KEY'),
],
]);
$pdf = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);
if ($statusCode < 200 || $statusCode >= 300) {
http_response_code(502);
echo 'Unable to generate PDF';
exit;
}
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="receipt.pdf"');
echo $pdf;Use callBackUrl when PHP should submit the job and let another endpoint receive the PDF later.
<?php
$payload = [
'html' => 'https://www.example.com/report',
'callBackUrl' => 'https://your-app.com/webhooks/html2pdf',
'state' => 'report-1042',
];
$ch = curl_init('https://api.html2pdf.app/v1/generate');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'X-API-Key: ' . getenv('HTML2PDF_API_KEY'),
],
]);
$response = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);
if ($statusCode < 200 || $statusCode >= 300) {
throw new RuntimeException('Unable to queue PDF conversion');
}Most integrations start with html and then add options as the document design becomes more specific.
html is required and accepts a public URL or raw HTML.format, landscape, width, and height control page size.marginTop, marginRight, marginBottom, and marginLeft control whitespace around the rendered page.media selects screen or print CSS rendering.callBackUrl switches the request to asynchronous generation and sends the completed PDF to your webhook.See the full API parameter reference for every supported field.
If the generated document is blank or missing styles, first confirm that the source URL is public and that required CSS, fonts, and images are reachable by the rendering service.