html4pdf vs mPDF

If you have outgrown Dompdf, mPDF is where most PHP projects land next, and for good reason. It is worth knowing exactly where its ceiling is before you build on it.

The short answer

mPDF is the best free answer in PHP for long, table-heavy documents written as documents. It is still its own engine, so a design built in a browser will not arrive intact.

What mPDF is

mPDF is a pure PHP HTML to PDF library, originally built on FPDF. It handles repeating table headers, running headers and footers, page numbering, watermarks, bookmarks and right-to-left scripts, which is more than most pure-language renderers manage. It needs no binary and installs with Composer.

Side by side

 mPDFhtml4pdf
Rendering engineIts own, in PHPHeadless Chrome
Tables across pagesStrong: repeating headers, row splittingVia CSS, which Chrome handles well
Headers and footersFirst class, with page countersPage options, or CSS Paged Media
Right-to-left textGood supportWhatever Chrome does, which is good
Flexbox and gridNot supportedSupported
JavaScriptNot executedRuns before capture
Memory on long documentsHigh, a known constraintServer side
InstallComposer, no system dependenciesNothing
CostFreeFrom $0.033 per document

Choose mPDF when

  • Long, table-heavy documents: statements, reports, catalogues.
  • You need running headers and page counters and would rather configure than write CSS.
  • Arabic, Hebrew or other right-to-left content in a PHP codebase.
  • No outbound calls, no recurring cost.

Choose html4pdf when

  • The document is a web page your designer built, and it has to look like one.
  • You need flexbox, grid or anything drawn by a script.
  • mPDF's memory use on long documents has become the bottleneck.
  • You would rather maintain one stylesheet than one for the screen and one for mPDF.

See the code

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

mPDF: common questions

mPDF or Dompdf?

mPDF, for anything long or table-heavy, and for right-to-left text. Dompdf is simpler and lighter for short documents. Neither is a browser, so neither reproduces a modern design.

Does mPDF support flexbox?

No. It implements its own subset of CSS, which does not include flexbox or grid, so those layouts fall back to block flow.

Why does mPDF run out of memory?

Layout happens in PHP and the whole document is held while it works, so long documents with large tables grow fast. Raising memory_limit helps up to a point; splitting the document helps more.

Can I keep mPDF for some documents?

Yes, and it is often the sensible split: leave the long statements where they work 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