# HTML to PDF in PHP (and Laravel) Convert HTML to PDF in PHP with Dompdf, mPDF or Browsershot, with working code, plus a one-call API option for plain PHP and Laravel. PHP has several ways to turn HTML into a PDF. The right one depends on how modern your CSS is. Pure-PHP libraries are easy to install but don't support flexbox or grid. Browser-based tools render like Chrome but need Chrome on the server. This guide covers both, then a third option that needs neither. **Quick answer.** Send your HTML to the html-pdf.com API and save the bytes it returns. No Chrome or PDF library is needed on your server. ```bash curl -X POST https://html-pdf.com/v1/pdf \ -H "Authorization: Bearer $HTML_PDF_KEY" \ -H "Content-Type: application/json" \ -d '{"html": "
Total: $120.00
'); $dompdf->setPaper('A4', 'portrait'); $dompdf->render(); file_put_contents('invoice.pdf', $dompdf->output()); // or stream to the browser: $dompdf->stream('invoice.pdf', ['Attachment' => false]); ``` Dompdf has no flexbox or grid, so layouts built with modern CSS collapse. Large tables can be slow and use a lot of memory, and you need to register custom fonts yourself. Only enable `isRemoteEnabled` if you trust the HTML. ## Option 2: mPDF mPDF works from PHP alone. It handles headers, footers and right-to-left or CJK text well. ```php 'A4', 'margin_top' => 15, 'margin_bottom' => 20]); $mpdf->SetHTMLFooter('Total: $120.00
'); $mpdf->Output('invoice.pdf', \Mpdf\Output\Destination::FILE); ``` Like Dompdf, it uses its own layout engine, so a page that looks right in Chrome may not look the same in the PDF. ## Option 3: Browsershot (headless Chrome) Browsershot gives you Chrome-quality PDFs. It calls Puppeteer from PHP, so the PDF matches Chrome's print output. ```bash composer require spatie/browsershot npm install puppeteer ``` ```php Invoice #042') ->format('A4') ->margins(15, 15, 15, 15) ->showBackground() ->save('invoice.pdf'); ``` The catch is running it. Your PHP servers now need Node.js, Puppeteer, a Chrome build with its system libraries, and enough memory to run a browser per request. On shared hosting that often isn't possible. ## Option 4: one API call, no Chrome on your server html-pdf.com renders your HTML in headless Chromium and returns the PDF. You get Browsershot-quality output with nothing but PHP's cURL extension. ```php