Error Serving a PDF

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
laceja
Forum Newbie
Posts: 4
Joined: Wed Sep 22, 2010 10:13 am

Error Serving a PDF

Post by laceja »

I've been struggling with a problem for several days now. I'm trying to serve a PDF file and, when I try to "Open with Adobe Reader...", I get an error that says:
Could not open "filename.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).
If I double click on the original file, in my file system, it opens without any problems in Adobe Reader.

Here's the relavent PHP code:

Code: Select all

session_cache_limiter('must-revalidate');
header("Pragma: public");
header("Expires: " . gmdate("D, d M Y H:i:s", time()) . " GMT");
header("Cache-Control: private");
header("Content-Description: File Transfer");
header("Content-Type: application/pdf");
header("Content-Disposition: attachment; filename=\"$asfname\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fsize);

$file = fopen($file_path,"rb");
if ($file) {
  while(!feof($file)) {
    print(fread($file, 1024*8));
    flush();
    if (connection_status()!=0) {
      fclose($file);
      die();
    }
  }
  fclose($file);
}
I'm running PHP 5.3.1 on Apache on a WindowsXP machine. I have verified that "extension=php_pdflib.dll" is in my php.ini and is not commented out. My browser is Firefox 3.6.10. I doubt the problem is with the browser because, I have no problem downloading and opening PDF files from external sites. I have also verified that all variables are correct and values are correct, using xdebug.

This is driving me crazy.

Help! :banghead:
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Error Serving a PDF

Post by Weirdan »

Does it work if you instead save it to the hard drive first? Something like right-click, 'Save as...'
laceja
Forum Newbie
Posts: 4
Joined: Wed Sep 22, 2010 10:13 am

Re: Error Serving a PDF

Post by laceja »

No, it does not work, if I save it to the hard drive. Actually, after clicking "Save", I looked at the file and it was zero length... empty.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Error Serving a PDF

Post by Weirdan »

So the real issue is that the file is not being stored properly. Check the response headers - it could be that apache or php is forcibly setting the transfer encoding to chunked.
laceja
Forum Newbie
Posts: 4
Joined: Wed Sep 22, 2010 10:13 am

Re: Error Serving a PDF

Post by laceja »

I'm sorry, I don't know how to check the actual message headers sent. I did set "Content-Transfer-Encoding: binary". Is it possible for Apache to override it. I'm using xampp (with the Tomcat plugin) and I've not done any special configuration of Apache.

How do I view the actual message headers sent?
laceja
Forum Newbie
Posts: 4
Joined: Wed Sep 22, 2010 10:13 am

Re: Error Serving a PDF

Post by laceja »

When I changed:

Code: Select all

$file = fopen($file_path,"rb");
if ($file) {
  while(!feof($file)) {
    print(fread($file, 1024*8));
    flush();
    if (connection_status()!=0) {
      fclose($file);
      die();
    }
  }
  fclose($file);
}
to a simple:

Code: Select all

readfile($file_path); 
It now works.
Post Reply