Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The most reliable fix for mPDF’s “The HTML code size is larger than pcre.backtrack_limit” error is to pass smaller, structurally complete pieces of HTML to WriteHTML(). If you control the PHP runtime, you can also raise pcre.backtrack_limit cautiously, but there is no universal safe value: very high settings can put PHP processes at risk. Large tables, complex CSS, and PHP/mPDF version compatibility can still cause problems after the error disappears.

What the error means

mPDF parses HTML and CSS using PHP’s regular-expression processing. The error means that processing reached the configured pcre.backtrack_limit while mPDF handled an input string. The message is about the amount of work allowed during parsing, not simply a fixed maximum PDF page count.

One reported mPDF issue reproduces the exception with output ranging from 2,500 to 6,000 rows and includes the message: “The HTML code size is larger than pcre.backtrack_limit 1000000. You should use WriteHTML() with smaller string lengths.” The row count is an example, not a general threshold: the effect depends on the HTML, CSS, runtime, and layout.

PHP’s current configuration documentation lists a default of 1,000,000 for pcre.backtrack_limit. It records 100,000 as the default in PHP versions before 5.3.7. The setting’s availability and effective value depend on the PHP version and configuration used by the process that generates the PDF.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Start by checking the failing input

  1. Confirm the exact exception. Distinguish the HTML-size message from a memory-limit error, timeout, or regex compilation error. They can occur in the same large-document workflow but do not have the same fix.
  2. Find the WriteHTML() call that fails. Log the approximate byte length of the HTML string passed to each call. If you write CSS separately, record that input too. Avoid logging sensitive document contents.
  3. Check the runtime configuration. Read the effective pcre.backtrack_limit in the same PHP execution context as the PDF job. CLI, PHP-FPM, and web-server PHP can use different configuration files or settings.
  4. Inspect the document structure. Look for an unusually large table, repeated markup, or elaborate CSS. These may make parsing and layout expensive even if you raise the PCRE limit.
  5. Check version compatibility. Compare your PHP version with the support information for the installed mPDF release before changing several variables at once.

Fix 1: split the HTML and call WriteHTML() repeatedly

Chunking is mPDF’s documented alternative to increasing the PCRE limit and is usually the best first change. Split the content where its structure permits—between records, table groups, or other complete blocks—rather than cutting an arbitrary string at a character count. An arbitrary cut can leave an open tag, divide an entity, or separate a table row from its table.

Keep document structure and styles consistent

Initialize the PDF once, write shared document content and styles as appropriate for your mPDF version, then pass each content chunk to WriteHTML(). A basic pattern is:

<?php
require_once __DIR__ . '/vendor/autoload.php';

$mpdf = new MpdfMpdf();

// Write shared document setup and styles in the mode appropriate
// to your mPDF version and existing application.
$mpdf->WriteHTML($css, MpdfHTMLParserMode::HEADER_CSS);

foreach ($htmlChunks as $chunk) {
    $mpdf->WriteHTML($chunk, MpdfHTMLParserMode::HTML_BODY);
}

$mpdf->Output('report.pdf', MpdfOutputDestination::FILE);

This is a pattern, not a drop-in replacement for every application: the example assumes $css and $htmlChunks have already been created, and parser-mode constants and output APIs must match the mPDF version installed. If your current code uses different modes, preserve its intended CSS/body handling while dividing the large body input.

For table-heavy output, a chunk should generally contain complete rows and the surrounding table structure needed by your document. Test page breaks, repeated headers, and styling across chunk boundaries; writing multiple strings can change how a document behaves if markup or layout assumptions cross those boundaries.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Fix 2: raise the limit cautiously when you control PHP

If chunking is impractical or does not resolve the error, mPDF’s guidance allows increasing the setting at runtime when the system permits. For example:

ini_set('pcre.backtrack_limit', '2000000');

The value 2000000 is an example of a bounded increase, not a universal recommendation or guaranteed fix. Choose a value for your deployment, then test with realistic documents and concurrent workload. PHP warns that very high values can consume process stack and crash PHP. Increasing the limit can also make a costly input run longer or use more memory; it does not simplify a large table or repair problematic markup.

If the setting does not take effect

  • The host may disallow runtime changes, or another configuration layer may control the value.
  • You may be checking CLI PHP while the PDF is generated by PHP-FPM or a web-server runtime with different settings.
  • The failure may have moved to another constraint, such as memory, processing time, or regex compilation.

Ask the hosting administrator which PHP configuration applies to the process that generates the PDF if you cannot change or verify the setting yourself.

Reduce table and layout costs

Passing the PCRE check does not guarantee that a large document will render efficiently. The mPDF memory and performance guidance identifies long documents and large tables as areas where smaller chunks and simpler layouts can help.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • Simplify table borders and styling where the design allows. Complex borders and padding can make table layout more expensive.
  • Consider simpleTables only when its trade-offs fit. mPDF’s performance guidance suggests it when complex table borders are unnecessary. Check the rendered result against your visual requirements before enabling it.
  • Avoid expensive layout features that the output does not need. Keep the change focused and compare the PDF’s appearance and processing behavior.
  • Process long documents in smaller pieces rather than building one exceptionally large HTML string, while preserving valid structure and intended page-break behavior.

These optimizations address layout and resource pressure; they are not substitutes for fixing malformed HTML or resolving a configuration error.

Use a repair sequence that isolates the cause

  1. Record which WriteHTML() input fails and its approximate size.
  2. Split that input at complete structural boundaries, then test the output and page breaks.
  3. If the error remains and you control the runtime, test a bounded increase to pcre.backtrack_limit.
  4. For tables, simplify borders or layout where acceptable; test simpleTables only if its rendering trade-offs are suitable.
  5. Check mPDF/PHP compatibility and update mPDF where practical.
  6. Retest with representative documents and the concurrency expected in production. Monitor failures and resource use rather than assuming the larger limit is safe.

Troubleshooting after the change

The same HTML-size error still appears

Verify that the failing string was actually divided and that the process sees the intended PCRE setting. Log the size of each input after chunking. If the HTML is being assembled elsewhere into one large string before a single call, splitting a different part of the workflow will not help.

The error changes to a regex compilation error

Do not keep increasing the numeric limit automatically. An mPDF issue records cases where increasing the setting did not resolve an underlying compilation problem. Inspect the HTML and CSS structure, and reduce or isolate complex styles to identify the input that triggers the new error.

The PDF now fails with a memory or timeout error

The PCRE exception may have been only the first bottleneck. Revisit chunk sizes, document length, table complexity, and runtime resource limits. The mPDF memory guidance discusses chunking long documents and table-related mitigations; there is no single chunk size or memory setting established for every server.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The output looks different after chunking

Check whether a chunk boundary splits a table or other structure, whether styles are available where each body chunk is parsed, and whether page-break behavior depends on content in adjacent chunks. Use complete logical units and compare the generated PDF, not just whether the exception disappeared.

Your PHP and mPDF versions may not match

Consult the official mPDF repository’s PHP-support information for the release you use. Do not assume guidance for a different mPDF release applies unchanged to your installation.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Or skip the browser setup

If the HTML you need to inspect is a live website, a screenshot can help you examine its rendered appearance, but it does not generate an mPDF document or fix an mPDF parsing error. ScreenshotNeo is a website screenshot API and MCP server; for the separate task of capturing a webpage, one request can return an image or PDF. See the ScreenshotNeo documentation.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
  • It accepts cookie/consent banners and removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each step can be turned off.
  • Bot checks/CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed; response headers report the page verdict and billing status.
  • An MCP server provides take_screenshot, get_page_info, and capture_pdf tools for AI agents and MCP clients.
  • The Free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 shots.

Sign up for ScreenshotNeo’s free plan to get 1,000 screenshots a month with no card.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Frequently Asked Questions

What should I set pcre.backtrack_limit to?

There is no universally safe value. Test a bounded increase in the PHP runtime that generates the PDF; very high values can consume process stack and crash PHP.

Does this error mean the PDF file is too large?

Not necessarily. It indicates that PCRE processing reached its configured limit while mPDF parsed an HTML or CSS input string. File size, memory, and processing time are separate concerns.

Can ScreenshotNeo fix an mPDF parsing error?

No. It captures live webpages as images or PDFs; it does not change mPDF’s PHP parsing limits or repair HTML passed to mPDF.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.