Yii2-找不到类'mPDF'

时间:2018-07-21 22:51:01

标签: yii2

当我尝试从应用程序导出为PDF时,却出现此错误:

mPDF

我真的不知道是什么引起了错误。我尝试了几种方法都没有用。这是组件:

<?php

namespace backend\components;
use Yii;
use yii\base\Component;
use yii\base\InvalidConfigException;
use common\models\Organization;
use yii\helpers\Html;
use yii\web\NotFoundHttpException;
use mPDF;

class ExportToPdf extends Component
{
public function exportData($title='',$filename='Jkkesrms Pdf',$html=NULL)
{
    $mpdf = new mPDF('utf-8', 'A4',0,'',15,15,25,16,4,9,'P');
    $mpdf->autoScriptToLang = true;
    $mpdf->autoLangToFont = true;
    $org = Organization::find()->asArray()->one();
    $src = Yii::$app->urlManager->createAbsoluteUrl('site/loadimage');
    $org_image=Html::img($src,['alt'=>'No Image','width'=>90, 'height'=>70]); 
    $org_name=$org['org_name'];
    $org_add=$org['org_address_line1']."<br/>".$org['org_address_line2'];

    $mpdf->SetHTMLHeader('<table style="border-bottom:1.6px solid #999998;border-top:hidden;border-left:hidden;border-right:hidden;width:100%;"><tr style="border:hidden"><td vertical-align="center" style="width:35px;border:hidden" align="left">'.$org_image.'</td><td style="border:hidden;text-align:left;color:#555555;"><b style="font-size:22px;">'.$org_name.'</b><br/><span style="font-size:10.2px">'.$org_add.'</td></tr></table>');
    $stylesheet = file_get_contents('css/pdf.css'); // external css
    $mpdf->WriteHTML($stylesheet,0);
    $mpdf->WriteHTML('<watermarkimage src='.$src.' alpha="0.33" size="50,30"/>');
    $mpdf->showWatermarkImage = true;
    $arr = [
      'odd' => [
        'L' => [
          'content' => $title,
          'font-size' => 10,
          'font-style' => 'B',
          'font-family' => 'serif',
          'color'=>'#27292b'
        ],
        'C' => [
          'content' => 'Page - {PAGENO}/{nbpg}',
          'font-size' => 10,
          'font-style' => 'B',
          'font-family' => 'serif',
          'color'=>'#27292b'
        ],
        'R' => [ 
          'content' => 'Printed @ {DATE j-m-Y}',
          'font-size' => 10,
          'font-style' => 'B',
          'font-family' => 'serif',
          'color'=>'#27292b'
        ],
        'line' => 1,
      ],
      'even' => []
    ];
    $mpdf->SetFooter($arr);
    $mpdf->WriteHTML('<sethtmlpageheader name="main" page="ALL" value="on" show-this-page="1">');
    $mpdf->WriteHTML($html);
    $mpdf->Output($filename.'.pdf',"I");

}
}

?>

我尝试了几种方法,但错误仍然存​​在。请如何解决该错误?

1 个答案:

答案 0 :(得分:0)

您需要在use语句中包含完整的名称空间。查看代码,看起来您正在使用this库。如果正确,则将其添加为

use Mpdf\Mpdf;

然后像下面一样使用它

$mpdf = new Mpdf('utf-8', 'A4',0,'',15,15,25,16,4,9,'P');