How to display pdf in php

How to display pdf in php

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

Stefan Bogdanescu

Founder & Senior Architect

Why PHP Forces PDF Downloads and How to Fix It

When you embed an <iframe> pointing directly to a file like xml.pdf in a static HTML page, the browser treats it as a resource request. If the server sends the correct MIME type (application/pdf) and sets appropriate headers, modern browsers render the document inline by default. However, switching that same logic into a PHP environment often breaks this behavior unexpectedly, causing the browser to initiate a download instead of rendering the content.

The core issue isn't usually the HTML syntax itself; it is almost always how the server serves the file or how the PHP script handles the output before sending the PDF path to the client. When you place raw HTML with an src="xml.pdf" inside a PHP file, you must ensure that the final HTTP response reaching the browser contains the correct headers for a PDF viewer.

The Server Configuration Factor

In many cases, the problem stems from the web server configuration rather than the PHP code. If your web server (Apache or Nginx) is not explicitly configured to serve files with a .pdf extension as application/pdf, it might default to sending application/octet-stream. This generic binary stream header tells the browser that the content is unknown data and should be downloaded immediately.

You can verify this by inspecting the page source and looking for the Content-Type header. If you see application/octet-stream, the browser will force a download regardless of your HTML tags. To fix this, you may need to adjust your server configuration. For Apache, adding an .htaccess rule or modifying the virtual host config ensures the correct MIME type is sent:

AddType application/pdf .pdf

Without this step, even if your PHP script outputs valid HTML, the underlying file retrieval might be misidentified by the server.

Handling PHP Output Buffering

Another common pitfall occurs when the PHP file itself interferes with the HTTP headers. If you accidentally echo or print any text before attempting to set headers (though less likely in a pure view scenario), it breaks the stream. However, a more subtle issue arises if you attempt to read the PDF file content directly within PHP and embed it into the HTML without proper handling.

If your intention is to serve the PDF directly from a local path like xml.pdf, you should generally avoid reading the file content in PHP unless necessary. Instead, pass the file path directly to the browser via the src attribute, ensuring no output buffering interferes. If you must read the file into memory (for example, to sanitize it or modify headers dynamically), you must explicitly set the headers before echoing any content:

header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="xml.pdf"');
readfile('path/to/xml.pdf');
exit;

However, for a simple display scenario using an iframe, keeping it static is often better. Ensure your src attribute points to the correct relative or absolute path and that no whitespace or BOM exists before the opening <html> tag in your PHP-generated file.

Using Libraries for Complex Scenarios

If you need more control over how the PDF appears, such as embedding an external viewer like PDF.js, static HTML might not be enough. Libraries like dompdf allow you to generate PDFs from HTML templates, but they are designed for creating documents, not viewing existing ones. For viewing existing files, consider using a dedicated JavaScript library that can handle PDF rendering within an iframe or a container div.

You can also leverage the LaravelCompany ecosystem if you are building a Laravel application. While there is no built-in "display PDF" helper that automatically renders arbitrary local files as inline viewers without configuration, the framework makes it easy to set up middleware or routes to ensure correct MIME types are served consistently across your application. For instance, using a route like:

Route::get('/view-pdf/{filename}', function ($filename) {
    $path = storage_path('app/public/pdfs/' . $filename);
    return response()->file($path, [
        'Content-Type' => 'application/pdf',
        'Content-Disposition' => 'inline; filename="' . $filename . '"'
    ]);
});

This approach decouples the file path logic from the view, ensuring that whether accessed via URL or iframe, the browser receives the correct headers to display the document rather than download it.

Our network