Failed to load PDF document in chrome browser

Failed to load PDF document in chrome browser

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Failed to Load PDF in Chrome: Debugging mPDF Output Issues

You're likely encountering this issue because of how different browsers handle binary data streams and HTTP headers differently. While Firefox is known for being forgiving with certain header configurations, Chrome enforces stricter validation on the Content-Disposition and Content-Type headers when trying to render inline PDFs directly in the browser.

The Root Cause: Header Configuration

The error you're seeing—specifically the "Failed to load resource" message—is almost always caused by a mismatch between what the server sends and what Chrome expects for an inline display. In your code, you're setting Content-Disposition: inline, which tells the browser to render the PDF within a new tab rather than downloading it immediately. However, there are subtle details in how these headers interact that can break this behavior in Chrome but work fine in Firefox.

One critical issue is the use of Accept-Ranges: bytes. While this header is useful for enabling byte-range requests (useful for resumable downloads or partial content loading), it's not required for simple inline rendering and can sometimes interfere with Chrome’s PDF viewer logic when combined with other headers. Removing it often resolves compatibility issues without affecting functionality in most cases.

Additionally, ensure that your Content-Type header is exactly application/pdf. Some variations like application/x-pdf or missing trailing spaces can cause Chrome to reject the file outright.

mPDF and Font Handling

Another common pitfall with mPDF is font handling. You've already set $mpdf->useOnlyCoreFonts = false, which allows you to use external fonts. However, if those fonts aren't properly registered or encoded correctly, mPDF may generate a malformed PDF that Chrome refuses to parse.

Make sure all font files are valid and that the path to them is correct. Also, check whether any non-UTF-8 characters are being written without proper encoding. mPDF defaults to UTF-8, but mixing encodings can corrupt the internal structure of the generated PDF.

CSS Injection Considerations

You're injecting a stylesheet using:

$stylesheet = file_get_contents(APPPATH . 'third_party/mpdf/style.css');
$mpdf->WriteHTML($stylesheet,1);

This is generally fine, but be cautious about including browser-specific CSS rules (like @media queries or unsupported properties). mPDF has limited CSS support and may misinterpret certain styles, leading to rendering issues or invalid PDF structures. Stick to basic block-level elements, borders, margins, and colors unless you're using official mPDF-supported extensions.

Testing Without Inline Rendering

If fixing the headers doesn't resolve the issue, consider switching from inline display to download-only mode temporarily:

header('Content-Disposition: attachment; filename="' . $yourFileName . '"');

This forces Chrome to open the PDF in a new tab using its built-in viewer instead of trying to render it inline. It bypasses many of the stricter validation checks and helps confirm whether the problem is purely header-related or related to the PDF content itself.

If this works, gradually reintroduce inline rendering by adjusting headers one at a time to identify which combination Chrome accepts.

Final Check: Ensure Binary Encoding

Always set Content-Transfer-Encoding: binary when serving PDFs. However, ensure no additional output is sent before or after the PDF data. Even a single whitespace character can break the stream in Chrome. Use ob_clean() or ob_end_clean() as you've done, but double-check that no debug logs or error messages are being echoed inadvertently.

By carefully validating your headers, ensuring proper font and CSS handling, and testing with different output modes, you should be able to get consistent PDF rendering across both Chrome and Firefox using mPDF.

Our network