Generate a PDF in PHP
The simplest way to generate a PDF in PHP is to call the Docweave API with cURL or file_get_contents() — no headless browser to install, configure, or keep running on your server.
Generate a PDF in PHP with cURL
POST HTML to /api/v1/pdf using PHP's built-in cURL extension, then write the response body to disk:
<?php
$ch = curl_init("https://docweave.dev/api/v1/pdf");
$payload = [
"source" => [
"type" => "html",
"html" => "<h1>Hello from PHP</h1><p>Rendered by Docweave.</p>",
],
"options" => ["format" => "A4"],
"idempotencyKey" => "php-example-001",
];
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => [
"Authorization: Bearer dw_live_your_key",
"Content-Type: application/json",
"Accept: application/pdf",
],
CURLOPT_POSTFIELDS => json_encode($payload),
]);
$pdfBytes = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($statusCode !== 200 || $pdfBytes === false) {
throw new RuntimeException("Docweave request failed with status {$statusCode}");
}
file_put_contents(__DIR__ . "/hello.pdf", $pdfBytes);
echo "Saved hello.pdf" . PHP_EOL;
Alternative: file_get_contents() with a stream context
If your project avoids the cURL extension, the standard file_get_contents() HTTP client works the same way once you attach a stream context with the POST method, headers, and body:
<?php
$payload = json_encode([
"source" => [
"type" => "html",
"html" => "<h1>Hello from PHP</h1><p>Rendered by Docweave.</p>",
],
"options" => ["format" => "A4"],
"idempotencyKey" => "php-example-002",
]);
$context = stream_context_create([
"http" => [
"method" => "POST",
"header" => implode("\r\n", [
"Authorization: Bearer dw_live_your_key",
"Content-Type: application/json",
"Accept: application/pdf",
]),
"content" => $payload,
"ignore_errors" => true,
],
]);
$pdfBytes = file_get_contents("https://docweave.dev/api/v1/pdf", false, $context);
$statusLine = $http_response_header[0] ?? "";
if ($pdfBytes === false || strpos($statusLine, "200") === false) {
throw new RuntimeException("Docweave request failed: {$statusLine}");
}
file_put_contents(__DIR__ . "/hello.pdf", $pdfBytes);
echo "Saved hello.pdf" . PHP_EOL;
…or from an AI agent
The same request works without any PHP code at all. Run npx @docweave/mcp and an AI agent gets a generate_pdf tool that wraps this same orchestration:
// From an AI agent over MCP — no HTTP client, no PHP required:
generate_pdf({
source: {
type: "html",
html: "<h1>Hello from PHP</h1><p>Rendered by Docweave.</p>"
},
outputPath: "/tmp/hello.pdf"
})FAQ
How do I generate a PDF in PHP without installing a headless browser?
Call the Docweave API instead of running Chromium/wkhtmltopdf yourself. POST your HTML to https://docweave.dev/api/v1/pdf with cURL or file_get_contents() and a stream context, and the response body is the finished PDF — no browser binary to install or maintain on your server.
Can I convert an HTML string to PDF in PHP with cURL?
Yes. Build a JSON payload with a source of type "html" containing your markup, set the Authorization: Bearer header with your API key, and POST it with curl_init()/curl_exec(). The raw response body is PDF bytes you can write with file_put_contents().
Does file_get_contents() work for calling a PDF API in PHP, or do I need cURL?
file_get_contents() works fine for this — wrap the request in a stream_context_create() call that sets the method to POST, adds the Authorization and Content-Type headers, and passes the JSON body as content. No extra PHP extensions are required.
How is PHP PDF generation billed with Docweave?
Per document, not per page. A single POST from your PHP app that renders a one-page receipt or a fifty-page report is billed the same, and passing an idempotencyKey means a retried request never generates or bills the same document twice.
Ready to generate PDFs from your PHP app?
Get an API key