Page 1 of 2

Opening a PDF on a website

Posted: Mon Oct 03, 2005 5:20 pm
by Luke
I am making a web site that will give the user the option to open one of several pdf files and print them. How do I make a link open a pdf file with adobe reader instead of trying to load it in the browser? Right now all it does is lead to a page that says http://www.mydomain.com/thepdf.pdf that is blank.

Posted: Mon Oct 03, 2005 6:01 pm
by Chris Corbyn

Code: Select all

header("Content-disposition: attachment; filename='foo.pdf'");

Posted: Mon Oct 03, 2005 6:11 pm
by Luke
Thank you thank you thank you!!

Posted: Mon Oct 03, 2005 6:19 pm
by Luke
:cry:
Adobe Reader could not open "cqapp.pdf" because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).
How do I avoid this??

Posted: Mon Oct 03, 2005 6:20 pm
by feyd
pdf files are binary files. If extra characters get added to the stream they can and often do have loading errors. Check to ensure you don't have any extra characters being output.


btw, read your PM's.

Posted: Mon Oct 03, 2005 6:30 pm
by Luke

Code: Select all

<?php
header("Content-disposition: attachment; filename='cqapp.pdf'"); 
?>
This is the entire php file. Is there something wrong with it?

Posted: Mon Oct 03, 2005 6:44 pm
by feyd
you need to set other headers such as content-length, content-type, encoding... and actually send the file :P

readfile()

Look in header() documentation and here about their help on downloads via php.

Posted: Mon Oct 03, 2005 7:12 pm
by Luke
Thanks! It works. Life saver!

Posted: Tue Oct 04, 2005 11:52 am
by Luke
OK... nevermind, it only works on my local server. Here is the new php file. What am I doing wrong now??

Code: Select all

<?php
           $filename = $_GET['file'];
           $filename = realpath($filename);
           header("Pragma: public");
           header("Expires: 0");
           header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
           header("Cache-Control: private",false);
           header("Content-Type: application/pdf");
           header("Content-Disposition: attachment; filename=\"$filename\";");
           header("Content-Transfer-Encoding: binary");
           header("Content-Length: ".@filesize($filename));
           set_time_limit(0);
           @readfile("$filename") or die("File not found.");
?>

Posted: Tue Oct 04, 2005 12:02 pm
by feyd
only the filename needs to be in the disposition header. You're storing the path details there too.. ;)

Posted: Tue Oct 04, 2005 12:33 pm
by Luke
So I removed this line:

Code: Select all

$filename = realpath($filename);
still doesn't work...

Posted: Tue Oct 04, 2005 12:58 pm
by Luke
Damn... I thought I figured it out. I was uploading the pdfs ascii instead of binary, so I uploaded them binary. Still doesn't work. This is frustrating.

Posted: Tue Oct 04, 2005 1:19 pm
by Luke
Copied this DIRECTLY from php.net and it doesn't work... the pdf file is getting corrupted somehow. I don't understand!

Code: Select all

$filename = 'cqapp.pdf';
           $filename = realpath($filename);

           $file_extension = strtolower(substr(strrchr($filename,"."),1));

           switch ($file_extension) {
               case "pdf": $ctype="application/pdf"; break;
               case "exe": $ctype="application/octet-stream"; break;
               case "zip": $ctype="application/zip"; break;
               case "doc": $ctype="application/msword"; break;
               case "xls": $ctype="application/vnd.ms-excel"; break;
               case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
               case "gif": $ctype="image/gif"; break;
               case "png": $ctype="image/png"; break;
               case "jpe": case "jpeg":
               case "jpg": $ctype="image/jpg"; break;
               default: $ctype="application/force-download";
           }

           if (!file_exists($filename)) {
               die("NO FILE HERE");
           }

           header("Pragma: public");
           header("Expires: 0");
           header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
           header("Cache-Control: private",false);
           header("Content-Type: $ctype");
           header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
           header("Content-Transfer-Encoding: binary");
           header("Content-Length: ".@filesize($filename));
           set_time_limit(0);
           @readfile("$filename") or die("File not found.");

Posted: Tue Oct 04, 2005 1:35 pm
by feyd
I have the sneaking suspicion that you have some extra characters on either side of the php tags (<?php ?>)

Posted: Tue Oct 04, 2005 1:39 pm
by Luke
nothing... not even spaces. how else could the file get corrupted. It opens adobe reader, but it's giving me this error:
There was an error opening this document. The file is damaged and could not be repaired.