PDFs are a common way to share documents, but creating them can be difficult. That’s where open-source PDF libraries for PHP come in. These libraries make it easy to create and manipulate PDFs, allowing you to get your work done quickly and easily. Here are five of the best open-source PDF libraries for PHP.
5 Best Open-Source PDF Libraries for PHP
TCPDF is the most popular PDF library for generating PDF documents. This PHP library doesn’t require extra libraries to be used except for basic functions and has all formats available like page margins, units of measurement, and Unicode languages-all without any need for externally compiling the standard TCPDF classes from within your local environment.
It’s compatible with UTF8 (UTF-8 Unicode) and Right-To-Left languages. Font subsetting is also possible with support for TrueTypeUnicode, OpenTypeUnicode v1, TrueType, OpenType v1, Type1 and CID-0 fonts.
Output can be in JPEG, PNG, or SVG format and it’s also possible to attach images, graphics, transformation methods, and ICC profiles.
require_once('tcpdf_include.php'); // create new PDF document $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // set document information $pdf->setCreator(PDF_CREATOR); $pdf->setAuthor('Nicola Asuni'); $pdf->setTitle('TCPDF Example 001'); $pdf->setSubject('TCPDF Tutorial'); $pdf->setKeywords('TCPDF, PDF, example, test, guide'); // set default header data $pdf->setHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128)); $pdf->setFooterData(array(0,64,0), array(0,64,128)); // set header and footer fonts $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); // set default monospaced font $pdf->setDefaultMonospacedFont(PDF_FONT_MONOSPACED); // set margins $pdf->setMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); $pdf->setHeaderMargin(PDF_MARGIN_HEADER); $pdf->setFooterMargin(PDF_MARGIN_FOOTER);
PDF annotations are also possible with links, text, and file attachments. TCPDF has text rendering modes, and multiple columns mode and allows no-write page regions. Automatic page numbering and groups are features along with the ability to move and delete pages.
Finally, compression is available after installing the php-zlib extension.
FPDF is a free, open-source PDF library for PHP. This means that you can generate technical documents and pdf files with pure PHP code. You don’t need to rely on external libraries or software to convert your data from HTML or other formats into an interactive PDF document.
require('fpdf.php'); $pdf = new FPDF(); $pdf->AddPage(); $pdf->SetFont('serif','A', 14); $pdf->Cell(50, 20, 'FPDF row here'); $pdf->Output();
FPDF includes features like high-level functions, page header, and footer management, image support, colors, links, and more. It also supports different page formats and margins. Page compression can help to keep file sizes small, making your documents easy to download and view.
The latest version of FPDF is PHP 5, 7, and 8 compatible. Additionally, it requires the Zlib extension for compression and the GD extension for GIF support.
This library is easy to use and can generate high-quality PDFs from UTF-8 encoded HTML. This library was developed based on HTML2FPDF and FPDF.
require_once __DIR__ . '/vendor/autoload.php'; $mpdf = new \Mpdf\Mpdf(); $mpdf->WriteHTML('<h2>mpdf title</h2>'); $mpdf->Output();
It also has a number of advanced features, such as compression and embedding resources like fonts.
Some advanced features, like compression of output and embedded resources like fonts, require additional extensions. These extensions may be needed for tasks such as generating barcodes or converting character sets.
Snappy is a good libraryfor generating thumbnails, snapshots, or PDFs from a URL or HTML page.
require __DIR__ . '/vendor/autoload.php'; use Knp\Snappy\Pdf; $snappy = new Pdf('/usr/local/bin/wkhtmltopdf');
It uses the excellent WebKit-based wkhtmltopdf and wkhtmltoimage libraries, which are available on OSX, Linux, and Windows. Be sure to download the latest version of wkhtmltopdf (0.12.x) in order to use Snappy!
This renderer is style-driven. It will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements. It also understands and supports most of the recent HTML tags.
It strives for high syntax and semantics compliance with HTML 4.0 and CSS 2.1 standards such as the W3C validator.
use Dompdf\Dompdf; // instantiate and use the dompdf class $dompdf = new Dompdf(); $dompdf->loadHtml('hello world'); // (Optional) Setup the paper size and orientation $dompdf->setPaper('A4', 'landscape'); // Render the HTML as PDF $dompdf->render(); // Output the generated PDF to Browser $dompdf->stream();
It can handle complex data sets, including row and column spans, separate and collapsed border models, and individual cell formatting.
The library supports images in common formats such as JPG and PNG. And it doesn’t require any other external libraries for handling PDF.