2016-02-18 83 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/

爲什麼郵件無法發送可能是有許多原因的原因,檢查應用程序日誌在運行時錯誤。