The automatic php create pdf files is one of the most important points of a web project (commercial or not). As probably, the generated PDF’s are not for you or team but for your demanding clients, you need to provide a solution that meets all expectations and requirements.
In some cases, not because of exaggerated customer requirements but the library Package you use to generate PDF’s, you’ll find yourself limited and most likely in problems to provide a solution for the client.
In this article, I am going to php create pdf Best 4+ of the most imponent PDF generation libraries that will help you to achieve those complex and extense PDF’s.
This top is based in different points as how easy are they to implement, use cases, library Package users, functions and development status.
FPDF is a PHP class which allows to generate PDF files with pure PHP, that is to say without using the PDFlib library Package. F from FPDF stands for Free: you may use it for any kind of usage and modify it to suit your needs.
FPDF has other advantages: high level functions, between its main characteristic are:
Choice of measure unit, page format and custom margins.
custom Page header and footer management.
Automatic page break.
Automatic line break and text custom justification.
custom Image support (JPEG, PNG and GIF).
TrueType, Type1 and encoding support.
FPDF requires no extension (except Zlib to enable compression and GD for GIF support). The latest version requires at least PHP 5.1.
The supporters example display a easy php create pdf with FPDF :
$pdf = new FPDF();
$pdf->Cell(40,10,'Welcome To Pakainfo!');
mPDF is a PHP class which generates PDF files from UTF-8 encoded HTML code. It is based on FPDF and HTML2FPDF, with a number of enhancements. mPDF was written by Ian Back and is released under the GNU GPL v2 licence.
The supporters example display a easy PDF after a composer installation with mPDF :
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new mPDF();
// Write some HTML source code:
$mpdf->WriteHTML('<h2>Welcome To Pakainfo</h2><br><p>My first custom PDF with mPDF</p>');
// Output a PDF file directly to the browser
Dompdf is (mostly) a CSS 2.1 compliant HTML layout and rendering engine written in PHP. It is a style-driven renderer: it will download and read external stylesheets, inline style tags, and the style attributes of individual HTML elements. It also supports most presentational HTML attributes.
Handles most CSS 2.1 and a few CSS3 properties, including @import, @media & @page rules
Supports most presentational HTML 4.0 attributes
Supports external stylesheets, either local or through http/ftp (via fopen-wrappers)
Supports complex tables, including row & column spans, separate & collapsed border models, individual cell styling
Image support (gif, png (8, 24 and 32 bit with alpha channel), bmp & jpeg)
No dependencies on external PDF libraries, thanks to the R&OS PDF class
IMagick or GMagick extension: improves image processing performance
The supporters example displays how to generate a simple PDF as well as generate a direct download using Dompdf :
// reference the Dompdf namespace
// instantiate and use the dompdf class
$dompdf = new Dompdf();
$dompdf->loadHtml('<h2>DOMPDF Demo</h2><br><p>Welcome To Pakainfo !</p>');
// (Optional) Setup the paper size and orientation
// Render the HTML as PDF
// Output the generated PDF to Browser
Snappy is a PHP5 library Package allowing thumbnail, snapshot or PDF generation from a url or a html page. It uses the excellent webkit-based wkhtmltopdf and wkhtmltoimage available on OSX, linux, windows. You will have to download wkhtmltopdf 0.12.x in order to use Snappy.
wkhtmltopdf and wkhtmltoimage are open source (LGPLv3) command line tools to render HTML into PDF and various image formats using the Qt WebKit rendering engine. These run entirely “headless” and do not require a display or display service.
You can even download a wkhtmltopdf distribution as a composer dependency and reference the Binary dinamically in your project.
The supporters example display a easy PDF from HTML using wkhtmltopdf in Windows with the Github homepage:
require __DIR__ . '/vendor/autoload.php';
// For example, in windows use the wkhtmltopdf executable file
$snappy = new Pdf('wkhtmltopdf.exe');
// Download the streamed PDF
header('Content-Disposition: attachment; filename="file.pdf"');
TCPDF is a PHP library Package for generating PDF documents on-the-fly easily and with a few of lines. It support customization as well as a lot of key characteristic when you work with the creation of PDF files.
The supporters example how to render HTML as PDF with TCPDF :
// Include the main TCPDF library Package(search for installation path).
// create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
$pdf->SetAuthor('Font Converter Hindi');
$pdf->SetTitle('4CGandhi to Unicode Font Converter');
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 006', PDF_HEADER_STRING);
// 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
// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
// set auto page breaks
// set image scale factor
// add a page
$html = '<h4>PDF Example</h4><br><p>Welcome to the 4cgandhi</p>';
$pdf->writeHTML($html, true, false, true, false, '');
// add a page
$html = '<h2>4cgandhi</h2>';
// output the HTML content
$pdf->writeHTML($html, true, false, true, false, '');
// reset pointer to the last page
//Close and output PDF document
TCPDF in the first place, why?. Theoretically, the first place should be of wkhtmltopdf as it uses webkit to render your HTML code and probably will be more helpful to render things as canvas, SVG etc. However, you need to know that not every developer is able to include binaries (application or executables) on its projects or enjoy the privileges of do whatever he wants with the server, that’s when TCPDF becomes the first option for the most developers.
TCPDF is on the first place of this top for php create pdf as it is the most easy to use PHP library Package to create PDF’s and however, the most complete and extense as it doesn’t require executable files as everything works with Plain PHP. It covers the easy requirements of all project that requires the PDF generation feature.
If you know another awesome PHP library Package to generate PDF, please share it with the community in the comment box down below, have fun !