10

I have PDF file for download by users on my website.

Is there a way to instruct Chrome not to view the PDF in the users browser when the user clicks the link to the PDF, but instead download the PDF to the users download folder instead? Or at least instruct Chrome to ask the user whether he wants to view the PDF in the browser or download it (rather than just viewing in browser automatically) ?

Thanks :)

rassom
  • 753
  • 2
  • 8
  • 16
  • Thanks :) Tried the top rated answer here, but it doesn't work :( https://wordpress.stackexchange.com/questions/5419/linking-a-pdf-as-a-downloadable-document – rassom Jan 24 '15 at 08:34
  • 1
    I think that will not work for chrome because chrome detect document as pdf before your htaccess load and interact with browser. so chrome will load it as PDF and do not access it as download action. –  Jan 24 '15 at 08:38
  • Maybe some javascript that finds the link through a specific CSS ID could be added and then adds a download attribute to it ref http://www.w3schools.com/TAgs/att_a_download.asp. If someone here knows enough javascript to do it, help would be appreciated, – rassom Jan 24 '15 at 08:52
  • See http://superuser.com/questions/199382/make-chrome-always-open-pdfs-itself and the other questions linked there – golimar Jan 24 '15 at 09:24
  • Found this solution that I just need to test now :) – rassom Jan 24 '15 at 09:30
  • 1
    @rassom, which web sever are you using? The `Content-Disposition` header mentioned in the link in the first comment is the right way to do this. – heavyd Jan 24 '15 at 09:54
  • I'm thinking it's nginx (http://wpengine.com/2013/07/08/nginx-overtakes-apache-as-the-server-of-choice-for-the-top-1000-trafficked-sites/) as I'm using Wpengine but don't know for sure. – rassom Jan 24 '15 at 11:54
  • 1
    The Content-Disposition in .htaccess doesn't work, tried it already. – rassom Jan 24 '15 at 11:54
  • What server-side language are you using? `Content-Disposition` is certainly the way to go (as already mentioned), but that solution on WordPress/.htaccess is not necessarily correct. The `Content-Type` should not be set to `application/octet-stream`, in an attempt to _fool_ the browser. (Not sure why this question was migrated from Webmasters, it is programming related, entirely to do with the HTTP response headers as sent from the server.) – MrWhite Jan 24 '15 at 12:01
  • "I'm thinking it's nginx" - .htaccess is an Apache feature, nginx has its own syntax. – MrWhite Jan 24 '15 at 12:15
  • Isnt it your browser settings? Dont you have Adobe Acrobat? AFAIK its a problem of the plugins at the user side (I mean client side) ... – TechLife Jan 24 '15 at 19:43
  • The following thread provides four methods in addition to HTML5's new download attribute. https://stackoverflow.com/questions/11620698/how-to-trigger-a-file-download-when-clicking-an-html-button-or-javascript –  Jan 25 '15 at 07:18

3 Answers3

11

You could use the download property in your link (anchor) like so:

<a href="some/path/somefile.pdf" download target="_blank">download PDF file</a>

This would force download in the browsers, which support it (Chrome, Firefox and Opera). However, it is not currently supported in IE or Safari as per this link

Source: https://stackoverflow.com/questions/3802510/force-to-open-save-as-popup-open-at-text-link-click-for-pdf-in-html

Kristian
  • 3,102
  • 13
  • 21
  • 1
    Documentation for the attribute because it has some cool features. It's too bad IE is stubborn. http://www.w3schools.com/tags/att_a_download.asp –  Jan 25 '15 at 07:18
  • It is also so bad Chrome limits the attribute for same domain and same port :-( however they let you download the file via "Save as" – user1156544 Aug 13 '18 at 17:08
2

I am not sure whether it is possible through custom code in website. As far as I know it is totally depends on user setting in their chrome browser.

Basically when you type : chrome://plugins/ , It will list you chrome plugins and If user disable plugin called : Chrome PDF Viewer , then only chrome will ask to download PDF otherwise PDF will open there automatically.

  • Thanks but doesn't answer the question about how I, as a webmaster, can make sure that is the behaviour for users who haven't set this Chrome preferences setting. But nice of you to answer :-) – rassom Jan 24 '15 at 21:05
1

It can be done simply by instead of clicking on the PDF file, right-click on it and select "save link as" and then chrome will start downloading the PDF file and make sure to check the file-format before saving the link address, generally it would be .pdf but if not then manually write .pdf after it.

Chirag
  • 55
  • 1
  • 2
  • 8
  • Thanks but doesn't answer the question about how I, as a webmaster, can make sure that is the behaviour for users who haven't set this Chrome preferences setting. But nice of you to answer :-) – rassom Jan 24 '15 at 21:05