1

I have a source URL that displays the pdf document in the browser. It supposes to download automatically.

Sample URL : http://doc.local.com/storage/temporary/2020-02-19/c3e175b4706492be8c0d90e497c86ea7/test.pdf

Exactly I want to implement how amazon s3 temporary url works (they added all the headers in the url)

response-content-disposition=attachment

Please don't suggest me to use the following code, as front-end expect an URL

header("Content-Length: " . filesize($file));
header("Content-Type: application/octet-stream;");
readfile($file);
Al Foиce ѫ
  • 4,195
  • 12
  • 39
  • 49
Albert
  • 174
  • 4
  • 16
  • What's your current code? add your controller code please. – Hafez Divandari Feb 19 '20 at 13:01
  • If you want the direct URL to the .pdf file to return the relevant headers when the user visits it, this would be something you'd have to configure in your webserver, nothing to do with PHP - because as you've made clear, PHP's responsibility is only to return the correct URL, not the file itself. The download operation is done between the browser and the webserver, without any execution of a PHP script. (N.B. Even then, setting this header is only an indication to the user's browser as to how to handle the file - it isn't obliged to obey.) – ADyson Feb 19 '20 at 13:07
  • There are several classes involved in the process. so giving you the how the url got generated. $filePath = 'temporary/2020-02-19/aaaaa/test.pdf' return Storage::disk($disk)->url($filePath); – Albert Feb 19 '20 at 13:08
  • @AlbertRaj You should add headers when handling file download not when generating the URL. Show us a code that handles the file response. – Hafez Divandari Feb 19 '20 at 13:13
  • @HafezDivandari If I have to handle this on the back end, I could have done it in response(), Here it's an API that expects the URL on success or message on failure. So front end (Angular ) redirects the given URL, which works when I give the s3 temporary URL where as I need the same behaviour in local storage file url. "return Storage::disk($disk)->temporaryUrl( $path, now()->addMinutes(5),["ResponseContentDisposition" => "attachment;"] );" – Albert Feb 19 '20 at 13:22
  • 1
    The way the URL got generated is irrelevant. URLs don't contain headers. HTTP responses contain headers. The content-disposition header needs to be sent bu the server when the user actually visits the URL of the file - i.e. at the moment when the download actually happens (not when you provide them with a URL to it..that is a separate response). Since your sample URL ends with `.pdf` I'd assume that's a direct link to the file, served directly by the webserver. Is that correct? If so then as i said, you cannot change the headers it returns by doing anything in your PHP. – ADyson Feb 19 '20 at 13:25
  • @ADyson I will change my question as below. How to generate the downloadable URL for the files stored in Laravel local – Albert Feb 19 '20 at 13:31
  • Please refer to this link: https://stackoverflow.com/questions/28465434/laravel-5-force-download-a-pdf-file – Kevin Patel Feb 19 '20 at 13:32
  • @AlbertRaj you mean you want to serve the files via PHP/Laravel rather than via a URL which points directly to the file? I think that's a separate and rather different question. If so please create a new post about it (having done some research of course...because other people will probably have implemented a similar thing before, and you could likely re-use that code, or maybe even use a plugin or something...it's a common requirement). Or, as I said earlier, you could simply re-configure your webserver to change how it handles these PDF files, and get it to inject the right header. – ADyson Feb 19 '20 at 14:00

3 Answers3

2

I have a little hack to achieve the task -

Let's assume you URL is something -

https://somedomain.com/somepath/file.pdf

Create an api as below -

https://somedomain.com/somepath/file.pdf?signature=signature&ttl=some-ttl
  • signature to validate the request it is authentic or not
  • ttl is to validate if the URL is still active for that signature

Then after validating the API request, you can return the download response as below -

public function getDownload()
{
    //PDF file is stored under project/public/download/info.pdf
    $file= public_path(). "/download/info.pdf";

    $headers = array(
              'Content-Type: application/pdf',
            );

    return Response::download($file, 'filename.pdf', $headers);
}

ref - Download files in laravel using Response::download

Keshari Nandan
  • 1,040
  • 9
  • 22
1

A simple solution from the front end.

If the front end has a href and triggers it, this code will work for sure.

I tested in PHP itself, here is the code.

$url = 'http://doc.local.com/storage/temporary/2020-02-19/c3e175b4706492be8c0d90e497c86ea7/test.pdf';
echo '<a download href="'. $url .'" target="_blank">click</a>';

Incase the front end is redirecting the url instead of trigger, have to find out the solution.

Albert
  • 174
  • 4
  • 16
-3

I believe this is a function of your browser instead of the actual code. Most modern browsers have a function to automatically open up PDF's inside a browser window. This setting is specific to your browser.

Google Chrome:

Settings -> Site Settings -> PDF Documents

  • Yes...but a suitable content-disposition header in the response containing the file is usually interpreted by the browser as an instruction to ignore the PDF plugin and just download the file to disk instead. The point of OP's question is about how/where to inject that header into the correct HTTP response. – ADyson Feb 19 '20 at 13:23