views:

186

answers:

5

When a user clicks a "Download PDF" link, I would like for the download prompt to appear and for the user to be able to download the file.

Currently, the user is just transferred to the address of the PDF file.

For example:

<a..[what goes here??]..>Download PDF</a>

It seems that there's a combination of JavaScript & PHP needed to do this.

Can anyone give an example?

A: 

It's not clear what you mean by

"Currently, the user is just transferred to the address of the PDF file."

But I suggest a simple approach:

<a href='http://example.com/download.pdf'&gt;Download PDF<a>

The choice of downloading or opening a file is more a setting of the browser.

pavium
A: 

You can force the browser to save the linked content by adding an HTTP header to the web server response:

Content-Disposition: attachment; filename=<default filename to save as>

On the other side, I don't really see the point in overriding the user's browser configuration, which usually tells if PDF documents should per default be opened in the browser, opened in a separate PDF viewer or saved to disk.

jarnbjo
While I agree, in general, that the user's preference should be honoured, I can think of at least one case where they should be encouraged to download the file, rather than view it in the browser: situations where they probably ought to save a copy, such as on-line insurance policies, or similar.
Roger Lipscombe
A: 

I don't believe there is a magical javascript/PHP solution to your problem, the vanilla HTML:

<a href="docco.pdf" title="this is a link to a pdf">download the PDF</a>

will do what is required, it will direct the browser to request the resource "docco.pdf", it is up to the browser (and any plugins) to do something with this.

For example, there is a firefox addon "PDF download" that offers the user the choice of what to do with a popup.

wiifm
+5  A: 

Redirect to a PHP page with this code on it:

<?php
header('Content-disposition: attachment; filename=movie.mpg');
header('Content-type: video/mpeg');
readfile('movie.mpg');
?>

your <a href code will need to point to a specific page, and readfile will call your resource

Further reading

Just as a side note, I do agree that you should not override a browsers settings, but sometimes when the boss asks, you just have to do.

andrewWinn
+2  A: 

If you're on Apache, you can drop this into your .htaccess:

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

This will send all PDF documents as downloads. mod_headers has to be enabled though.

middus
That's just what I was looking for, thanks!
You're welcome. :)
middus