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

10,767

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.

Share:
10,767
Yugal Kishor Bais
Author by

Yugal Kishor Bais

Updated on June 21, 2022

Comments

  • Yugal Kishor Bais
    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 here

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

  • RiggsFolly
    RiggsFolly over 7 years
    Look its an anchor tag that is being used
  • Yugal Kishor Bais
    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
    Dekel over 7 years
    This is not related to the html - the header is something that the server is sending.
  • RiggsFolly
    RiggsFolly over 7 years
    Now that I like much better UV, and I learnt something about S3 too, thanks
  • Dekel
    Dekel over 7 years
    Will appreciate to know from the down-voter why did I get a down vote on the answer!
  • RiggsFolly
    RiggsFolly over 7 years
    Probably @AjayThakur being spiteful because someone downvoted his answer, and it probably was not even you that did it
  • Dekel
    Dekel over 7 years
    Wasn't me. @AjayThakur - if the downvote was yours I would appreciate an explanation here.