doumeikuan6834
2018-03-28 14:14
浏览 84
已采纳

Yii2发送pdf到邮件不工作?

I am using kartik mpdf extension to generate using the mention below code it work and show the pdf in next tab

  $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();

Now for send the generatd pdf on mail i want to send the mail before save it on server using below code

$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();

Try my best to hit api and generate pdf but this also doesnt work.

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

It also retrn Null

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douxiza9868 2018-03-29 07:07
    已采纳

    First i have save the generated pdf in the server directory and then send it to mail and unlink after sending succcessfully using the below code

       $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');
                      }
    
    打赏 评论
  • duanan6043 2018-03-28 14:26

    I cant find where the exact error is but I had successfully generated pdf via mpdf with

      $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();
    

    and make sure the$contentis pure html.

    打赏 评论

相关推荐 更多相似问题