I'm using the TCPDF library to create invoices in my CakePHP application. If I go to /invoices/view/1
my view.ctp includes the code to generate and display the pdf in the browser. I want to add the ability to send the pdf as an email attachment so I created the action /invoices/send_invoice
and copied the code from view.ctp into an email template.
Now where I'm stuck, I don't know how to have the PDF generated first before attaching it. At the end of my view.ctp page template I use
$pdf->Output(APP . 'File/pdf_invoices' . DS . 'invoice-'.$invoice['Invoice']['id'].'.pdf', 'I');
which sends the pdf to view in browser without creating the file. I can save the file if I use 'F' or 'FI' as the last parameter to $pdf->Output() but it's not guaranteed that the user will view the invoice before trying to send it as an email.
In my send_invoice action I need to have the pdf generated as a file in order to attach it with:
$email->attachments(APP . 'File/pdf_invoices' . DS . 'invoice-'.$invoice['Invoice']['id'].'.pdf');
having the code that generates the pdf file in my email template means the file doesn't exist yet when I try to attach it. Is there some way to attach the file from the email template itself? I was thinking maybe I can write a function on my Invoice model to generate the pdf but I want to use some View Helpers (like the Number helper) to format my data. Can I execute the code in a view and then return to the controller/action without displaying the view?