当我想从HTML生成PDF文件时,如何在YII中使用mPDF?

时间:2017-09-18 05:49:43

标签: php pdf yii mpdf

<div class='postHeader'>
  <div>{post.title}</div>
  <div>{post.author}</div>
</div>

以上代码用于使用YII框架生成PDF。

1 个答案:

答案 0 :(得分:0)

前两行将创建css $pdf_style和html文件$pdf_part,PDF将写入此文件。

WriteHTML函数中包含这两个变量。

    $pdf_style = \Yii::$app->view->renderFile(PROTECTEDPATH . 'common/views/contract/style.css', $data);
    $pdf_part = \Yii::$app->view->renderFile(PROTECTEDPATH . 'common/views/contract/file.tpl', $data);


    $mpdf = new \mPDF('utf-8','A4');

    $mpdf->autoScriptToLang = true;
    $mpdf->baseScript = 1;  // Use values in classes/ucdn.php  1 = LATIN
    $mpdf->autoVietnamese = true;
    $mpdf->autoArabic = true;
    $mpdf->autoLangToFont = true;

    $mpdf->SetMargins(null,null,40);
    $mpdf->SetDisplayMode('fullpage');
    $mpdf->SetHTMLHeader('<div style="text-align:right"><img src="'. DOCROOT  .'images/logo_contract.png" height="68" /></div>');

    $mpdf->setFooter(' | www.facebook.com | {PAGENO}');
    $mpdf->WriteHTML($pdf_style.'<div class="wrapper">'.$pdf_part.'</div>');

    $mpdf->Output('Contract-'.date("iYsmd"), $action, $dest);