Guides › mPDF › Troubleshooting mPDF

Troubleshooting mPDF

Written for mPDF 8.2 on PHP 8.1+. Updated 2 November 2026.

These are the problems that come up again and again on forums and Q&A sites, with the fix for each.

“Data has already been sent to output, unable to output PDF file”

Something printed output before mPDF tried to send the PDF. Look for:

  • an echo, print_r or var_dump left in the code
  • a PHP warning or notice shown on screen (check display_errors)
  • a blank line or byte-order mark before <?php, or after ?> in an included file

Remove the output. Leaving out the closing ?> in PHP-only files avoids the last cause. As a stopgap, ob_clean() just before Output() discards what was printed. In a framework, return the PDF in a response instead (see output).

“Temporary files directory … is not writable”

mPDF needs a writable cache folder. Set one explicitly:

$mpdf = new \Mpdf\Mpdf(['tempDir' => sys_get_temp_dir() . '/mpdf']);

“Class ‘mPDF’ not found”

This is mPDF 6 code running against mPDF 7 or 8. The class is now \Mpdf\Mpdf, it’s loaded with Composer’s autoloader, and it takes one array of settings. See install and configuration.

“Unable to create output file”

You used the FILE destination and PHP can’t write to that path. Check that the folder exists and is writable by the web server user. Use an absolute path.

Images are missing

Turn on $mpdf->showImageErrors = true; to see the reason. Usually the relative path is resolved from somewhere other than you expect, or a remote URL can’t be fetched (firewall, SSL certificate, allow_url_fopen off). See images.

Squares, question marks or blank spaces instead of text

The font doesn’t contain those characters. Use a font that does, such as dejavusans, or turn on autoScriptToLang and autoLangToFont. Also check that mode isn’t c and that the text really is UTF-8. See fonts and languages.

The layout looks nothing like the browser

mPDF doesn’t support flexbox, grid or several other modern CSS features. Rebuild the layout with tables or floats. Supported CSS lists the gaps and the workarounds.

Increase margin_top and margin_bottom so the body starts below the header and ends above the footer. margin_header and margin_footer only set where the header and footer themselves sit.

“Allowed memory size exhausted” or very slow output

See performance and memory: write in chunks, use simpleTables and packTableData for large tables, and resize images.

The PDF shows as garbage text in the browser

The browser received the PDF bytes without a Content-Type: application/pdf header. That usually means the framework or a debug toolbar added output or headers. Return the bytes with the correct headers, as in output.

Still stuck?

Search or ask with the mpdf tag on Stack Overflow, and include your mPDF version, PHP version and a minimal HTML sample. The project’s GitHub repository is the place for genuine bugs.