I'm having a serious problem with a PHP + Java application I'm creating. The app is almost entirely created in PHP, but we have a single Java Servlet that is generating PDF files dynamically using FOP.
In order to ensure security, we're reading the PDF contents from the JSP then writing them to the browser directly from a PHP script. We decided to implement it this way for several reasons, which I can't really get in to.
The script was working fine for a few months, but then it stopped working as the server started getting more traffic and the app slowed down. What started happening is that my PHP would output a PDF file with 0 bytes of data - empty page. Seemed to me that feof() might be returning true straight away?
I added a sleep(8) and that fixed it for a week. Then it stopped working again. I upped it to sleep(30). No luck.
Any ideas why this is happening? I was under the impression that fopen() and fread() were synchronous?
Code: Select all
<?php
/**
* I eliminated unnecessary PHP code here...
*/
// Sample path for your understanding of what we're doing
$pdfServletPath = "http://localhost:8080/pdf_generator.jsp?data=report1.xml&fo=report.xsl&username=Steven";
// Generate and read the contents of the PDF file from the local FO application as binary data
$file = fopen($pdfServletPath , "rb");
// Sleep until the PDF generates - This was added after the bugs started happening
sleep(30);
// Append the contents of the PDF Servlet to a string / buffer
while (!feof($file)) {
$fileBuffer .= fread($file, 4096);
}
// Close the servlet
fclose($file);
// Get the file size
$fileSize = strlen($fileBuffer);
// Set the PDF header info
header("Content-type: application/pdf");
header("Content-Length: {$fileSize}");
header("Content-Disposition: inline; filename={$pdfFileName}");
// Output the PDF to the browser
print $fileBuffer;
?>
Thanks in advance for any help you can offer!!!
- Steve