Question

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?

Was it helpful?

Solution 2

You can do so by creating a view object, then manually calling the functions you need to get it to render properly.

 $view = new View(null, false);
 $view->set(//set view data here like normal);
 $view->viewPath = 'pdf';  //folder to look in in Views directory
 $output = $view->render('pdf', 'pdf');  //layout name and view name

Then just save the output, or use it however you need.

OTHER TIPS

Cakephp Email with PDF Attachment and PDF generation with CakePDF plugin

Step1: Installing and Setup CakePDF

Step 2: Configuration

Step 3: App Controller Configuration

App::uses('CakePdf', 'CakePdf.Pdf');
public $components = array("Email","RequestHandler");

Step 4: Controller Code for your CakePHP

    // PDF Generation code
    $this->set('extraparams', $categories);

    $this->pdfConfig = array(
        'orientation' => 'portrait',
        'filename' => 'invoice_'. $orderID
    );

    $CakePdf = new CakePdf();
    $CakePdf->viewVars(array('extraparams' => $categories));
    $CakePdf->template('confirmpdf', 'default');
    //get the pdf string returned
    $pdf = $CakePdf->output();
    //or write it to file directly


    $pdf = $CakePdf->write(APP . 'webroot'. DS .'files' . DS . 'orders' . DS . 'order_'.$orderID.'.pdf');
    $pdf = APP . 'webroot'. DS .'files' . DS . 'orders' . DS . 'order_'.$orderID.'.pdf';
    // PDF Generation code

    return $pdf;

Very helpful link, just follow mentioned steps and your PDF are generating:

Read More

Another possibility, if you can use wkhtmltopdf, is to generate a temp pdf file, attach it and delete it. wkhtmltopdf does the main job: It directly converts an html file to a pdf. The html is your view.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top