PDF Generation in PHP: Library Overview and Practical Examples
Creating PDF documents is one of the common tasks in web development. From invoices and reports to certificates and books, PDF remains a universal format for document exchange. In PHP, there are several powerful libraries that allow you to generate PDFs both from HTML and programmatically. In this article, we will cover four popular tools: TCPDF, FPDF, Dompdf, and mPDF.
1. FPDF — Lightweight and Fast Classic
FPDF is one of the oldest and simplest libraries. It does not require additional extensions (except GD). The main advantage is minimalism and speed. FPDF works directly with coordinates, giving you full control over the placement of elements.
Example of creating a simple PDF:
<?phprequire('fpdf.php');
$pdf = new FPDF();$pdf->AddPage();$pdf->SetFont('Arial', 'B', 16);$pdf->Cell(40, 10, 'Hello, World!');$pdf->Output();?>Pros:
- Very small size (about 100 KB)
- No external dependencies required
- UTF-8 support via separate fonts
Cons:
- Complex work with HTML
- Limited CSS support
2. TCPDF — Powerful and Feature-Rich
TCPDF is an extended version of FPDF with support for UTF-8, HTML, CSS, images, QR codes, digital signatures, and much more. It is the standard choice for complex documents.
Example with HTML:
<?phprequire_once('tcpdf/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);$pdf->AddPage();$pdf->writeHTML('<h1>Heading</h1><p>This is a paragraph with <b>bold</b> text.</p>');$pdf->Output('example.pdf', 'I');?>Features:
- Full Unicode and RTL support (Arabic, Hebrew)
- Built-in fonts (DejaVu, FreeSerif)
- Support for SVG, JavaScript, forms
3. Dompdf — Generation from HTML/CSS
Dompdf converts HTML and CSS into PDF. This is an ideal choice if you already have a page layout or template. Dompdf supports most CSS properties, but not all (for example, Flexbox and Grid may work incorrectly).
Example:
<?phprequire 'vendor/autoload.php';
use Dompdf\\Dompdf;
$dompdf = new Dompdf();$html = '<h2 style="color:red;">Red Heading</h2><p>Text with margins.</p>';$dompdf->loadHtml($html);$dompdf->setPaper('A4', 'portrait');$dompdf->render();$dompdf->stream("document.pdf");?>Pros:
- Simplicity: write HTML, get PDF
- Good support for tables and images
Cons:
- Requires PHP 7.1+ and the mbstring, dom extensions
- Complex CSS layouts may break
4. mPDF — Hybrid Approach
mPDF is a library that combines the capabilities of TCPDF and Dompdf. It works excellently with HTML and CSS, supports Unicode, tables, headers/footers, watermarks. mPDF is especially good for creating books and magazines.
Example with CSS:
<?phprequire_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \\Mpdf\\Mpdf();$html = '<div style="border:1px solid #000; padding:10px;">Block content</div>';$mpdf->WriteHTML($html);$mpdf->Output('output.pdf', 'I');?>Advantages:
- Excellent support for CSS 2.1 and partial CSS3 <
Also in library
React for JavaScript (Node.js): A Complete Guide with Examples
Geolocation and Maps in C# (.NET): Libraries, Examples, and Recommendations
Apache HttpClient for Java: Complete Guide with Examples
InversifyJS: IoC Container for TypeScript Applications
Working with ZIP Archives in C++: libzip and zlib Libraries