1

if I clicked a link with url : website.com/open/pdf/Document_One_Drag_3.pdf, it will open the pdf in the browser instead of download. This is my code in Route.php

Route::get('/open/pdf/{filename}', function($filename)
{
    // Check if file exists in app/storage/file folder
    $file_path = public_path('uploads/docs/'.$filename);
    if (file_exists($file_path))
    {
        return Response::make(file_get_contents($file_path), 200, [
            'Content-Type' => 'application/pdf',
            'Content-Disposition' => 'inline; '.$filename,
        ]);
    }
    else
    {
        // Error
        exit('Requested file does not exist on our server!');
    }
});

the PDF file still downloading and not opened in the browser. What is wrong ?

Hendrik Eka
  • 145
  • 3
  • 14

1 Answers1

1

Considering Laravel 5.2, The download method may be used to generate a response that forces the user's browser to download the file at the given path.

 $pathToFile = public_path(). "/download/fileName.pdf";
 return response()->download($pathToFile);

The download method accepts a file name as the second argument to the method, which will determine the file name that is seen by the user downloading the file. Finally, you may pass an array of HTTP headers as the third argument to the method:

 return response()->download($pathToFile, $name, $headers);

So, you may use

    $headers = ['Content-Type' => 'application/pdf'];

as the third parameter.

This information might be useful too:

Note: Symfony HttpFoundation, which manages file downloads, requires the file being downloaded to have an ASCII file name.

L5.2 Documentation

To open in browser

<a  href="{{url('/your/path/to/pdf/')}}" target="_blank" >My PDf </a>
VipindasKS
  • 516
  • 7
  • 15
  • So if I wanted to open the file I should use 'return response()->file($pathToFile, $headers);' right ? and the result is the pdf file still downloading. I don't know what is wrong. – Hendrik Eka Apr 25 '16 at 08:47
  • Are you not trying to download file? Or, just want to see the pdf file open in browser tab? All your code you have written is for downloading. – VipindasKS Apr 25 '16 at 08:52
  • I want to view the pdf in browser not download it. – Hendrik Eka Apr 25 '16 at 08:56
  • that doesn't work too.. All the code that I've try is not working on my server but working on my localhost .. is it the server or what ?? – Hendrik Eka Apr 25 '16 at 09:11
  • Can you show us the link generated? Also, please check there is a file in that path and/or there are enough permissions set – VipindasKS Apr 25 '16 at 09:17