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 bination of JavaScript & PHP needed to do this.
Can anyone give an example?
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 bination of JavaScript & PHP needed to do this.
Can anyone give an example?
Share Improve this question asked Oct 2, 2009 at 11:47 edtedt 22.4k32 gold badges85 silver badges119 bronze badges7 Answers
Reset to default 8Redirect 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.
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.
You can also use the following code:
<?php
$file = filter_input(INPUT_GET, 'file');
$filename = basename($file);
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=\"{$filename}\";");
readfile("your file path {$file}");
?>
This will prompt any file extensions such as (.pdf,.docx,.zip,.csv,.txt,.html,.php and so on...) without directly opening into the new browser.
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.
You can use the HTML5 download
attribute on your a tag.
<a href="http://love4cats./kitten-catalogue.pdf" download>Download the CATalogue</a>
You should check your analytics and make sure that your target browser supports the attribute. See the caniuse. entry page for browser support.
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.
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./download.pdf'>Download PDF</a>
The choice of downloading or opening a file is more a setting of the browser.