Yii2 发送 pdf 到邮件不起作用?

Yii2 send pdf to mail not working?

我正在使用 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();

现在为了在邮件上发送生成的 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

我找不到确切的错误所在,但我已经通过 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。

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');
                  }

做这样的事情 ...

if (!empty($id)) {

            $emailSend = Yii::$app->mailer->compose(['html' =>'My-template-html'],[
                    'email' => $receiver->email,
                    'name'  => $receiver->name

                ])

                ->setFrom(["info@mail.com"])
                ->setTo($email)
                ->setSubject($subject)
                ->attach(Yii::getAlias('@backend').'/web/uploads/pdf/'.'Certificate'.'.pdf');
                return $emailSend->send();

               Yii::$app->session->setFlash('success', 'Email Sent Successfully');


            } 
...