Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue with downloading PDF from S3 on Chrome

I'm facing an issue on downloading PDF files from Amazon S3 using Chrome.

When I click a link, my controller redirect the request to the file's URL on S3.

It works perfectly with Firefox, but nothing happens with Chrome.

Yet, if I perform a right click -> Save location as will download the file ...

And even a copy-paste of the S3 URL into Chrome will lead to a blank screen ...

Here is some information returned by curl:

Date: Wed, 01 Feb 2012 15:34:09 GMT
Last-Modified: Wed, 01 Feb 2012 04:45:24 GMT
Accept-Ranges: bytes
Content-Type: application/x-pdf
Content-Length: 50024
Server: AmazonS3

My guesses are related to an issue with the content type ... but all I tried didn't work.

like image 387
Arkan Avatar asked Feb 01 '12 15:02

Arkan


2 Answers

The canonical Internet media type for a PDF document is actually application/pdf as defined in The application/pdf Media Type (RFC 3778) - please note that application/x-pdf, while commonly encountered and listed as a media type in Portable Document Format as well, is notably absent from the official Application Media Types listed by the Internet Assigned Numbers Authority (IANA).

I'm not aware of why and when application/x-pdf came to life, but apparently the Chrome PDF Plugin does not open application/x-pdf documents as of today.

Consequently you should be able to trigger a different behavior in Chrome by changing the media type of the stored objects accordingly.

Alternative (for authenticated requests)

Another approach would be to Force a PDF to download instead of letting Chrome attempt to open it, which can be done by means of triggering the Content-Diposition: attachment header with your GET request - please see the S3 documentation for GET Object on how to achieve this via the response-content-disposition request parameter, specifically response-content-disposition=attachment as demonstrated there in section Sample Request with Parameters Altering Response Header Values.

This is only available for authenticated requests though, see section Request Parameters:

Note

You must sign the request, either using an Authorization header or a Pre-signed URL, when using these parameters. They can not be used with an unsigned (anonymous) request.

like image 189
Steffen Opel Avatar answered Sep 28 '22 04:09

Steffen Opel


There is an html based solution to this. Since chrome is up to date with HTML5, we can use the shiny new download attribute!

<a href="http://www.domain.com/painful.pdf">Broken</a>
<a href="http://www.domain.com/painful.pdf" download="notsopainful">Works</a>

like image 43
InsanelyADHD Avatar answered Sep 28 '22 04:09

InsanelyADHD