Generate PDF from html using PHP

Hi all, generating the PDF from PHP coding is very time consuming work. In earlier days developers were using FPDF for generating PDF from PHP code. But in now a days there are several libraries are available which can generate the PDF from the HTML you provide to them. So there is no task included like measurement and all that.

In earlier days FPDF is used. Below are some features for that:

FPDF

FPDF is a PHP class which allows to generate PDF files with pure PHP, that is to say without using the PDFlib library. F from FPDF stands for Free: you may use it for any kind of usage and modify it to suit your needs.

FPDF features:

  • Choice of measure 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)
  • Colors
  • Links

Here are the libraries which can generate PDF from the HTML directly.

DomPDF

dompdf is an HTML to PDF converter. dompdf is (mostly) CSS2.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.

DomPDF Features:

  • handles most CSS2.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, bmp & jpeg)
  • no dependencies on external PDF libraries, thanks to the R&OS PDF class

You can find the dompdf code at here. You will get the whole code with lots of examples developed in that.

TCPDF

TCPDF is an Open Source PHP class for generating PDF documents.

TCPDF project was started in 2002 and now it is freely used all over the world by millions of people. TCPDF is a Free Libre Open Source Software (FLOSS).

TCPDF  Features:

  • no external libraries are required for the basic functions;
  • all standard page formats, custom page formats, custom margins and units of measure;
  • UTF-8 Unicode and Right-To-Left languages;
  • TrueTypeUnicode, OpenTypeUnicode, TrueType, OpenType, Type1 and CID-0 fonts;
  • font subsetting;
  • methods to publish some XHTML + CSS code, Javascript and Forms;
  • images, graphic (geometric figures) and transformation methods;
  • page compression (requires php-zlib extension);
  • automatic page header and footer management;

For more features checkout here.

I have included few of the libraries, but if you are aware of any other good library then share with all by commenting here.

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by: