html4pdf vs Dompdf

Nearly every PHP project reaches for Dompdf first, usually through laravel-dompdf. It is free, installs with Composer, and needs nothing on the server. Then somebody applies a flexbox layout and the PDF arrives stacked.

The short answer

Dompdf is the right answer for a simple document on shared hosting with no extra services. It is the wrong answer for anything designed with modern CSS, because it implements CSS 2.1.

What Dompdf is

Dompdf is an HTML to PDF converter written entirely in PHP. It targets CSS 2.1 with some CSS 3 properties, needs no binary and no shell access, and works on hosting where you cannot install anything. For headings, paragraphs and tables it does a sound job.

Side by side

 Dompdfhtml4pdf
Rendering engineIts own, in PHP, CSS 2.1Headless Chrome
Flexbox and gridNot supportedSupported
JavaScriptNot executedRuns before capture
Web fontsLocal fonts need registering, with caveatsGoogle Fonts and @font-face work
InstallComposer, no system dependenciesNothing
Shared hostingWorksWorks, it is just an HTTPS call
Long documentsSlow and memory hungry past a few dozen pagesHandled server side
CostFreeFrom $0.033 per document

Choose Dompdf when

  • The document is simple: a heading, some paragraphs, a table.
  • You want zero recurring cost and zero outbound calls.
  • You are on hosting where you control nothing.
  • It already works and nobody is changing the design.

Choose html4pdf when

  • The invoice template uses flexbox or grid and arrives looking nothing like the preview.
  • You need a web font, or a chart, or anything drawn by a script.
  • Documents are long enough that Dompdf's memory use has become a problem.
  • You want the PDF to match the page your designer approved, without maintaining a second stylesheet.

See the code

The working example, in the language this decision is being made in.

Dompdf: common questions

Does Dompdf support flexbox?

No. It implements CSS 2.1 with some additions, and flexbox is not part of that. Flex containers fall back to block layout, which is why columns arrive stacked.

Dompdf or mPDF?

mPDF handles tables, headers, footers and right-to-left text better, and is the stronger choice for long documents. Both implement their own engine, so neither gives you browser-accurate output.

Why is my Dompdf output so slow?

Layout runs in PHP, so cost grows with the number of elements. Large tables are the usual cause. Reducing nesting helps, but there is a ceiling.

Can I keep Dompdf for some documents?

Yes, and that is often the sensible move: leave the simple receipts where they are and route only the designed documents elsewhere.

Try it before you decide

New accounts get 10 free credits, no card. Send the HTML you already have and compare the output against whatever you are using now.

Get API key

Other comparisons