CakePHP生成pdf然后作为电子邮件附件发送 [英] CakePHP generate pdf then send as email attachment

查看:439
本文介绍了CakePHP生成pdf然后作为电子邮件附件发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用TCPDF库在我的CakePHP应用程序中创建发票。如果我去 / invoices / view / 1 我的view.ctp包括生成和显示pdf在浏览器的代码。我想添加的能力发送pdf作为电子邮件附件,所以我创建了操作 / invoices / send_invoice 并将代码从view.ctp复制到电子邮件模板。



现在,我不知道如何在附加PDF之前先生成PDF。
在我的view.ctp页面模板的末尾,我使用

  $ pdf-> File / pdf_invoices'。DS。'invoice  - '。$ invoice ['Invoice'] ['id']。'。pdf','I'); 

这将发送pdf在浏览器中查看而不创建文件。如果我使用'F'或'FI'作为$ pdf-> Output()的最后一个参数,我可以保存文件,但不能保证用户在尝试以电子邮件发送之前查看发票。



在我的send_invoice操作中,我需要将pdf生成为一个文件,以便附加它:

  $ email->附件(APP。'File / pdf_invoices'。DS。'invoice  - '。$ invoice ['Invoice'] ['id']。'。 

具有在我的电子邮件模板中生成pdf文件的代码意味着该文件不存在我试着附上它。有没有什么方法从电子邮件模板本身附加文件?我想或许我可以在我的发票模型上编写一个函数来生成pdf,但我想使用一些View Helpers(如Number helper)来格式化我的数据。我可以在视图中执行代码,然后返回到控制器/操作而不显示视图?

解决方案

通过创建一个视图对象,然后手动调用你需要得到它正确渲染的函数。

  $ view = new View null,false); 
$ view-> set(//正常设置视图数据);
$ view-> viewPath ='pdf'; //文件夹在Views目录中查找
$ output = $ view-> render('pdf','pdf'); //布局名称和视图名称

然后只需保存输出, / p>

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?

解决方案

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.

这篇关于CakePHP生成pdf然后作为电子邮件附件发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆