Generating PDF Reports in PHP: Overview of Libraries and Tools
Creating PDF documents is one of the common tasks in PHP web development. Whether it's generating invoices, reports, certificates, or simply exporting data to a print-friendly format, PHP offers several mature solutions. In this article, we will look at four main libraries: TCPDF, FPDF, Dompdf, and mPDF. You will learn their features, strengths, and get code examples for each.
1. FPDF — Lightweight and Fast Classic
FPDF is perhaps the most well-known library for generating PDFs in PHP. It requires no external extensions, works out of the box, and is very fast. The main drawback is limited support for complex CSS styles and HTML.
Example of creating a simple PDF report with a table:
<?phprequire_once('fpdf.php');
$pdf = new FPDF();$pdf->AddPage();$pdf->SetFont('Arial', 'B', 16);$pdf->Cell(40, 10, 'Sales Report');$pdf->Ln(20);
$pdf->SetFont('Arial', '', 12);// Table headers$pdf->Cell(60, 7, 'Product', 1);$pdf->Cell(30, 7, 'Quantity', 1);$pdf->Cell(40, 7, 'Price', 1);$pdf->Ln();
// Data$items = [ ['Laptop', 5, 45000], ['Mouse', 10, 1200], ['Keyboard', 7, 2500]];
foreach ($items as $item) { $pdf->Cell(60, 7, $item[0], 1); $pdf->Cell(30, 7, $item[1], 1); $pdf->Cell(40, 7, $item[2] . ' RUB', 1); $pdf->Ln();}
$pdf->Output();?>When to use: for simple reports where complex layout is not needed, and speed and small file size are important.
2. TCPDF — Powerful Tool for Complex PDFs
TCPDF is a fork of FPDF with extended capabilities. It supports Unicode, JPEG2000, QR codes, SVG, as well as working with HTML and CSS (limited). The library is actively developed and used in systems such as Drupal and Moodle.
Example of generating a report using HTML markup:
<?phprequire_once('tcpdf/tcpdf.php');
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);$pdf->SetCreator(PDF_CREATOR);$pdf->SetAuthor('Your Company');$pdf->SetTitle('Monthly Report');
$pdf->AddPage();$html = '<h2>Monthly Report</h2><table border="1" cellpadding="5"> <tr style="background-color:#cccccc;"> <th>Indicator</th><th>Value</th> </tr> <tr> <td>Revenue</td><td>1,200,000 RUB</td> </tr> <tr> <td>Expenses</td><td>850,000 RUB</td> </tr> <tr> <td>Profit</td><td><strong>350,000 RUB</strong></td> </tr></table><p>Date generated: ' . date('d.m.Y') . '</p>';
$pdf->writeHTML($html, true, false, true, false, '');$pdf->Output('report.pdf', 'I');?>When to use: for complex documents with tables, images, custom fonts, and Cyrillic support.
3. Dompdf — Turning HTML into PDF
Dompdf is a library that renders an HTML page into PDF. It supports CSS2.1 and partially CSS3, allowing you to create reports using familiar layout techniques. It is ideal for converting ready-made HTML templates.
Example of creating a report from an HTML template:
<?phprequire_once 'dompdf/autoload.inc.php';use Dompdf\\Dompdf;
$dompdf = new Also in library
Generating PDF Reports in JavaScript (Node.js): Library Overview and Code Examples
Generating PDF Reports in C++ with libharu and PoDoFo
Creating CLI Applications in Node.js: Commander and Yargs Frameworks
Working with Excel in Kotlin: Apache POI and Apache POI for Kotlin Libraries
Working with Markdown in Kotlin: Libraries, Parsing, and Generation