I’ve pretty much tried all the PDF libraries out there for saving HTML to PDF appropriately and correctly using PHP and came to a conclusion (continue reading).

The one that worked the fastest and most efficiently was the HTML to PDF class on PHP Classes but eventually this EasySW remote HTML to PDF service in the HTML to PDF class stopped serving PDF files from HTML submissions as the Easy Software closed their business.

I then tried several others and it seems like DomPDF is the most robust and thorough library, parsing all formatting, styling, etc. from the HTML into the PDF file. There are certain things which are not supported such as floats but overall the DomPDF library is great and very effective. Best is that you don’t have to rely on a 3rd party service shutting down, this runs inside your application.

I’ll show you how to use DomPDF in PHP to generate PDF files from HTML code.

1. Download and Install

Download and Install DomPDF

Download DomPDF library from Google Code.

Then install DomPDF with the installation instructions provided.

2. Code HTML to PDF

Write your HTML to PDF code

With the installation of DomPDF done, you can write a short bit of code to load HTML into the DomPDF class and render and stream/output the PDF to the browser. Below is some code but you can look at the DomPDF Wiki as well for more information.