PDFs in Amazon S3 don't open in Chrome for view
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.
Yugal Kishor Bais
Updated on June 21, 2022Comments
-
Yugal Kishor Bais almost 2 years
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 hereBut below file is opening for viewing in chrome Please check this
-
RiggsFolly over 7 yearsLook its an anchor tag that is being used
-
Yugal Kishor Bais over 7 years@Dekel PDF link is created in index page and it is simple html anchor tag. how do I put content-type for that particular link.
-
Dekel over 7 yearsThis is not related to the html - the header is something that the server is sending.
-
RiggsFolly over 7 yearsNow that I like much better UV, and I learnt something about S3 too, thanks
-
Dekel over 7 yearsWill appreciate to know from the down-voter why did I get a down vote on the answer!
-
RiggsFolly over 7 yearsProbably @AjayThakur being spiteful because someone downvoted his answer, and it probably was not even you that did it
-
Dekel over 7 yearsWasn't me. @AjayThakur - if the downvote was yours I would appreciate an explanation here.