Image link that downloads a pdf

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
thoque
Forum Newbie
Posts: 6
Joined: Fri May 15, 2009 1:55 pm

Image link that downloads a pdf

Post 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.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Image link that downloads a pdf

Post 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.
thoque
Forum Newbie
Posts: 6
Joined: Fri May 15, 2009 1:55 pm

Re: Image link that downloads a pdf

Post by thoque »

I have no problem making a link. How can I download the pdf?
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Image link that downloads a pdf

Post 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
thoque
Forum Newbie
Posts: 6
Joined: Fri May 15, 2009 1:55 pm

Re: Image link that downloads a pdf

Post by thoque »

don't understand how to use that.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Image link that downloads a pdf

Post 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).
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Image link that downloads a pdf

Post 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 :)
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Image link that downloads a pdf

Post 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
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Image link that downloads a pdf

Post 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\"
Post Reply