简体   繁体   English

CakePHP生成pdf,然后作为电子邮件附件发送

[英]CakePHP generate pdf then send as email attachment

I'm using the TCPDF library to create invoices in my CakePHP application. 我正在使用TCPDF库在CakePHP应用程序中创建发票。 If I go to /invoices/view/1 my view.ctp includes the code to generate and display the pdf in the browser. 如果我转到/invoices/view/1我的view.ctp包含用于在浏览器中生成并显示pdf的代码。 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. 我想添加将pdf作为电子邮件附件发送的功能,所以我创建了动作/invoices/send_invoice并将代码从view.ctp复制到了电子邮件模板中。

Now where I'm stuck, I don't know how to have the PDF generated first before attaching it. 现在卡住了,我不知道如何在附加PDF之前先生成它。 At the end of my view.ctp page template I use 在view.ctp页面模板的最后,我使用

$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. 它会发送pdf以便在浏览器中查看而不创建文件。 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. 如果我将$ F-或-FI作为$ pdf-> Output()的最后一个参数,则可以保存文件,但不能保证用户在尝试将其作为电子邮件发送之前会先查看该发票。

In my send_invoice action I need to have the pdf generated as a file in order to attach it with: 在我的send_invoice动作中,我需要将pdf生成为文件,以便附加:

$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. 在我的电子邮件模板中包含生成pdf文件的代码意味着当我尝试附加该文件时,该文件尚不存在。 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. 我当时想也许可以在Invoice模型上编写一个函数来生成pdf,但是我想使用一些View Helpers(例如Number helper)来格式化数据。 Can I execute the code in a view and then return to the controller/action without displaying the view? 我可以在视图中执行代码,然后在不显示视图的情况下返回到控制器/操作吗?

Cakephp Email with PDF Attachment and PDF generation with CakePDF plugin 带有PDF附件的Cakephp电子邮件和带有CakePDF插件的PDF生成

Step1: Installing and Setup CakePDF 步骤1:安装和设置CakePDF

Step 2: Configuration 步骤2:设定

Step 3: App Controller Configuration 步骤3:App Controller配置

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

Step 4: Controller Code for your CakePHP 步骤4: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: 非常有用的链接,只需按照上述步骤操作,您的PDF就会生成:

Read More 阅读更多

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. 然后只保存输出,或根据需要使用它。

Another possibility, if you can use wkhtmltopdf , is to generate a temp pdf file, attach it and delete it. 如果可以使用wkhtmltopdf ,另一种可能性是生成一个临时pdf文件,将其附加并删除。 wkhtmltopdf does the main job: It directly converts an html file to a pdf. wkhtmltopdf做主要工作:它直接将html文件转换为pdf。 The html is your view. html是您的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM