Skip to main content
PHP Blog

Back to all posts

How to View Pdf Without Downloading In Laravel?

Published on
7 min read
How to View Pdf Without Downloading In Laravel? image

Best PDF Viewing Solutions for Laravel to Buy in October 2025

+
ONE MORE?

To view a PDF without downloading it in Laravel, you can utilize the Laravel Response class to stream the PDF file directly to the browser instead of downloading it. This can be achieved by setting the appropriate headers in the response before returning the PDF file.

You can write a controller method that fetches the PDF file and returns a response with the MIME type set to 'application/pdf' and the content disposition set to 'inline'. This will prompt the browser to display the PDF file directly in the browser window rather than initiating a download.

By setting the headers correctly and returning the PDF file as a stream in the response, you can allow users to view the PDF content without having to download the file to their local device. This can provide a more seamless and user-friendly experience for viewing PDFs on your Laravel application.

How do I show a PDF file in the browser without downloading it in Laravel?

To show a PDF file in the browser without downloading it in Laravel, you can use the following approach:

  1. Firstly, install the barryvdh/laravel-dompdf package in your Laravel application by running the following command:

composer require barryvdh/laravel-dompdf

  1. Next, create a route in your web.php file that points to a controller method that will handle the PDF file display:

Route::get('/show-pdf', 'PDFController@showPDF');

  1. Create a controller named PDFController using the following command:

php artisan make:controller PDFController

  1. In the PDFController, add the showPDF method that will generate and display the PDF file in the browser: