Page 1 of 1

Using fsockopen to display pdf file

Posted: Thu Dec 18, 2003 5:07 am
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.

:?:

Posted: Thu Dec 18, 2003 3:07 pm
by Crashin
I've never seen it, but it sounds cool! 8)

Posted: Thu Dec 18, 2003 3:14 pm
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();

Posted: Fri Dec 19, 2003 2:52 am
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.