FrankenPHP: A Modern Application Server for PHP
FrankenPHP has been getting a lot of attention in the PHP world, so I want to give a short, practical overview of what it is and why it might matter if you…
Sooner or later most PHP projects need to produce a PDF: an invoice, a report, a ticket or a contract. There are several mature open-source libraries for this, and they work in quite different ways. Here is how I see the main options and when I would pick each one.
PHP PDF libraries fall roughly into two groups:
There is also a third option: driving a headless browser, which gives the best CSS support but needs extra software on the server.
Dompdf is a pure-PHP HTML-to-PDF converter and probably the most common choice for simple documents like invoices.
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->loadHtml('<h1>Invoice #1001</h1>');
$dompdf->setPaper('A4');
$dompdf->render();
$dompdf->stream('invoice.pdf');
mPDF is also an HTML-to-PDF library written in PHP, and it shines with multilingual content.
$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('<h1>Report</h1>');
$mpdf->Output('report.pdf', 'D');
If your documents contain Persian, Arabic or other non-Latin text, mPDF is the one I would try first.
TCPDF is one of the oldest and most feature-rich PHP PDF libraries.
FPDF on GitHub · FPDI on GitHub
FPDF is a small, lightweight library for drawing PDFs by hand. It has no HTML support, but it is fast and simple. FPDI adds the ability to import pages from existing PDFs, which is useful for filling in a template or stamping a document.
Browsershot from Spatie uses Puppeteer and headless Chrome to render a page and save it as PDF.
If you use Laravel, there are convenient wrappers: laravel-dompdf for Dompdf and spatie/laravel-pdf, which renders Blade views to PDF.
Start with the hosting you have. If you cannot install Node.js or Chrome, stick with a pure-PHP library. Then look at your content: if your layout is simple, Dompdf is the quickest win; if you need non-Latin text, go with mPDF; if you need specialized PDF features, TCPDF covers the most ground.
FrankenPHP has been getting a lot of attention in the PHP world, so I want to give a short, practical overview of what it is and why it might matter if you…
No comments yet