Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDFs in Amazon S3 don't open in Chrome for view

I have a website which is developed in PHP and hosted in amazon server. PDF files that are uploaded in server are not opening for view in chrome browser but this pdf file is opening in other browser (internet explorer) for viewing. In chrome it is downloaded. I want this pdf to be open for viewing. code for link is

<a href="<?php echo $filename;?>" target="_blank"><?php echo $data['File_Label'];?></a>


URL: Please click here

But below file is opening for viewing in chrome Please check this

like image 578
Yugal Kishor Bais Avatar asked Oct 03 '16 12:10

Yugal Kishor Bais


1 Answers

If you want the browser to treat the file as pdf you should let him know that his file is a pdf. The way to do so is to send the relevant header:

Content-type: application/pdf

The current header that is sent by s3 is:

Content-type: application/octet-stream

Since you upload the files to s3 - you can either set the correct mime-type for the file during uploading or afterward, using some of the s3 tools (for example s3cmd for windows).

According to the link to the documentation you provided, the putObjectFile function is deprecated, but this is the definition:

putObjectFile (string $file, string $bucket, string $uri, [constant $acl = S3::ACL_PRIVATE], [array $metaHeaders = array()], [string $contentType = null])

As you can see - the last parameter is contentType - so you can set it to application/pdf when uploading the file.
This will set the correct header when downloading.

like image 152
Dekel Avatar answered Sep 19 '22 22:09

Dekel