Chrome has "Failed to load PDF document" error message on inline PDFs

Chrome has "Failed to load PDF document" error message on inline PDFs

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

Stefan Bogdanescu

Founder & Senior Architect

Chrome has "Failed to load PDF document" error message on inline PDFs

When building a PHP application that serves documents, you often want to leverage the browser's native rendering capabilities rather than forcing a download. The goal is to display a PDF directly in the window if the user's browser supports it, or prompt a download otherwise. To achieve this, developers typically set the Content-Disposition header to inline.

However, a common frustration arises when users open Chrome and see a "Failed to load PDF document" error instead of the expected viewer. This behavior is inconsistent; some files render perfectly while others fail immediately, even on the same server with identical PHP configurations. The culprit is rarely file permissions or basic MIME type mismatches, as you might initially suspect.

The core issue lies in how Chrome handles external resources and its strict security model regarding inline PDFs. When you serve a PDF via readfile() with an inline disposition, Chrome attempts to render the document using its internal viewer engine. If the content stream is malformed, or if the server sends headers that conflict with Chrome's expectations of an embedded resource, the viewer will fail to initialize.

A critical detail often overlooked in the provided PHP code snippet is the handling of headers and output buffering. The sequence ob_clean(); flush(); readfile($path); suggests an attempt to ensure clean output, but it introduces a specific pitfall. If any whitespace exists before the first byte of the actual file content—such as BOM markers from the source file or accidental newlines in the script—the header negotiation can break down. While readfile() outputs the file content directly, Chrome's PDF viewer is sensitive to even minor discrepancies between the declared headers and the actual byte stream.

Furthermore, the Content-Disposition: inline directive tells the browser to treat the resource as an embedded object. Chrome has historically been stricter about validating the integrity of these inline resources compared to other browsers like Firefox or Safari. If the PDF file itself is corrupted, incomplete, or contains specific binary structures that trigger a security flag in Chrome's rendering engine, it will reject the document outright, displaying the error message regardless of your PHP headers being technically correct.

Another potential cause involves the server's response headers interfering with each other. In your example, you are setting Content-Length dynamically based on the file size. While this is standard practice, ensure that no other headers (like Transfer-Encoding) are set in a way that conflicts. The code sets Content-Transfer-Encoding: binary, which is generally safe, but if the web server software (like Apache or Nginx) has specific configurations for PDF handling, they might override or conflict with your PHP-generated headers.

To troubleshoot this effectively, you must isolate whether the issue is with the file content or the delivery mechanism. Try serving the same problematic PDF through a different script that outputs minimal headers and uses echo instead of readfile() to rule out buffering issues. Additionally, inspect the network tab in Chrome DevTools. If the status code is 200 but the viewer fails, the issue is almost certainly within the PDF's internal structure or a specific incompatibility with Chrome's current rendering engine.

If you are using a framework like Laravel, the Response facade simplifies this process and often handles header ordering more robustly than raw PHP headers. You can use response()->stream($path)->download($filename) but modify the disposition to inline if desired, ensuring the framework manages the output buffering correctly.

Ultimately, while inline is the correct semantic choice for displaying documents within a web page, Chrome's implementation of PDF viewing adds a layer of complexity that requires careful validation of both the file integrity and the exact byte-for-byte delivery of headers and content.

Our network