The php pdf library for automatic creation of PDF files is one amongst the foremost small print of a project (commercial or not for PDF generation libraries). As most likely, the generated PDF’s aren’t for you or team except for your demanding shoppers, you wish to produce an answer that meets all expectations and necessities.
Top five : Best open source PDF generation libraries for PHP
In some cases, not owing to exaggerated client necessities however the library you employ to get PDF’s, you will find yourself restricted and presumably in issues to produce an answer for the shopper.
In this article, we have a tendency to are aiming to show five of the foremost imponent PDF generation libraries which will assist you to realize those advanced and extense PDF’s.
This high is predicated in numerous points as however straightforward ar they to implement, use cases, library users, functions and development standing.
5. FPDF : PDF generation libraries
FPDF could be a PHP category that permits to get PDF files with pure PHP, that’s to mention while not exploitation the PDFlib library. F from FPDF stands for Free: you will use it for any reasonably usage and modify it to fit your wants.
FPDF has different advantages: high level functions, between its main options are:
Choice of live unit, page format and margins.
- Page header and footer management.
- Automatic page break.
- Automatic line break and text justification.
- Image support (JPEG, PNG and GIF).
- TrueType, Type1 and encryption support.
- Page compression.
- Free PDF generation libraries
FPDF needs no extension (except Zlib to alter compression and GD for GIF support). the most recent version needs a minimum of PHP 5.1.
The following snip show a basic PDF generated with FPDF :
<?php require('fpdf/fpdf.php'); $f_pdf = new FPDF(); $f_pdf->AddPage(); $f_pdf->SetFont('Arial','B',17); $f_pdf->Cell(40,10,'Hello World At pakainfo.org !'); $f_pdf->Output(); ?>
4. mPDF : PDF generation libraries
mPDF could be a PHP category that generates PDF files from UTF-8 encoded html. it’s supported FPDF and HTML2FPDF , with variety of enhancements. mPDF was written by Ian Back and is free beneath the antelope GPL v2 licence.
Before begin exploitation it, we have a tendency to encorage you to browse the documentation settled at https://mpdf.github.io.
The following snip show a basic PDF once a musician installation with mPDF :
<?php // require_once autoload.php of mPDF require_once a dirty . '/vendor/autoload.php'; // mPDF category $m_pdf = new mPDF(); // Add some html code/text: $m_pdf->WriteHTML('<h2>Hello World,</h2><br><p>My 1st PDF with mPDF At pakainfo.org !!</p>'); // Output a PDF file directly to your the browser $m_pdf->Output(); ?>
3. DOMPDF : PDF generation libraries
Dompdf is (mostly) a CSS two.1 compliant HTML layout and rendering engine written in PHP. it’s a style-driven renderer: it’ll transfer and skim external stylesheets, inline vogue tags, and also the vogue attributes of individual HTML parts. It additionally supports most display HTML attributes.
- Handles most CSS 2.1 and a couple of CSS3 properties, as well as @import, @media & @page rules
- Supports most display HTML 4.0 attributes
- Supports external stylesheets, either native or through http/ftp (via fopen-wrappers)
- Image support (gif, png (8, twenty four and thirty two bit with alpha channel), bmp & jpeg)
- Free PDF generation libraries
- Supports advanced tables, as well as row & column spans, separate & folded border models, individual cell styling
- Inline PHP support
- No dependencies on external PDF libraries, because of the R&OS PDF category
- Basic SVG support
- DOM extension
- PHP version 5.3.0 or higher
- GD extension
- MBString extension
- IMagick or GMagick extension: improves image process performance
- OPcache (OPcache, XCache, APC, etc.): improves performance
The following snip shows a way to generate an easy PDF and generate an on the spot transfer exploitation Dompdf as PDF generation libraries :
<?php // need dompdf autoload file require_once 'dompdf/autoload.inc.php'; // the Dompdf namespace for reference use Dompdf\Dompdf; // the dompdf category for instantiate and use $dom_pdf = new Dompdf(); $dom_pdf->loadHtml('<h2>Demo OF DOMPDF</h2><br><p>Hello World At pakainfo.org !!!</p>'); // (Optional) Setup the paper size and orientation $dom_pdf->setPaper('A4', 'portrait'); // Render the HTML as PDF $dom_pdf->render(); // Output the generated PDF to Browser $dom_pdf->stream();
2. Snappy (wkhtmltopdf) : PDF generation libraries
Snappy could be a PHP5 library permitting fingernail, shot or PDF generation from a universal resource locator or a HTML page. It uses the wonderful webkit-based wkhtmltopdf and wkhtmltoimage accessible on OSX, linux, windows. you may need to transfer wkhtmltopdf zero.12.x so as to use Snappy.
wkhtmltopdf and wkhtmltoimage are open supply (LGPLv3) command tools to render HTML into PDF and numerous image formats exploitation the Qt WebKit rendering engine. These run entirely “headless” and don’t need a show or show service.
You can even transfer a wkhtmltopdf distribution as a musician dependency and reference the Binary dinamically in your project.
The following snip show a basic PDF from HTML exploitation wkhtmltopdf in Windows with the Github homepage:
php pdf library
<?php require a unclean . '/vendor/autoload.php'; use Knp\Snappy\Pdf; // for instance, in windows use the wkhtmltopdf feasible file $snappy_pdf = new Pdf('wkhtmltopdf.exe'); // we transfer the streamed PDF // set header header('Content-Type: application/pdf'); header('Content-Disposition: attachment; filename="file_pakainfo.pdf"'); // execute file echo $snappy_pdf->getOutput('http://www.github.com');
1. TCPDF : PDF generation libraries
TCPDF could be a PHP library for generating PDF documents on-the-fly simply and with one or two of lines. It support customization and lots of key options after you work with the creation of PDF files.
This library supports Unicode fonts and it’s actively maintained by Nicola Asuni within the Github repository.
It has a extense assortment of examples and ready-to-test snippets on its official homepage. TCPDF has as main options :
- No external libraries area unit needed for the fundamental functions.
- UTF-8 Unicode and Right-To-Left languages.
- all commonplace page formats, custom page formats, custom margins and units of live.
- TrueTypeUnicode, OpenTypeUnicode v1, TrueType, OpenType v1, Type1 and CID-0 fonts.
- supports JPEG, PNG and SVG pictures natively, all pictures supported by GD (GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM) and every one pictures supported via ImagMagick (http://www.imagemagick.org/script/formats.php)
- images, graphic (geometric figures) and transformation strategies.
- JPEG and PNG ICC profiles, Grayscale, RGB, CMYK, Spot colours and Transparencies.
- 1D and second barcodes: CODE thirty-nine, ANSI MH10.8M-1983, USD-3, 3 of 9, CODE 93, USS-93, commonplace two of five, Interleaved two of five, CODE 128 A/B/C, two and five Digits UPC-Based Extension, EAN 8, EAN 13, UPC-A, UPC-E, MSI, POSTNET, PLANET, RMS4CC (Royal Mail 4-state client Code), complete blood count (Customer Bar Code), KIX (Klant index – client index), Intelligent Mail Barcode, Onecode, USPS-B-3200, CODABAR, CODE 11, PHARMACODE, PHARMACODE TWO-TRACKS, Datamatrix, QR-Code, PDF417.
- transactions to UNDO commands.
- automatic page header and footer management.
- PDF annotations, as well as links, text and file attachments.
- document secret writing up to 256 bit and digital signature certifications.
- bookmarks, named destinations and table of content.
- no-write page regions.
- multiple columns mode.
- text rendering modes (fill, stroke and clipping).
- text stretching and spacing (tracking).
- automatic page enumeration and page teams.
- text hyphenation.
- Page compression (requires php-zlib extension).
- automatic page break, line break and text alignments as well as justification.
- Layers and object visibility.
- move and delete pages.
- PDF/A-1b support.
- XOBject Templates.
- Free PDF generation librari
The following example the way to render HTML as PDF with TCPDF as PDF generation libraries:
<?php // embrace the most TCPDF library (search for installation path). require_once('tcpdf_include.php'); // produce new PDF document $tc_pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // set document info $tc_pdf->SetCreator(PDF_CREATOR); $tc_pdf->SetAuthor('Our Code World'); $tc_pdf->SetTitle('Example Write Html'); // set default header information $tc_pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 006', PDF_HEADER_STRING); // set header and footer fonts $tc_pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); $tc_pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); // set default fount $tc_pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); // set margins $tc_pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); $tc_pdf->SetHeaderMargin(PDF_MARGIN_HEADER); $tc_pdf->SetFooterMargin(PDF_MARGIN_FOOTER); // set motorcar page breaks $tc_pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); // set image multiplier factor $tc_pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); // add a page $tc_pdf->AddPage(); $html = '<h4>PDF Example</h4><br><p>Welcome to the www.pakainfo.com</p>'; $tc_pdf->writeHTML($html, true, false, true, false, ''); // add a page $tc_pdf->AddPage(); $html = '<h1>Hey</h1>'; // output the HTML content $tc_pdf->writeHTML($html, true, false, true, false, ''); // reset pointer to the last page $tc_pdf->lastPage(); //Close and output PDF document $tc_pdf->Output('example_pakainfo.pdf', 'I');
TCPDF within the initial place as PDF generation php pdf library, why?. in theory, the primary place ought to be of wkhtmltopdf because it uses webkit to render your HTML and possibly are going to be a lot of useful to render things as canvas, SVG etc. However, you wish to grasp that not each developer is ready to incorporate binaries (application or executables) on its comes or fancy the privileges of do no matter he needs with the server, that is once TCPDF becomes the primary possibility for the foremost developers.
TCPDF is on the primary place of this high because it is that the most simple to use PHP library to form PDF’s and but, the foremost complete and extense because it does not need viable files as everything works with Plain PHP. It covers the fundamental needs of all project that needs the PDF generation feature.
Here are some more FAQ related to this Article: