view pdf without downloading using dompdf

view pdf without downloading using dompdf

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

Stefan Bogdanescu

Founder & Senior Architect

Why stream() Doesn't Always Trigger Inline Viewing in DomPDF

The snippet you are attempting relies on setting the Attachment parameter to false within the stream method:

$dompdf->stream('my.pdf', array('Attachment' => 0));

While this approach is technically correct regarding the MIME type and content disposition headers, it frequently fails to render PDFs inline in modern browsers. This failure isn't a bug in DomPDF itself; it is a result of how contemporary browser security models handle file downloads versus inline previews.

When you set Attachment to 0, you are instructing the server to send the header Content-Disposition: inline. However, this header alone does not guarantee that the browser will bypass its download manager or open the PDF in an embedded viewer. Many browsers, particularly Chrome and Edge on Windows, prioritize user safety by forcing a download for unknown file types or large files, ignoring the "inline" preference unless specific conditions are met.

To achieve consistent inline viewing, you must address three critical factors: the MIME type, the Content-Type header, and the browser's internal handling of PDFs.

Ensuring the Correct MIME Type

DomPDF defaults to generating a file with the MIME type application/pdf. While this is standard, explicitly setting it in your controller ensures there is no ambiguity, especially if DomPDF has been configured with custom settings that alter defaults.

You can enforce the correct content type by passing it directly to the stream method or by configuring the response object beforehand. When using the stream method, ensure you are not inadvertently overriding the disposition logic with a generic response setup.

$dompdf->loadHtml($html);
$dompdf->setPaper('A4', 'landscape');
$dompdf->render();

// Explicitly set the stream options to force inline viewing
$options = array(
    'Attachment' => 0, // This is equivalent to false
    'isRemoteEnabled' => true // Sometimes required for external resources
);
$dompdf->stream('my.pdf', $options);

The Browser Disposition Hierarchy

The primary reason your current implementation isn't working is likely due to the browser's "Download" behavior taking precedence over the inline directive. Modern browsers treat PDFs as a special case. Even with Content-Disposition: inline, if the user has previously saved that specific file or if the file size exceeds a certain threshold (often 1MB+), the browser may cache the decision to download rather than render.

To counteract this, you must ensure that no caching headers are interfering. DomPDF handles output buffering and headers internally when using stream(), but adding explicit headers to the response can reinforce the intent:

$response = new Response($dompdf->output(), 200, array(
    'Content-Type' => 'application/pdf',
    'Content-Disposition' => 'inline; filename="my.pdf"',
    'Cache-Control' => 'no-cache, no-store, must-revalidate',
));

return $response;

While DomPDF's stream() method returns a response object that you can send directly, explicitly controlling the headers helps override browser heuristics. The key here is the combination of inline in the disposition and no-cache directives to prevent the browser from serving a cached "downloaded" version of the file.

Alternative: Direct Output for Better Control

If switching to the response object feels too verbose, you can manually trigger the headers before outputting the content. This gives you granular control over how the browser interprets the stream.

$dompdf->loadHtml($html);
$dompdf->render();
$pdf = $dompdf->output();

// Set headers manually
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="my.pdf"');
header('Cache-Control: no-cache, must-revalidate');
header('Pragma: no-cache');

echo $pdf;

This method strips away any ambiguity regarding how the content is delivered. By manually setting the headers, you bypass the internal logic of DomPDF's stream() method regarding disposition, ensuring the browser receives exactly what you intend.

Conclusion on Browser Behavior

The core issue is that Attachment => 0 sets the disposition to inline, but it does not override the browser's default download behavior for PDFs. You must explicitly manage caching and ensure the Content-Type is strictly defined. If inline viewing still fails, consider that you may need to rely on the browser's native PDF viewer settings or provide a JavaScript prompt to open the file if automatic detection fails.

For projects requiring robust PDF generation within a Laravel ecosystem, tools like barryvdh/laravel-dompdf can simplify this further by integrating these header manipulations directly into the Blade views or controllers, though understanding the underlying HTTP headers remains essential for troubleshooting cross-browser inconsistencies.

Our network