Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

laravel

I have a PDF file stored in app/storage/, and I want authenticated users to be able to view this file. I know that I can make them download it using

return Response::download($path, $filename, $headers); 

but I was wondering if there is a way to make them view the file directly in the browser, for example when they are using Google Chrome with the built-in PDF viewer. Any help will be appreciated!

like image 297
Luis Aceituno Avatar asked Sep 19 '14 16:09

Luis Aceituno


People also ask

How can I open PDF without downloading in laravel?

As of Laravel 5.2 documented under Other response types you can now use the file helper to display a file in the user's browser. You just need to send the contents of the file to the browser and tell it the content type rather than tell the browser to download it. $filename = 'test.

How do I display PDF in browser instead of downloading?

At the top right, click More Settings. At the bottom, click Show advanced settings. Under “Privacy”, click Content settings. Under “PDF Documents," check the box next to "Open PDF files in the default PDF viewer application.” (Uncheck this box if you want PDFs to open automatically when you click them.)

How do I make a PDF not downloadable?

Making a PDF non-downloadable is as simple as adding #toolbar=0 to the URL.

How do I view a PDF in laravel?

In Laravel 5.2 you can use file helper to display a file in the user's browser. return response()->file($pathToFile); return response()->file($pathToFile, $headers); If the Laravel version is below 5.2, send the contents of the file to the browser and tell the browser the content type.


1 Answers

Update for 2017

As of Laravel 5.2 documented under Other response types you can now use the file helper to display a file in the user's browser.

return response()->file($pathToFile);  return response()->file($pathToFile, $headers); 

Source/thanks to below answer

Outdated answer from 2014

You just need to send the contents of the file to the browser and tell it the content type rather than tell the browser to download it.

$filename = 'test.pdf'; $path = storage_path($filename);  return Response::make(file_get_contents($path), 200, [     'Content-Type' => 'application/pdf',     'Content-Disposition' => 'inline; filename="'.$filename.'"' ]); 

If you use Response::download it automatically sets the Content-Disposition to attachment which causes the browser to download it. See this question for the differences between Content-Disposition inline and attachment.

Edit: As per the request in the comments, I should point out that you'd need to use Response at the beginning of your file in order to use the Facade.

use Response; 

Or the fully qualified namespace if Response isn't aliased to Illuminate's Response Facade.

like image 84
Ben Swinburne Avatar answered Oct 15 '22 02:10

Ben Swinburne