tags:

views:

39

answers:

3

hi,

I've a link to a pdf file in my page.

Safari opens the pdf file directly in the website. I would like to download it instead.

I've tried to add target="_blank" to the element, but it doesn't work if the pop-ups are disabled in the browser settings.

How can I solve this ?

thanks

+1  A: 

To make so, you need to change headers for .PDF files.

So, in your .htaccess, you need to do like this:

 AddType application/octet-stream .pdf
shamittomar
Use content-disposition, don't send a "I don't know what this is" header instead of "This is PDF"
David Dorward
This method was described at AskApache: http://www.askapache.com/htaccess/ultimate-htaccess-file-sample.html
shamittomar
The author of that site doesn't appear to be affiliated with the Apache Foundation, so the title of it smells a bit. It has quite a lot in it that I'd disagree with too.
David Dorward
If "this is a pdf" means that you have to add a header for each file, than this sounds like a valid solution, even if it's a dirty one.
Kau-Boy
@David, I agree. But I suggested this method because it appears that OP does not pass the content through a PHP (or other server-side language) where he can send headers. As he is just linking the .PDF files. So, I thought this is a workaround. And, it worked for me :)
shamittomar
Better to say "This is a PDF" and add a header to say "It should be downloaded" then to change a header to say "I don't know what this is". The latter will work, the former is still better.
David Dorward
@shamittomar — as Darin said, you can configure webservers to send content-disposition headers. You don't need to involve a server side programming language to do that.
David Dorward
+1  A: 

You need to set the Content-Disposition HTTP header to attachment for this file.

Content-Disposition: attachment; filename=test.pdf

Depending on the web server you are using the way to configure this might vary. Another option is to have a server side script which would stream the pdf and set this header.

Darin Dimitrov
what if I have many files ?
Patrick
But, he is not generating the PDF file or passing it through a server-side script. He is just giving a link `<a href>` to download it.
shamittomar
As Darin said, you just configure your server, but it depends on which server you are using. http://www.thingy-ma-jig.co.uk/blog/06-08-2007/force-a-pdf-to-download for example.
David Dorward
you can't download many files once (unless you put them in an archive), i suggest you to make your PHP file generic, see my edit.
youssef azari
+1  A: 

You'll need to dynamicaly force attachement headers using a server side script like PHP. Here's an example using PHP :

<?php
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('original.pdf');
?>

Edit :

$file = $_GET['file']; 
readfile($file."pdf");  // Before doing this, check whather the user have permission to download that file.

call your script : download.php?file=document will download document.pdf

youssef azari
Better to use server configuration if you are dealing with static files. It's simpler.
David Dorward
What if he's on shared hosting ?
youssef azari
Is there any Apache based hosting so bad that it doesn't let you set a content-disposition header with Apache configuration directives?
David Dorward
yes, i'm afraid.
youssef azari
In that cause, using a server side script to do it is the only real option (if you discount "Getting half-decent hosting"). Being able to configure the server is still better though ;)
David Dorward