Page 1 of 1

Unable to serve PDF files

PostPosted: 22. September 2010 18:33
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 used to server the file:

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);
}


Also, if I choose to "Save" the file, the saved file has a length of zero... It's empty.

I'm running an XAMPP installation (PHP 5.3.1 on Apache 2, with the Tomecat plugin) 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 the PHP variables are correct with correct values, using xdebug.

This is driving me crazy.

Please Help!

Re: Unable to serve PDF files

PostPosted: 22. September 2010 18:42
by Nobbie
Try to use readfile() instead of your opening/reading/printing/closing the file.

http://de2.php.net/manual/en/function.readfile.php

Re: Unable to serve PDF files

PostPosted: 22. September 2010 21:50
by laceja
Resolved.

use readfile($file_path); instead.