Joel Joel Binks Joel Joel Binks - 26 days ago 9
PHP Question

Laravel 5.1 - how to download pdf file from S3 bucket

I am using Laravel's Storage facade and I am able to upload the pdf to S3 and I am also able to get() its contents but I cannot display or download it to the end user as an actual pdf file. It just looks like raw data. Here is the code:

$file = Storage::disk($storageLocation)->get($urlToPDF);
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename='file.pdf'");
echo $file;


How can this be done? I have checked several articles (and SO) and none of them have worked for me.

Answer Source

I figured it out. Silly mistake. I had to remove the single quotes from filename.

Fix:

$file = Storage::disk($storageLocation)->get($urlToPDF);
header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=file.pdf");
echo $file;