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.

About the Author: Avinash

Experienced full stack freelance web developer with a track record of producing excellent results for 11+ years. Excellent communication and collaboration skills, managing with a team of 20+ people. I’m motivated to make a positive impact on your revenue through the customer web portal development project.

15 Comments

  1. […] 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 […]

  2. frostwire January 18, 2011 at 2:24 am - Reply

    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.

  3. Avinash January 18, 2011 at 9:43 pm - Reply

    Thanks

  4. dani April 9, 2011 at 11:30 am - Reply

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

  5. Philippe B April 9, 2011 at 1:19 pm - Reply

    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

  6. Remo April 9, 2011 at 1:59 pm - Reply

    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?

  7. aviation courses July 10, 2011 at 1:49 am - Reply

    Thanks for the comment, bookmarked!

  8. Submarine Manufacturing March 19, 2012 at 6:20 pm - 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

  9. Jordan April 19, 2012 at 6:20 pm - Reply

    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.

  10. Ketan June 1, 2012 at 7:26 pm - Reply

    Thanks Its really helpful to my team.

  11. Jerome June 5, 2012 at 7:56 pm - Reply

    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

  12. Dan February 3, 2013 at 2:39 pm - Reply

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

  13. John Steele March 18, 2013 at 2:37 am - Reply

    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

Leave A Comment

FREE QUOTE

Do you need help with services? Request a free quote to get in touch.