PDF¶
PDF, Portable Document Format, is a file format developed by Adobe that presents documents independently of software, hardware, or operating system. It is widely used for reports, invoices, contracts, and other print-ready documents.
PHP does not include a built-in PDF extension in its standard library. Several libraries are available: TCPDF and FPDF generate PDFs from scratch using PHP code; mPDF converts HTML/CSS to PDF; and Dompdf also renders HTML to PDF.
For reading or manipulating existing PDFs, smalot/pdfparser extracts text and metadata, and tools like pdftk can be called via exec().
The libharu extension provides a binding to the Haru Free PDF Library.
<?php
// Using Dompdf to generate a PDF from HTML
use Dompdf\Dompdf;
$dompdf = new Dompdf();
$dompdf->loadHtml('<h1>Hello, PDF!</h1><p>Generated by PHP.</p>');
$dompdf->setPaper('A4', 'portrait');
$dompdf->render();
// Output to browser
$dompdf->stream('document.pdf', ['Attachment' => false]);
?>
See also Generate PDFs with PHP: The Best 5 Libraries Compared.
Related : Image, JPEG, PNG, Plain Text, Rich Text, Template
Related packages : dompdf/dompdf, tecnickcom/tcpdf, mpdf/mpdf