Using fsockopen to display pdf file

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
DriveBoy
Forum Newbie
Posts: 2
Joined: Thu Dec 18, 2003 5:07 am

Using fsockopen to display pdf file

Post by DriveBoy »

I'm using fsockopne to open a connection to a port in a java program (must do it this way due to firewall problems) which has read a pdf file a is outputting it as a stream.

I know the java code is ok.

When I display the returned data in php it seems that some of it may be corrupted. Php code below...


while (!feof($fp)) {
$buff = fgets ($fp,128);
$pdffile .= $buff;
}

When I try to get the browser to launch pdf viewer..

header("Content-type: application/pdf");
header("Content-disposition: filename=dmsdoc.pdf");
echo $pdffile;

I get error saying the launchpdf.php (name of script) cannot be downloaded.

All code runs on windows, using apache.

Has anyone else done something similar? Any advice greatly appreciated.

:?:
User avatar
Crashin
Forum Contributor
Posts: 223
Joined: Mon May 06, 2002 3:42 pm
Location: Colorado

Post by Crashin »

I've never seen it, but it sounds cool! 8)
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

Found this somewhere. might be interesting?

Code: Select all

$filename = 'foo.pdf';
   $filesize = filesize($filename);
   header("Pragma: public");
   header("Expires: 0"); // set expiration time
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
   header("Content-Type: application/pdf");
   header("Content-Length: ".$filesize);
   header("Content-Disposition: inline; filename=$filename");
   header("Content-Transfer-Encoding: binary");
    /*
    * Can't use readfile() due to poor controlling of the file download.
    * (IE have this problems)...
    */
   // readfile($filepath);

   //use fopen() instead of readfile...
   $fp = fopen($filepath, 'rb');
   $pdf_buffer = fread($fp, $filesize);
   fclose ($fp);

   print $pdf_buffer;

    /*
    * Required, to keep IE from running into problems
    * when opening the file while downloading or downloading...
    * (IE been acting strange lately...)
    */
   exit();
DriveBoy
Forum Newbie
Posts: 2
Joined: Thu Dec 18, 2003 5:07 am

Post by DriveBoy »

Thanks for the help, but as I said I'm getting the contents from a network connection to a java program. I need to know if I should use fgets(), fread() etc and who to use them.

It seems like the data may be being converted to characters/string and thus corrupting the pdf file.
Post Reply