PDF Generation in PHP: A Complete Overview of Tools and Libraries
Generating PDF documents on the server side is a common task in web development. From invoices and reports to certificates and books, the PDF format remains the standard for document workflow. In PHP, there are several mature libraries, each with its own strengths. In this article, we will review four main tools: FPDF, TCPDF, Dompdf, and mPDF, provide code examples, and help you choose the right solution for your project.
1. FPDF — Lightweight and Fast Library
FPDF is one of the oldest and most popular libraries for PDF generation in PHP. It requires no external dependencies, works quickly, and is ideal for simple documents: invoices, receipts, certificates.
Installation
composer require setasign/fpdfExample: Simple PDF with Text and Image
<?phprequire_once 'vendor/autoload.php';
$pdf = new FPDF();$pdf->AddPage();$pdf->SetFont('Arial', 'B', 16);$pdf->Cell(40, 10, 'Hello, World!');$pdf->Image('logo.png', 10, 20, 30);$pdf->Output();?>FPDF supports:
- Working with different fonts (TrueType, Type1)
- Creating tables, multi-line cells
- Inserting images (JPEG, PNG, GIF)
- Page numbering, headers and footers
Disadvantage: limited support for HTML and CSS — all elements must be created programmatically.
2. TCPDF — Powerful Tool for Complex Documents
TCPDF is an extended version of FPDF that adds support for Unicode, HTML tags, SVG, digital signatures, QR codes, and much more. It is excellent for creating invoices, certificates, reports with tables.
Installation
composer require tecnickcom/tcpdfExample: PDF with HTML Content and Table
<?phprequire_once 'vendor/autoload.php';
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);$pdf->AddPage();$html = '<h1>Heading</h1><table border="1"><tr><td>Cell 1</td><td>Cell 2</td></tr></table>';$pdf->writeHTML($html, true, false, true, false, '');$pdf->Output('example.pdf', 'I');?>Key features of TCPDF:
- Full Unicode (UTF-8) and Cyrillic support
- Built-in templates, page numbering
- Vector graphics (SVG, PDF forms)
- Digital signatures, encryption
- JavaScript support in PDF
3. Dompdf — Generation from HTML/CSS
Dompdf allows you to create PDF from HTML markup, processing CSS (including Flexbox, Grid, media queries). It is ideal if you already have web templates or want to quickly turn an HTML page into a PDF.
Installation
composer require dompdf/dompdfExample: PDF from HTML String
<?phpuse Dompdf\\Dompdf;
require_once 'vendor/autoload.php';
$dompdf = new Dompdf();$html = '<h2 style="color:blue;">Hello from Dompdf!</h2><p>This is a PDF generated from HTML.</p>';$dompdf->loadHtml($html);$dompdf->setPaper('A4', 'portrait');$dompdf->render();$dompdf->stream("document.pdf");?>Features of Dompdf:
- Support for CSS 2.1 and partially CSS3
- Working with images, fonts (local and Google Fonts)
- Built-in Cyrillic support when using proper fonts
- Easy integration with frameworks (Laravel, Symfony)
Important: for complex layouts with JavaScript or anima
Also in library
Image Processing in C#: Resize, Crop with ImageSharp Library
Quartz Scheduler for Java: Complete Guide with Examples
Working with Excel in JavaScript (Node.js): Libraries and Tools
JWT Tokens in Java: Complete Guide to Working with JSON Web Tokens