Generate PDFs Using Free Open-Source PHP Libraries

I recently had to figure out how to generate PDFs using PHP. There are tons of solutions out there, free and paid, with pros and cons. This is a summary of what I found out during my research, however, it’s by no means comprehensive. I basically stopped when I found solutions that satisfied my requirements. So, please feel free to comment on any insights you have in the comments below!

During my research, I discovered a continuum of PHP solutions ranging from simpler, lighter-weight solutions, where you construct each element of the page “by hand”, to much more heavyweight solutions that take in a whole HTML file and output a PDF. Here are the solutions I investigated, from lightweight to heavyweight.

1. FPDF: Ideal for Simple PDFs

Any search for a PHP PDF generator will lead you at some point to FPDF. It’s one of the most popular PHP PDF generators around, and a good place to start. It’s free, lightweight, and does not require any special libraries. It lets you set things like margins, headers, footers, and page breaks.

After initial setup, you construct the page by adding “cells” of content at specific coordinates. It’s great for simple documents where the content doesn’t vary much in size. But, if you need to display complex pages, the coordinate-based approach gets cumbersome and impractical quickly.

2. FPDFI: Ideal for Certificates

FPDFI allows you to import a PDF into FPDF (or TCPDF, described later in this article) and add content to it. This is actually a pretty awesome concept. Probably the most common application is to generate custom “graduation” certificates for online classes. You start with a PDF of a fancy border and seal, and just add the student’s name and date. Simple! Here’s a great tutorial on how to do it.

This is a great solution if you need to generate custom certificates, membership cards, name tags, or any document where you have simple custom content over a non-editable background.

3. TCPDF: Lightweight HTML Parsing (This Is What I Chose)

Like FPDF, TCPDF lets you set up margins, headers, footers, etc. But the difference is that it lets you use simple HTML and embedded CSS for the main content of the page. This is a game-changer compared with FPDF because it allows much more dynamic content. In our case, we had tables with varying lengths of content in each table cell. This type of page is easy to build in HTML but very difficult in FPDF. We just fed the HTML into TCPDF and it spit out a PDF!

Keep in mind, though, that TCPDF only parses a limited set of HTML tags and embedded CSS. You’re not going to get CSS grid working with this. But, for many applications, this might be the winner. It was for us.

As a bonus, TCPDF does not require any external libraries.

On the downside, there is technically not a supported version of TCPDF now. The current version is slated to be replaced by tc-lib-pdf, but that isn’t done yet. Part of the joys of free software.

4. mPDF: Similar to TCPDF

mPDF has similar capabilities as TCPDF. It takes simple HTML and converts it to a PDF. Since I was happy with TCPDF, I didn’t delve into mPDF very much, but it might be worth looking into for your application.

5. Dompdf: Better HTML Parsing

DomPDF has even more extensive CSS and HTML support than TCPDF. It can (mostly) parse a CSS2.1 style sheet in the head are of an HTML page, which is pretty impressive.

I tested it and it worked well. But, for me, the rendering time and, I assume, CPU resource requirement, were considerably more than for TCPDF. Since TCPDF did what I needed, it was a no-brainer for me to choose TCPDF. Also, I couldn’t figure out how to get Dompdf to generate headers and footers, but that might just be because I gave up too soon.

But, if you need better HTML and CSS rendering than TCPDF can offer, the Dompdf might be the best solution for you.

6. wkhtmltopdf: Real WebKit Rendering to PDF

If you need even more accurate HTML rendering to PDF, check out wkhtmltopdf, a command-line tool to render HTML into PDF using the Qt WebKit rendering engine.

To use this with PHP, you’ll need a wrapper. One example is Mike Haertl’s phpwkhtmltopdf. Another is snappy. These require installing wkhtmltopdf on your server, so it involves more expertise to set up. Also, this might put a considerable load on your server, so be sure to consider that.


I hope this quick survey was helpful to you. Another great article on this topic is Carlos Delgado’s Top 5: Best open source PDF generation libraries for PHP. Here’s another great article, Picking a PHP tool to generate PDFs (2021 update) by Piotr Horzycki.

Please let me know about your experience with PHP-to-PDF generators in the comments below! – Brian

Please Leave a Question or Comment

Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
%d bloggers like this: