0
votes

I am dynamically generating pdf files using dompdf and attaching those to email and sending. But in mail always Im getting the attachment as 0 byte. I'am creating the files under cake's tmp folder and it is getting created successfully. Im using cakephp 2.x and sending mail using CakeEmail. dompdf folders are in plugin folder. I have given necessary permission to lib/fonts directory. The file is not sending as attachment immediately after the creation. Whether there will be any kind of locking/delay while using dompdf that is cakeemail could not able to get the contents of the file (but file exists) ?

App::uses('CakePdf', 'CakePdf.Pdf');
$CakePdf = new CakePdf();

$orderDetails = array('1' => 'myproduct');
$CakePdf->viewVars(compact('orderDetails'));      

$CakePdf->template('view_ticket_pdf', 'default');
$filename = TMP . $data['Billing']['order_id'] . '.pdf';
$pdf = $CakePdf->output();
$CakePdf->write( $filename);

$email = new CakeEmail('smtp');
$desc = $email->template('order_mail');
$email->emailFormat('html');
$email->from(array(ADMIN_EMAIL => 'Admin'));
$email->to('[email protected]');
$email->attachments(array($filename));
$email->subject('Your order');
$content = $email->send();

Im getting error as fread(): Length parameter must be greater than 0 in CakeEmail. Instead of pdf, if I create a sample text file, it is attaching and size will be greater than 0. I am using github.com/ceeram/CakePdf plugin for pdf creation.

1
Are you sure that the $path and $filename are correct? If so, are you deleting the created file after the send() ? - noslone
Thanks for the quick reply. Im sure the path is correct. If the hard code the name of a previously generated file there, it is attaching. I am not deleting the file. Also in my local machine, the css and images are getting displayed in the pdf but when I move to production, those are not displaying. whether it will affect this ? - binoy
I have implemented something similar and i got the 0 byte attachment when the file could not be found. Have you checked the logs? - noslone
Why didnt you post any code? Maybe one could help you then a little bit better than just discussing it theoretically. - mark
Is it possible that your file has not been unlocked - and accessing it from CakeEmail or any other class is not possible due to this file lock? $CakePdf->write() probably needs some file-unlocking afterwards? - mark

1 Answers

0
votes

Same problem here ($Email->attachments() does not attach file). It is not a locking issue by the way. If you write your PDF to a database you can do an ugly workaround. Have a look here: https://stackoverflow.com/a/17744761/1145597