Page 1 of 1

Image link that downloads a pdf

Posted: Fri Jun 05, 2009 11:57 am
by thoque
Hi I want to have a image link where people can click to download a pdf file. I would appreciate any sort of help.

Re: Image link that downloads a pdf

Posted: Fri Jun 05, 2009 12:19 pm
by mikemike
I think you need to look at basic HTML tutorials.
http://www.w3schools.com/htmL/html_images.asp
http://www.w3schools.com/htmL/html_links.asp

Some browsers open PDFs in the browser, if you're looking to force a download then you'll need to look into HTTP headers and the header() function... but if you're struggling to even make a linked image then I'd leave the force download stuff for now.

Re: Image link that downloads a pdf

Posted: Fri Jun 05, 2009 12:38 pm
by thoque
I have no problem making a link. How can I download the pdf?

Re: Image link that downloads a pdf

Posted: Fri Jun 05, 2009 12:43 pm
by mikemike
See Example 1 in the header() documentation as I pointed out in the earlier post:
http://uk2.php.net/manual/en/function.header.php

Re: Image link that downloads a pdf

Posted: Fri Jun 05, 2009 1:29 pm
by thoque
don't understand how to use that.

Re: Image link that downloads a pdf

Posted: Fri Jun 05, 2009 1:31 pm
by mikemike
Link to a PHP page, in that PHP page have:

Code: Select all

<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');
 
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
 
// The PDF source is in original.pdf
readfile('original.pdf');
?>
Obviously changing the filenames to match the one's you want. Easy. (that code is unedited from php.net).

Re: Image link that downloads a pdf

Posted: Fri Jun 05, 2009 1:38 pm
by Darhazer
thoque wrote:don't understand how to use that.
Well, here you have to make yourself familiar with HTTP, not with PHP, because header() function sends HTTP headers. However, I assume you are looking for quick solution (read about the HTTP protocol as soon as you can), so I will tell you, that you have to send a Content-type header (usualy application/ocstream to force download instead of opening with the configured plugin; this is like saying "unknown type, so you cannot open it right now, you have to downloading first) and Content-disposition: attachment. I'm writing this on prima vista, I didn't check the exact spelling, but I believe that you can find this with one search in Google. So:

Code: Select all

header('Content-type: application/ocstream');
header('Content-Disposition: attachment; filename="your-pdf-file.pdf"');
readfile('your-pdf-file.pdf');
On the other hand, do you have a reason to force download, instead of viewing? I'm using similar code in cases when I want to provide a download button for common type of files, that browser will probably just open. And recently I had to force opening the PDF file in browser, instead of downloading... It was pretty simple: the embed tag :)

Re: Image link that downloads a pdf

Posted: Fri Jun 05, 2009 3:07 pm
by mikemike
Why not use the proper mime-type like the php.net documentation does? I'm not trying to sound coy, I'm genuinely intrigued

Re: Image link that downloads a pdf

Posted: Fri Jun 05, 2009 3:36 pm
by Darhazer
mikemike wrote:Why not use the proper mime-type like the php.net documentation does? I'm not trying to sound coy, I'm genuinely intrigued
It's all about the browser. I do not remember exactly, as this was 2 or 3 years ago when I had problems with downloading the file... as far as I remember some of the major browsers was ignoring the content-disposition header. Just try your code with IE 6, FF and Opera... If it works with all of them, than I'm just wrong. But I have the habit to use octet-stream content-type since the time I was implementing the downloading of files for the first time

P.S. it seems to be an IE problem: Microsoft Support on Content-disposition
Somewhere at Internet... wrote:To be on the safe side and ensure consistent behavior in all browsers, it's usually better to use both:

Content-Type: application/octet-stream
Content-Disposition: attachment;filename=\"My Text File.txt\"