Display PDF file in storage in Laravel

We know that a pdf file can be downloaded from the browser. In this session, we will discuss how to view a file in the browser.

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.

$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, this will cause the browser to automatically download the file.