Generate PDFs in PHP
Render a PDF from any PHP script with cURL — no framework and no DomPDF/TCPDF.
You don’t need DomPDF, TCPDF or mPDF to make a PDF in PHP. With the cURL extension you POST your HTML and data to PDFgen and get the PDF bytes back to save or stream.
This works in any PHP environment — plain scripts, WordPress, or a legacy app. The example writes the invoice to a file.
Generate a PDF in PHP
POST your HTML and data to /api/v1/generate with your API key, then save the PDF that comes back. Swap in your own HTML or a saved template_id and loop over records to render at scale.
<?php$ch = curl_init('https://pdfgen.com/api/v1/generate');curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true,CURLOPT_POST => true,CURLOPT_HTTPHEADER => ['Authorization: Bearer pdfg_live_xxx','Content-Type: application/json',],CURLOPT_POSTFIELDS => json_encode(['html' => '<h1>Invoice {{number}}</h1><p>Billed to {{client}} — total {{total}}</p>','engine' => 'handlebars','format' => 'A4','data' => ['number' => 'INV-001', 'client' => 'Acme Corp', 'total' => '$2,400.00'],]),]);$pdf = curl_exec($ch);curl_close($ch);file_put_contents('invoice.pdf', $pdf);
Convert existing HTML to PDF in PHP
Already have finished HTML? Skip templating — read an .html file and POST it with cURL and send it with engine: "legacy", which tells the API your markup is final.
<?php// Your existing, already-styled HTML file.$html = file_get_contents('invoice.html');$ch = curl_init('https://pdfgen.com/api/v1/generate');curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true,CURLOPT_POST => true,CURLOPT_HTTPHEADER => ['Authorization: Bearer pdfg_live_xxx','Content-Type: application/json',],CURLOPT_POSTFIELDS => json_encode(['html' => $html,'engine' => 'legacy','format' => 'A4',]),]);$pdf = curl_exec($ch);curl_close($ch);file_put_contents('invoice.pdf', $pdf);
How it works
- 1
Send your HTML + data
POST an HTML template (with optional Handlebars tokens) and a data object to the API.
- 2
We render the PDF
PDFgen renders it server-side — no headless browser for you to run or scale.
- 3
Get the PDF back
Receive the PDF binary (or a hosted link with export_type:"url") and serve it from PHP.
Good to know
- Uses the built-in cURL extension — no Composer package required.
- To stream to the browser instead, send a "Content-Type: application/pdf" header and echo $pdf.
- Keep your API key in an environment variable (getenv) rather than hard-coding it.
Frequently asked questions
- How do I generate a PDF in plain PHP?
- Use cURL to POST your HTML and data to /api/v1/generate, then write the returned bytes with file_put_contents or echo them with a PDF content type.
- Do I need DomPDF, TCPDF or mPDF?
- No. Rendering happens on PDFgen, so a single cURL request replaces those libraries and their CSS limitations.
- Can I use this in WordPress?
- Yes — the same cURL call works inside a plugin or theme; or use wp_remote_post() for the WordPress HTTP API.