2016-02-18 85 views
0

我正在生成pdf报告并将其添加为电子邮件的附件。我正在使用tcpdf扩展并使用Yii框架。我怀疑是在哪里放置代码来生成pdf。我可以在发送电子邮件之前将其添加到控制器代码中,但我想创建一个单独的函数,在调用时生成pdf并将其保存到磁盘。分离Yii中的pdf生成模块

这里是我的示例代码

   $transaction->commit(); 



         $mail = Yii::app()->Smtpmail; 
         $mail->IsSMTP(); 
         $mail->IsHTML(true);         

         $mail->SetFrom("[email protected]", 'XXX Admin'); 
         //$headers = "Content-Type: text/html; charset=UTF-8"; 

         $mail->Subject = "Confirmation of survey completion"; 
         $mail->MsgHTML("Hi $survey->first_name $survey->last_name, <br><br>Thank you for completing the survey. The details of your survey result will be sent to the leader. <br><br>Please contact the leader for further details.<br><br>Regards,<br>Admin, XXX"); 


         $mail->AddAddress("[email protected]", ""); 
         $mail->CharSet = 'UTF-8'; 
         //to invoke another controller/component to generate pdf here 
         $controller1 = new PdfReportController("test"); 
         $controller1->actionEnglishReport(); 
         $dir = realpath(__DIR__ . '/../runtime/pdf-assets'); 
         $file = $dir . '/export.pdf'; 
         $mail->AddAttachment($file); 
         if(!$mail->Send()) 
         { 
           Yii::log("Mailer Error: " . $mail->ErrorInfo, 'error','application'); 
         } 
         else 
         { 
          Yii::log('Data1 saved!. Mail sent', 'info','application'); 
         } 

我不是如何将这个PDF生成模块分开很清楚。请帮助

更新: 我设法向PDF模块这样的分离:

    //Pdfreports is a component class which has MandarinReport   as a static method 
       PdfReports::MandarinReport(); 
       $dir = realpath(__DIR__ . '/../runtime/pdf-assets'); 
       $file = $dir . '/export.pdf'; 
       $mail->AddAttachment($file);      

的问题是,虽然创建PDF,带有附件的邮件没有发送

回答

0

我建议你看看那个Yii的使用http://www.yiiframework.com/doc/guide/1.1/en/basics.mvc

MVC模式是一种被广泛用于开发GUI的,如果你看看这里的方式隔开在三个不同层次的代码的责任MVC模式( https://en.wikipedia.org/wiki/Model -view-controller#Interactions)你可以看到控制器只能发送命令来更新模型或视图的状态

在Yii中,永远不应该实例化控制器。这是Yii会为你处理的事情。其真不明白您的示例代码被执行,但你可以创建用于发送电子邮件和产生的Yii组件(http://www.yiiframework.com/doc/guide/1.1/en/basics.component)的PDF格式的

class mailer extends CComponent{ 

    public static function sendMail($model){ 
     // You're mail code 
     return $mail->send(); // return true the mail is send 
    } 

    public static function generatePDF($model){ 
     // You're mail code 
     return $pdf->path; //return the path where the pdf is saved 
    } 
} 

你可以不过看看Yii的事件和行为,但它是一个有点更棘手。 (http://www.yiiframework.com/wiki/44/behaviors-events/ & http://www.yiiframework.com/doc/guide/1.1/en/basics.component#component-event

为了存储你的PDF您可以使用警予资产管理公司http://www.yiiframework.com/wiki/148/understanding-assets/

为什么邮件无法发送可能是有许多原因的原因,检查应用程序日志在运行时错误。