0

I am working on the project wherein i have to keep option for pdf to be downloaded. When i click on download button[Image], it is directly opening the pdf file.

Instead of that i want "save as" dialog to be come which is comming only if file to be downloaded is in .doc or docx format.

Is there any other alternative for pdf format?

Thank You.

Freelancer
  • 9,008
  • 7
  • 42
  • 81

3 Answers3

3

HTML5 way:

Add the download attribute to your download links:

In some cases, resources are intended for later use rather than immediate viewing. To indicate that a resource is intended to be downloaded for use later, rather than immediately used, the download attribute can be specified on the a or area element that creates the hyperlink to that resource.

The attribute can furthermore be given a value, to specify the file name that user agents are to use when storing the resource in a file system. This value can be overridden by the Content-Disposition HTTP header's filename parameters. [RFC6266]

In cross-origin situations, the download attribute has to be combined with the Content-Disposition HTTP header, specifically with the attachment disposition type, to avoid the user being warned of possibly nefarious activity. (This is to protect users from being made to download sensitive personal or confidential information without their full understanding.)

This means you can write:

<a href="/path/to/file.pdf" download>Download the file</a>

<a href="/path/to/export-as-pdf.php?id=1" download="report.pdf">Download the file</a>

Old School:

You can tell the webserver to add the Content-Disposition: attachment header which causes (sane) browsers to pop the "Save As..." dialog. If you are using Apache and mod_headers is installed then add these lines in the .htaccess file:

<Files *.pdf>
    Header set Content-Disposition attachment
</Files>

This is discussed in more detail here:
How to send Content-Disposition headers in apache for files.

Community
  • 1
  • 1
Salman A
  • 262,204
  • 82
  • 430
  • 521
1

If you can modify HTTP headers, you should add the following when serving your PDF:

Content-Disposition: attachment;filename=xxxxxx.pdf

orique
  • 1,295
  • 1
  • 27
  • 36
  • I didnt understood. Can you please give me more idea related to this? – Freelancer Feb 28 '13 at 11:15
  • 2
    When you request any file using your browser, the browser first tries to find any plugin that can manage that file. In your case, when you request the PDF the browser finds your PDF plugin (Acrobat Reader most likely). When you request .doc or .docx, your browser does not find any plugin that can manage those files and shows you the "Save as" dialog. In order to override this browser behaviour, your server can send an HTTP header named `Content-Disposition` with value `attachment;filename=xxxxx.pdf` which forces the browser to show the "Save as" dialog. – orique Feb 28 '13 at 11:18
  • Are these HTTP Headers? – Freelancer Feb 28 '13 at 11:25
  • @freelancer No, these are meta tags. If you are using some server-side language like PHP, Python or Java you could add that HTTP header there. If you are with plain HTML files you probably need to modify your server configuration. – orique Feb 28 '13 at 11:33
  • ok. Its plain html site. what is happening is that pdf file is of 10MB and if any one doesnt want to read it then only , by clicking on download image it will consume 10MB space. – Freelancer Feb 28 '13 at 11:35
1

In Firefox

Tools -> Options -> General -> Select Always ask me where to save files

In Chrome

Open Settings -> Select Advanced Settings -> Check ask where to save each file before downloading

In Opera

Go to Opera Menu -> Settings -> Preferences -> -> Advanced Downloads -> Advanced -> Edit the mime type you want and select show download dialog

In IE

Open the Adobe Reader program -> Preferences -> Internet -> Untick Display PDF in Browser

Downloads

You can access your downloads folder from any browser with Ctrl+J if your not sure where the PDFs get saved to, any PDfs open in the browser will go to a temporary folder but the downloaded ones go to whatever downloaded directory you have selected.

HTML 5 Attribute workaround

You can use this HTML attribute to force the link, also jquery supports the same thing (credit to Austin)

<a href="path/to/file" download>Click here to download</a>
Ninja2k
  • 819
  • 3
  • 9
  • 34
  • sure, this i can do. but how can i tell these steps to my each client? and second question is bydefault it works for doc files but not for pdf files. how? – Freelancer Feb 28 '13 at 11:19
  • The default behaviour for most browsers is to open the PDF which the client can change however to make it save by force, this may help http://stackoverflow.com/questions/11353425/force-a-browser-to-save-file-as-after-clicking-link – Ninja2k Feb 28 '13 at 11:25
  • download is any attribute in this? – Freelancer Feb 28 '13 at 11:37
  • 1
    download is the attribute so just change path/to/file to your file location, it works for all MIME types. Take note this is a HTML5 attribute. – Ninja2k Feb 28 '13 at 11:40
  • Ok. Fine. I will try to use it. Thank You. – Freelancer Feb 28 '13 at 11:51
  • 1
    you can see an example of this in action set to auto download at http://jsfiddle.net/DerekL/uMwqc/2/ – Ninja2k Feb 28 '13 at 11:53
  • Is this the best editor for HTML5? I want to learn it. So please suggest me the best Editor for HTML5.http://www.kendoui.com/download.aspx – Freelancer Feb 28 '13 at 11:56
  • I am an old school coder I like to write in the likes of Sublime 2 and Notepad++ – Ninja2k Feb 28 '13 at 11:59
  • since i am new learner.. does it give me with auto suggest ? please guid me. – Freelancer Feb 28 '13 at 12:00
  • Sublime Text 2 does auto suggest for almost every language, you can download plugins for it, it takes a little getting used to but I love it and I am only a casual programmer. – Ninja2k Feb 28 '13 at 12:02