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 and More (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.

In addition, reader Andy points out that TCPDF “…is a very flexible and powerful PDF generator that has CMYK compatibility, spot colours, crop marks, placing of various image formats including Jpeg PNG, EPS, AI and SVG. including support for alpha transparency. Because of its capabilities, it is used in many web to print plugins and applications on the web.”

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.

A Non-Free PDF Generator Service

If none of these free options works for you, consider a paid service such as DocRaptor, a REST HTML-to-PDF conversion API. I have not personally tried it, so let me know what you think of it if you do.

Conclusion

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

Shares

Please Leave a Question or Comment

Subscribe
Notify of
guest

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

4 Comments
Inline Feedbacks
View all comments
James Paden
2 months ago

Brian, it might be worth exploring the CSS Paged Media and Generated Content modules. While unfortunately not fully implemented by the browsers, the two modules provide a lot of advanced functionality for PDFs with multiple pages. Headers/footers, varying page styles and sizes within the same document, footnotes, leaders, cross-reference, etc..

Commercial tools such as DocRaptor (I work for DocRaptor) and PrinceXML offer the most CSS Paged Media support, but WeasyPrint and Paged.js are open-source tools focused on supporting paged media.

https://docraptor.com/css-paged-media has a lot more details.

Andy
Andy
3 months ago

Sad to see you have not given TCPDF the credit it is due, it is much more than a lightweight HTML to PDF file converter, it is a very flexible and powerful PDF generator that has CMYK compatibility, Spot colours, crop marks, placing of various image formats including Jpeg PNG, EPS, AI and SVG. including support for alpha transparency.

Because of its capabilities it is used in many web to print plugins and applications on the web.

TCPDF is a port of FPDF and has been developed futher which is why a lot of its functions are the same or similar.