DEV Community

loading...

What is the best way to create PDF files with PHP

antondachauer profile image Anton Dachauer ・1 min read

Hello, what is the best and easiest way to create PDF files with PHP?

I know about libs like fpdf, tcpdf and wkhtmltopdf. But they have all their own limitations.

What is your preferred way to create pdf files with php, optimaly by transforming a html file?

Discussion (5)

pic
Editor guide
Collapse
marcusatlocalhost profile image
Marcus

I got pretty far with wkhtmltopdf, but it can be tricky or one is not able to install the latest version at all (depending on hosting plan)

There is a python lib weasy print, that seems to be good, but you need to install a ton of stuff.

Typeset.sh typeset.sh/en/ is pure php and seems to be pretty good, but it is expensive.

Here is an overview: html2pdf.guru/

Collapse
sergiokessler profile image
Sak

I used "spipu/html2pdf" for a while, then switched to mpdf (way faster, and a little more css complaint).

The workflow always being: take the html, give me the pdf.

No mater what you choose, be prepared for some pain...

Collapse
bpedroza profile image
Bryan

Mpdf is fast but lacks many features and newer css support. DOMPDF is more full featured but much much slower.

A different approach is to just open the print dialog with js and let the user print to PDF with the browser, which is almost certainly going to be a more accurate representation of what's on screen than any PHP library.

Collapse
iamreinder profile image
Reinder

Mpdf always got me quite far. But in my opinion, it's always somewhat of a pain, no matter what package you use.

There are generator services that allow generating PDFs through an api. I've seen this working incredibly well. And probably in some use cases the benefits will outweigh the costs. But these services are quite expensive, so it's often too costly for small projects.

The best solution for me is to create a nice looking template and adding text in fixed positions to that using Mpdf. That works rather well. The more complex it becomes, the harder it gets to generate good PDFs, no matter which package for php you pick.

Collapse
imcarlosnunez profile image
Carlos Núñez

I've been using TCPDF with Laravel for simple or complex printing or parsing html to pdf