Yii2发送pdf到邮件不工作?

时间:2018-03-28 14:14:22

标签: php yii2 mpdf kartik-v

我正在使用kartik mpdf扩展来生成使用下面提到的代码,并在下一个标签中显示pdf

  $pdf = new Pdf([
              'mode' => Pdf::MODE_UTF8, // leaner size using standard fonts
              'filename' => 'Bill_of_lading_'.$exportDetail->booking_number.'_'.$customerDetail->customer_name.'_'.$customerDetail->company_name.'.pdf',
              'content' => $this->renderPartial('landing', [
                  'model' => $this->findModel($id),
              ]),
              'options' => [
                  'title' => 'Privacy Policy - Krajee.com',
                  'subject' => 'Generating PDF files via yii2-mpdf extension has never been easy'
              ],
              'methods' => [
                  'SetHeader' => ['Generated By: ARIANA WORLDWIDE||Generated On: ' . date("r")],
                  'SetFooter' => ['|Page {PAGENO}|'],
              ]
          ]);
return $pdf->render();

现在发送邮件的generatd pdf我希望发送邮件,然后使用以下代码将其保存在服务器上

$content = $pdf->content;
      $filename = $pdf->filename;
$sendemail=Yii::$app->mail->compose()
             ->attachContent($content, [
                           'fileName'    => $filename,
                            'contentType' => 'application/pdf'
                           ])
                             ->setFrom('mushahidh224@gmail.com')
                             ->setTo('rajwabarocho@gmail.com')
                             ->setSubject('Design Beta sending subject here')
                             ->send();

尽我所能来打api并生成pdf,但这也行不通。

  $mpdf = $pdf->getApi();
       $mpdf->WriteHTML($content); 
       $path = $mpdf->Output(Yii::getAlias('@backend').'/uploads/pdf/'.$filename.'.pdf', 'F');

它也会追溯Null

3 个答案:

答案 0 :(得分:1)

  

首先,我已将生成的pdf保存在服务器目录中,然后将其发送到邮件并在使用以下代码成功发送后取消链接

   $pdf = new Pdf([
              'mode' => Pdf::MODE_UTF8, // leaner size using standard fonts
              'filename' => 'Bill_of_lading_'.$exportDetail->booking_number.'_'.$customerDetail->customer_name.'_'.$customerDetail->company_name.'.pdf',
              'content' => $this->renderPartial('landing', [
                  'model' => $this->findModel($id),
              ]),
              'options' => [
                  'title' => 'Privacy Policy - Krajee.com',
                  'subject' => 'Generating PDF files via yii2-mpdf extension has never been easy'
              ],
              'methods' => [
                  'SetHeader' => ['Generated By: ARIANA WORLDWIDE||Generated On: ' . date("r")],
                  'SetFooter' => ['|Page {PAGENO}|'],
              ]
          ]);
         if($mail){
             $content = $pdf->content;
             $filename = $pdf->filename;
         //  $mpdf = $pdf->getApi();
      //  $mpdf->WriteHtml($content);

      $path = $pdf->Output($content,Yii::getAlias('@backend').'/uploads/pdf/'.$filename.'.pdf',\Mpdf\Output\Destination::FILE);

             $sendemail=Yii::$app->mail->compose()
             ->attach(Yii::getAlias('@backend').'/uploads/pdf/'.$filename.'.pdf')
                             ->setFrom('mushahidh224@gmail.com')
                             ->setTo('rajwabarocho@gmail.com')
                             ->setSubject('Design Beta sending subject here')
                             ->send();
                             if($sendemail)
                             {
        unlink(Yii::getAlias('@backend').'/uploads/pdf/'.$filename.'.pdf');
                    return $this->render('mailed');
                  }

答案 1 :(得分:0)

我无法找到确切错误的位置,但我已成功通过mpdf

生成pdf
  $model= $this->findModel($id);
    // get your HTML raw content without any layouts or scripts
    $content = $this->renderPartial('print_salaryslip',['model'=>$model]);

    // setup kartik\mpdf\Pdf component
    $pdf = new Pdf([
        // set to use core fonts only
        'mode' => Pdf::MODE_BLANK,
        // A4 paper format
        'format' => Pdf::FORMAT_A4,
        // portrait orientation
        'orientation' => Pdf::ORIENT_PORTRAIT,
        // stream to browser inline
        'destination' => Pdf::DEST_BROWSER,
        // your html content input
        'content' => $content,
        // format content from your own css file if needed or use the
        // enhanced bootstrap css built by Krajee for mPDF formatting
        'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
        // any css to be embedded if required
        'cssInline' => '.kv-heading-1{font-size:18px}',
        // set mPDF properties on the fly
        'options' => ['title' => 'Krajee Report Title'],
        // call mPDF methods on the fly
        'methods' => [
            'SetHeader'=>[' PAYSLIP'],
            'SetFooter'=>['{PAGENO}'],
        ]
    ]);

    // return the pdf output as per the destination setting
    return $pdf->render();

并确保$content是纯HTML。

答案 2 :(得分:0)

做这样的事情     ...

export default combineReducers({
    rooms: roomsReducer
    room1: getRoomReducer(1),
    room2: getRoomReducer(2),
    room3: getRoomReducer(3)
});