Web Development

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.

Shares:
  • […] This post was mentioned on Twitter by Avinash Zala and Avi, Avi. Avi said: Generate PDF from html using PHP: Hi all, generating the PDF from PHP coding is very time consuming work. In earli… http://bit.ly/dlZjAB […]

    Reply
  • frostwire
    frostwire
    January 18, 2011 at 2:24 am

    Wonderful goods from you, man. I’ve understand your stuff previous to and you are just extremely wonderful. I actually like what you’ve acquired here, really like what you’re stating and the way in which you say it. You make it entertaining and you still take care of to keep it wise. I cant wait to read much more from you. This is actually a tremendous site.

    Reply
  • Avinash
    January 18, 2011 at 9:43 pm

    Thanks

    Reply
  • dani
    April 9, 2011 at 11:30 am

    How does dompdf compare to mpdf? I’ve only used the latter.

    Reply
  • Philippe B
    April 9, 2011 at 1:19 pm

    Easiest and most powerfull / accurate way is to generate an html file and convert it to pdf using webkit engine. No more unit conversion hassle, full xhtml/css support, more accurate than your pc’s pdf driver !

    http://code.google.com/p/wkhtmltopdf/
    https://github.com/mreiferson/php-wkhtmltox

    Reply
  • Remo
    April 9, 2011 at 1:59 pm

    Thanks for this post! I’ve never heard of dompdf until know. Like Dani, I’ve worked with mpdf too. I think mpdf works quite nicely but uses a lot of resources. I’d especially like to know about performance differences between those two – has anyone worked with both?

    Reply
  • aviation courses
    July 10, 2011 at 1:49 am

    Thanks for the comment, bookmarked!

    Reply
  • Does anyone know if it is possible to generate PDF on fly using a background image as page template? We have a new letterhead design and we want to use this same design for generating all of our product datasheets dynamically using PHP

    Reply
  • Jordan
    Jordan
    April 19, 2012 at 6:20 pm

    I use http://www.pdfservices.net, create pdf from files html, css, php, ajax, mysql, printable and auto-print is better than any class I’ve used.

    Reply
  • Ketan
    Ketan
    June 1, 2012 at 7:26 pm

    Thanks Its really helpful to my team.

    Reply
  • Jerome
    Jerome
    June 5, 2012 at 7:56 pm

    Hi there,
    Thanks for the great info. Really interesting and definitely will save time.
    But i hear a lot of people comparing to dompdf, PDFLib etc… but which one perform better as i need to genereate thousands of pdf using a cron and dont want to take too long.

    Thanks again for all the info.

    Jerome

    Reply
  • Dan
    Dan
    February 3, 2013 at 2:39 pm

    wkhtmltopdf is the current de facto standard for HTML to PDF conversion. It’s based on WebKit and therefore can render with the same quality and look as some of the major browsers do (Safari, Chrome etc).

    Reply
  • John Steele
    March 18, 2013 at 2:37 am

    Seems no one has answered the performance/resources questions.

    Some one has got to have done some testing on this? I’d love to see some kind of comparisions…

    Thanks,

    John

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *