PDF Generation in PHP: Overview of Libraries and Tools
Creating PDF documents is a common task in web development. PHP offers several mature libraries that allow you to generate PDF from HTML, dynamic data, or directly through drawing. In this article, we will look at four main libraries: TCPDF, FPDF, Dompdf, and mPDF, their features, usage examples, and recommendations for choosing for an educational website.
1. TCPDF — Industry Standard
TCPDF is one of the most powerful libraries for PDF generation in PHP. It supports Unicode, TrueType and OpenType fonts, password protection, watermarks, QR codes, and much more. It is ideal for creating complex reports, invoices, and certificates.
Installation: via Composer: composer require tecnickcom/tcpdf
Example of creating a simple PDF:
<?phprequire_once __DIR__ . '/vendor/autoload.php';
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator('PythonLib.ru');$pdf->SetAuthor('Author');$pdf->SetTitle('TCPDF Example');$pdf->SetSubject('PDF Generation');
$pdf->AddPage();$pdf->SetFont('dejavusans', '', 14);$pdf->Cell(0, 10, 'Hello, world! This is a PDF from TCPDF.', 0, 1);
$pdf->Output('example_tcpdf.pdf', 'I');?>2. FPDF — Lightweight and Fast
FPDF is a classic library for simple PDF documents. It does not require installing additional extensions and is very easy to learn. It is suitable for creating tables, simple reports, and invoices.
Installation: download from fpdf.org or via Composer: composer require setasign/fpdf
Example with a table:
<?phprequire_once __DIR__ . '/vendor/autoload.php';
$pdf = new FPDF();$pdf->AddPage();$pdf->SetFont('Arial', 'B', 12);
// Table headers$pdf->Cell(40, 10, 'First Name', 1);$pdf->Cell(40, 10, 'Last Name', 1);$pdf->Cell(40, 10, 'Age', 1);$pdf->Ln();
$pdf->SetFont('Arial', '', 10);$pdf->Cell(40, 10, 'Ivan', 1);$pdf->Cell(40, 10, 'Petrov', 1);$pdf->Cell(40, 10, '25', 1);
$pdf->Output('example_fpdf.pdf', 'I');?>3. Dompdf — From HTML to PDF
Dompdf allows you to convert HTML+CSS to PDF. This is convenient if you already have page layouts (e.g., an invoice or an article). It supports CSS 2.1 and partially CSS 3, fonts, and images.
Installation: composer require dompdf/dompdf
Example of HTML conversion:
<?phprequire_once __DIR__ . '/vendor/autoload.php';
use Dompdf\\Dompdf;
$dompdf = new Dompdf();$html = '<h1 style="color: #3498db;">PDF Title</h1><p>This document was created from HTML using Dompdf.</p><table border="1" cellpadding="5"> <tr><th>Column 1</th><th>Column 2</th></tr> <tr><td>Data 1</td><td>Data 2</td></tr></table>';
$dompdf->loadHtml($html);$dompdf->setPaper('A4', 'portrait');$dompdf->render();$dompdf->stream('example_dompdf.pdf', ['Attachment' => 0]);?>4. mPDF — Powerful HTML Converter
mPDF is another library for converting HTML to PDF. It has better support for CSS, Unicode, Eastern fonts, table of contents creation, and headers/footers compared to Dompdf. It is excellent for books, magazines, and complex documents.
Installation: composer require mpdf/mpdf
Example with headers and footers:
<? Also in library
Go (Golang) Standard Library: Complete Overview
Testing in C++: Overview of Frameworks and Tools (Google Test, Catch2, doctest)
Working with Excel in PHP: PhpSpreadsheet and OpenSpout Libraries
WebSocket in Rust: tokio-tungstenite and tungstenite libraries