Laravel - display a PDF file in storage without forcing download?

Laravel - display a PDF file in storage without forcing download?

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

Stefan Bogdanescu

Founder & Senior Architect

Serving Stored PDFs Without Forcing Downloads in Laravel

When handling file storage in Laravel, the default behavior for accessing assets often leans toward downloading them rather than displaying them inline. If you have a PDF residing in storage/app, using Response::download() works perfectly well for generating a download link or triggering an immediate save to the client's disk. However, this approach forces the browser to treat the file as an attachment, bypassing the native previewer capabilities of modern browsers like Google Chrome.

To allow authenticated users to view a PDF directly in the browser window without triggering a download dialog, you need to serve the file with specific HTTP headers that signal to the client that it should be rendered inline. The core mechanism involves using Response::make() instead of Response::download() and explicitly setting the Content-Type header to application/pdf while adjusting the disposition strategy.

You can achieve this by returning a response with the file path as the source and configuring the headers appropriately:

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;

public function viewPdf(Request $request)
{
    $filePath = public_path('storage/app/example.pdf');
    
    return Response::make(
        file_get_contents($filePath),
        200,
        [
            'Content-Type' => 'application/pdf',
            'Content-Disposition' => 'inline; filename="example.pdf"',
        ]
    );
}

The critical header here is Content-Disposition. When set to inline, it instructs the browser to display the file if it supports PDF rendering natively. If the browser does not support inline viewing (such as mobile Safari on iOS, which historically treats PDFs differently), the browser will fall back to prompting a download or opening an external app. This fallback behavior is standard and acceptable; you cannot force a browser to render a PDF type it doesn't support.

Another consideration is the file storage location. If your PDFs are stored in storage/app, ensure that this directory is served by your web server, typically configured via .htaccess for Apache or nginx.conf for Nginx. If you are using Laravel's built-in development server (php artisan serve), it will automatically serve the contents of the public directory, but production environments require explicit configuration to map the storage link correctly.

Security is another dimension that cannot be overlooked. Serving files directly exposes them to potential access control issues if not gated properly. Since you mentioned restricted access for authenticated users, ensure your route is protected by middleware before returning the file content. You might also consider checking the MIME type of the file server-side to prevent serving unexpected file types with the application/pdf header.

Additionally, be aware that large PDF files can strain memory if read entirely into RAM using file_get_contents(). For massive documents, consider streaming the file directly from the disk stream instead of loading it all at once:

return Response::stream($filePath, 200, [
    'Content-Type' => 'application/pdf',
    'Content-Disposition' => 'inline; filename="example.pdf"',
]);

Streaming is generally more efficient and prevents memory spikes on the server. It also handles very large files better than loading them into memory first.

While some developers might look for shortcuts or third-party packages to simplify this, Laravel’s core response helpers provide everything needed when configured correctly. The decision to use inline versus attachment ultimately depends on your user experience goals: do you want users to review the document in place or save it immediately? By controlling the headers, you give users the flexibility to choose how they interact with your stored content.

Our network