Generate PDF from html using PHP

  • Avinash
  • 15
  • Oct 23, 2010
  • Web Development

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:

  • Pingback: Tweets that mention Generate PDF from html using PHP | Xpert Developer -- Topsy.com

  • frostwire

    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.

  • xpertdev

    Thanks

  • http://www.inmensys.com dani

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

  • http://www.zeeagency.com/ Philippe B

    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

    • xpertdev

      Thanks for the links…

  • http://www.codeblog.ch Remo

    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?

  • http://www.yahoo.com aviation courses

    Thanks for the comment, bookmarked!

    • xpertdev

      thx..

  • http://www.smp-ltd.co.uk Submarine Manufacturing

    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

  • Jordan

    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.

  • Ketan

    Thanks Its really helpful to my team.

  • Jerome

    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

  • http://www.htm2pdf.co.uk/ Dan

    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).

  • http://steelesoftconsulting.com/ John Steele

    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