I am creating a PDF, from a template, using JasperPHP.
I then immediately try to display it.
How can I ensure that it is completely created, before displaying?
I am checking that the file exists but the PDF is corrupt, at this point, so I am assuming it is still being created.
I have also tried using flock, to open it with an exclusive lock, but this seems to work, even when the PDF is not completely rendered.
This is what I am doing (the dd is just for testing, it never gets here):
/**
* Check that we can lock a file
* this ensures that it has been completely created.
*
* @param $filename
* @return bool
*/
function stillWriting($filename)
{
$stillWriting = true;
if($fp = fopen($filename, "r+"))
{
if (flock($fp, LOCK_EX)) // Try to get an exclusive lock on the file
{
flock($fp, LOCK_UN); // Unlock - we're just checking
$stillWriting = false;
}else{
dd('failed');
}
fclose($fp);
}
return $stillWriting;
}
Mick
Edit - added more detail
This is where the PDF gets created:
exec($this->the_command, $output, $return_var);
I am using https://github.com/cossou/JasperPHP
It executes JasperStart [https://sourceforge.net/projects/
This is where the PDF is displayed:
return Response::make(file_get_contents($this->outputFilename .'.pdf'), 200, [
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'inline; filename="' . $this->outputFilename .'.pdf' .'"'
]);
The actual problem that I am getting is that Chrome says "Failed to load PDF document", I am guessing from this, that it is a timing problem. The file opens correctly If I then go to Windows and double click.