PHP Create PDF file using mpdf | tcpdf | fpdf | php html to pdf

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.

PHP How-to create Dynamic PDF Files using FPDF
PHP How-to create Dynamic PDF Files using FPDF

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.

5. FPDF

Examples

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).
  • custom Colors.
  • custom Links.
  • TrueType, Type1 and encoding support.
  • Page compression.

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 :

AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Welcome To Pakainfo!');
$pdf->Output();
?>

4. mPDF

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.

Before start using it, i encorage you to read the documentation located at https://mpdf.github.io/.

The supporters example display a easy PDF after a composer installation with mPDF :

WriteHTML('

Welcome To Pakainfo


My first custom PDF with mPDF

'); // Output a PDF file directly to the browser $mpdf->Output(); ?>

3. DOMPDF

Github

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.

Characteristic

  • 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
  • custom Inline PHP support
  • easy to use custom SVG support

Requirements

  • PHP version 5.3.0 or higher
  • custom DOM extension
  • GD extension
  • MBString extension
  • custom php-font-lib
  • php-svg-lib

Recommendations

  • OPcache (OPcache, XCache, APC, etc.): improves performance
  • 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 :

loadHtml('

DOMPDF Demo


Welcome To Pakainfo !

'); // (Optional) Setup the paper size and orientation $dompdf->setPaper('A4', 'portrait'); // Render the HTML as PDF $dompdf->render(); // Output the generated PDF to Browser $dompdf->stream();

2. Snappy (wkhtmltopdf)

Github Snappy Library Package | Github wkhtmltopdf

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:

getOutput('http://www.github.com');

1. TCPDF

Github | Homepage

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.

This library Package supports Unicode fonts and it is actively maintained by Nicola Asuni in the Github repository.

It has a extense collection of examples and ready-to-test examples on its official homepage. TCPDF has as main characteristic :

  • No external libraries are required for the easy php functions.
  • all standard page formats, custom page formats, custom margins and units of measure.
  • UTF-8 Unicode and Right-To-Left languages.
  • TrueType, OpenType v1, TrueTypeUnicode, OpenTypeUnicode v1 Type1 with CID-0 fonts.
  • font subsetting.
  • methods to publish some XHTML + CSS code, Javascript with Forms.
  • images, graphic (geometric figures) with transformation methods.
  • supports JPEG, PNG with SVG images natively, all images supported by GD (GD, GD2, GD2PART, GIF, JPEG, PNG, BMP, XBM, XPM) with all images supported via ImagMagick (http://www.imagemagick.org/script/formats.php)
  • 1D with 2D barcodes: CODE 39, ANSI MH10.8M-1983, USD-3, 3 of 9, CODE 93, USS-93, Standard 2 of 5, Interleaved 2 of 5, CODE 128 A/B/C, 2 with 5 Digits UPC-Based Extension, EAN 8, EAN 13, UPC-A, UPC-E, MSI, POSTNET, PLANET, RMS4CC (Royal Mail 4-state Customer Code), CBC (Customer Bar Code), KIX (Klant index – Customer index), Intelligent Mail Barcode, Onecode, USPS-B-3200, CODABAR, CODE 11, PHARMACODE, PHARMACODE TWO-TRACKS, Datamatrix, QR-Code, PDF417.
  • JPEG with PNG ICC profiles, Grayscale, RGB, CMYK, Spot Colors with Transparencies.
  • automatic page header with custom footer management.
  • document encryption up to 256 bit with digital signature certifications.
  • custom transactions to UNDO commands.
  • PDF annotations, including links, text with file attachments.
  • text rendering modes (fill, stroke with clipping).
  • multiple columns mode.
  • no-write page regions.
  • bookmarks, named destinations with table of content.
  • custom text hyphenation.
  • text stretching with custom spacing (tracking).
  • automatic page break, line break with text alignments including justification.
  • automatic page numbering with page groups.
  • move with delete pages.
  • custom Page compression (requires php-zlib extension).
  • XOBject Templates.
  • Layers with object visibility.
  • PDF/A-1b support.

The supporters example how to render HTML as PDF with TCPDF :

SetCreator(PDF_CREATOR);
$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
$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);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// add a page
$pdf->AddPage();

$html = '

PDF Example


Welcome to the 4cgandhi

'; $pdf->writeHTML($html, true, false, true, false, ''); // add a page $pdf->AddPage(); $html = '

4cgandhi

'; // output the HTML content $pdf->writeHTML($html, true, false, true, false, ''); // reset pointer to the last page $pdf->lastPage(); //Close and output PDF document $pdf->Output('pakainfo_invoice.pdf', 'I');

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 !

Leave a Comment