Performance and memory
Written for mPDF 8.2 on PHP 8.1+. Updated 2 November 2026.
mPDF does real typesetting in PHP, so big documents cost time and memory. A few settings and habits make a large difference.
“Allowed memory size exhausted”
The quick fix is more memory for the script that makes PDFs:
ini_set('memory_limit', '512M');
The better fix is to make the job lighter, using the techniques below.
Write in chunks
Passing one 5 MB HTML string to WriteHTML() holds the whole parsed document in memory. Feed it piece by piece instead:
$mpdf->WriteHTML($css, \Mpdf\HTMLParserMode::HEADER_CSS);
foreach ($sections as $section) {
$mpdf->WriteHTML($section, \Mpdf\HTMLParserMode::HTML_BODY);
}
Write the CSS once, then each section as body HTML. Each chunk should be a complete element: don’t split a table across two calls.
Big tables
Tables are the slowest part of mPDF’s layout, because it measures every cell to size the columns. For long data tables:
$mpdf->simpleTables = true; // skip complex per-cell border handling
$mpdf->packTableData = true; // store table data compactly in memory
simpleTables ignores per-cell borders and uses the table’s own, which is fine for most reports. Also give columns fixed widths (<td width="30%">), so mPDF doesn’t have to work them out, and avoid nested tables in long lists.
Images
Every image is decoded at full size. A 12-megapixel photo printed 6 cm wide wastes memory and file size. Resize images to the size they’ll print at, about 150–300 dpi (see images), and prefer JPEG for photos.
Fonts
- Font subsetting is on by default, so only used glyphs are embedded. Keep it on.
autoLangToFontchecks text for every script, which costs time. Turn it off for documents in a single known language.'mode' => 'c'(core fonts only) is fastest of all for plain English documents.
The temp directory
mPDF caches font metrics in tempDir. If that folder is wiped often, for example on each deploy or in a short-lived container, every run rebuilds the cache. Point it at persistent storage.
Do the work off the request
For anything more than a few pages, generate PDFs in a queue job and email them or store them for download. Visitors get an instant response, and a slow document never times out a web request.