View Full Version : How to make PDF files available for download
Memoire
03-21-2010, 06:07 AM
Many sites I visit I can simply click on a link and download a pdf.
How do I put this option on my site?
At the moment I have zero knowledge about this; I don't know where to put the pdf file on the server and how to create the link that will download this.
Any help you can give would be most welcome.
Thank you
Steven
digital29
03-21-2010, 12:49 PM
You can just upload the pdf file to your site, and link to it..
The download windows will appear automatically.
You can just upload the pdf file to your site, and link to it..
The download windows will appear automatically.
Actually, in many cases, the browser will attempt to display the pdf file in the browser instead of downloading it.
If you want to force all pdf files to be downloaded, there are two common methods. The first, assuming you are using Apache and have the ability to edit .htaccess directives, involves adding the following line to your .htaccess file in the root folder of your site:
AddType application/octet-stream .pdfThis line will force the file to be downloaded.
I should mention though, that is a bit of a hack, and the following code should work better:
<FilesMatch "\.(?i:doc|odf|pdf|rtf|txt)$">
Header set Content-Disposition attachment
</FilesMatch> This code will also work for a few other types of files, as you can probably see from the first line.
Another option, if you are using PHP to retrieve the file, is to create a script that will output the proper headers to force the file to be downloaded. See the notes here for a sample: PHP: header - Manual (http://php.net/manual/en/function.header.php)
DaveSawers
03-22-2010, 04:06 PM
Even if the file is displayed directly in the browser, it is still "downloaded" to your computer. The only thing is that if you want to save a permanent copy of that file you need to use the browsers "Save As..." menu option or click the save icon on the PDF toolbar.
puamana
03-22-2010, 05:17 PM
You might also want to target the pdf link to a new window (target="_blank") so that the original page
linked from isn't replaced. The pdf will open in a new window and can be read by the visitor, or click on the 'disk' link to save a copy to your computer.
If the visitor decides NOT to 'save' the pdf document, they can simply close that window or tab and still be at the originating page.
- Puamana
Clint1
03-22-2010, 11:07 PM
Since Steven said he has "zero knowledge about this", no one has addressed the simplest of all and what he may be asking; the simple href link.
To learn how others do things, just right click a bare spot of a webpage and "View source" (or similar in non IE browsers) and you can see how anything is done. Search the text file for the text you're looking for and you can see the code. To answer your question directly:
<a href="http://www.stevenredhead.com/downloads/File-Name.pdf" title="More of the file name" target="_blank">Descriptive link text</a>
The blue areas are optional:
title="More of the file name" just gives you some nice hover text that may help with the blind or screen readers, and puts more descriptive words for the link out there for the SE's.
target="_blank" as Puamana mentioned, is nice for large files of any kind and allows the file to downloaded in the background in another page while a visitor can still browse your site.
www. - Your site can be accessed with and without the www prefix. Generally not a good thing, but it is at least better than one version returning a "The page cannot be displayed". A highly debated and discussed topic, the general consensus (with G) is that they see each version as a different page, therefore duplicate content, therefore another reason for them to possibly penalize you for it. It can also dilute PR.
(As Wige touched on), Most are on Apache/Linux servers, and that can be easily remedied on them by something called a "301 redirect", "301" meaning permanent (as opposed to a 302 which is temporary), which is a simply line of code in a file called .htaccess (it actually has no name, that's the extension of the file, and it most times is hidden if you have it, so you have to ask your hosts about it). The line of code, by your choice, will direct all requests, both www and non-www, to a version of your choosing. Then, regardless of how your URL is displayed, it will always go to the version that you choose. This will make any links to your site, regardless of how others type them, all go to the same version. If you need that code, most anyone around here can give it to you, just ask. It's not mandatory to do this redirect, but it is a good idea.
As for "downloading" or "reading" the PDF file, as "digital29", Dave and Wige brought up: the file is always "downloaded" as soon as its link is clicked, even if you don't download it. It goes to your temporary internet files folder, which is generally emptied (should be) regularly, so the file is deleted.
IMO, it's best to have the PDF file simply displayed and then let the visitor choose if they want to simply just read it where it is, or click the save icon and permanently save it to their HD. Options are always best and makes for a more user-friendly website. The actual end behavior of what a PDF file does after its link is clicked can also be determined by the user's browser settings and or PDF reader, they can be set to display the file in a browser window, or in a separate (Adobe or other reader) window.
deepsand
03-23-2010, 01:15 AM
I have a very strong preference for being given the choice between having the file immediately rendered or saved; the latter is very much faster than is the three-step of view, save, and close.
Rod Abbotson
03-23-2010, 01:44 AM
To open in a new window or tab use this snippet of code
onclick="window.open(this.href,'_blank');return false;"
this will validate in xhtml 1.0 strict
also this works in all browsers....
I got the info from this forum and have employed it on my sites ever since.
Rod
Clint1
03-23-2010, 02:58 AM
To open in a new window or tab use this snippet of code
onclick="window.open(this.href,'_blank');return false;"
this will validate in xhtml 1.0 strict
also this works in all browsers....
I got the info from this forum and have employed it on my sites ever since.
Rod
But what happens if one has a problem with JS or has it disabled?
That is a failsafe script - if JS is disabled, it will simply open the document in the current window.
Clint1
03-24-2010, 03:16 AM
That is a failsafe script - if JS is disabled, it will simply open the document in the current window.
I see. They all should be "failsafe". ;) Is there any advantage of using something like that over the target="_blank" tag?
With the target="blank" attribute, you can get unpredictable results in many browsers that don't support frames, especially mobile and alternate platform browsers (iPhones, entertainment systems like Playstation, browsers integrated into home theater systems, etc.) Sometimes, the link may work by just opening the page in the current window, other times a new task may be spawned, or the link may not work at all. With the onclick, if Javascript is not supported, the onclick attribute is ignored and the href is processed as though it were a normal link.
Clint1
03-24-2010, 09:32 AM
With the target="blank" attribute, you can get unpredictable results in many browsers that don't support frames, especially mobile and alternate platform browsers (iPhones, entertainment systems like Playstation, browsers integrated into home theater systems, etc.) Sometimes, the link may work by just opening the page in the current window, other times a new task may be spawned, or the link may not work at all. With the onclick, if Javascript is not supported, the onclick attribute is ignored and the href is processed as though it were a normal link.
Thanks. Where exactly does it go, after the .html" ?
......html" onclick="window.open(this.href,'_blank');return false; title="More of the file name">Link text</a>
Or anywhere in the href tag?
DaveSawers
03-25-2010, 08:06 AM
Where exactly does it go, after the .html" ?
The order of attributes within a tag does not matter. So put it wherever.
borther_of_devil
04-13-2010, 03:29 AM
Many sites I visit I can simply click on a link and download a pdf.
How do I put this option on my site?
Put it into zip. This is the simpliest possible way.
Or you can change the httpd file type of the .pdf extension so it would download rather than opened. Not a good solution anyway. It is better to leave choice to a user as other said.