PDF for download, not for viewing / downloading

I try to do this when you click "download here", the pdf file will be downloaded to the user's desktop. How can I do it?

+2
source share
5 answers

Use the Content-Disposition header.

But please: do this only if the user really needs to download the PDF file, and not open it (so that his help simply does not allow the user to explicitly choose "save file"). Do not use it to force the user to download it, although he just wants to view it.

+9
source

code for a.php:

 <?php header('Content-type: application/pdf'); header('Content-Disposition: attachment; filename=file.pdf') echo file_get_contents("file.pdf"); ?> 

connect it in the network:

  <a href="a.php">Download</a> 
+3
source

What you are asking for is to control the behavior of the browser - it is not easy.

The best bet you have is to provide a link to what seems like a web page (i.e. download.php?file=1 ), and as soon as the browser sees its binary data, it will ask the user what he wants to do with it . This method is likely to do its job.

But note that this behavior cannot be guaranteed and is highly dependent on browser configuration.

0
source

This is a common problem, and few know about a simple HTML 5 solution:

 <a href="./directory/yourfile.pdf" download="newfilename">Download the pdf</a> 

Where newfilename is the suggested file name for saving the file. Or it will default to the file name on the server if you leave it empty, for example:

 <a href="./directory/yourfile.pdf" download>Download the pdf</a> 

Compatibility: I tested this on Firefox 21 and Iron, both worked fine. This may not work with HTML5-incompatible or outdated browsers. The only browser I tested that did not force download was IE ...

0
source

I think there is a <meta> tag for this. I think it was called "nosave". Do not use WWW for this - I searched and could not find it.

0
source

All Articles