I'm using FDPF to generate invoices for customers, which are then attached to an e-mail and sent along to the customer. The invoices / emails are generated in a batch (several hundred at a time). In the first real world run of the batch, a handful (about 5 out of 200) of customers received corrupt PDFs. The common link between them was that they had larger invoices than average, which leads me to believe that the time it takes to generate the invoice is causing a race condition and perhaps the e-mail is being sent before the PDF has time to finish generating completely. Is there a way to validate that the PDF is not corrupt before sending the e-mail? Or is there another way to approach the problem that I'm overlooking?
You can test if the pdf is fully generated by creating an md5 hash for the pdf at the time the file is first sent, and then again while the email is sending, and finally after it has been sent. If the md5 changes each time, then the file is still being created by the pdf generator while the email is sending.
Here's an example on how to use md5 hash:
<?php
$file_name = 'md5_demonstration_file';
$file_changer = 0;
while($file_changer < 10)
{
file_put_contents($file_name, $file_changer);
echo md5_file ($file_name) . '</br>';
$file_changer++;
}
?>
You will notice that the md5 hash changes on each iteration of the function because the file is still being written. If you try this sample code, you may have to set the permissions on 'md5_demonstration_file' manually so that anyone can write to it.
If race condition isn't the problem have you read this SO suggested thread: http://stackoverflow.com/questions/3578752/corrupt-pdf-email-attachment-when-generated-by-fpdf-and-php