PHP yii2.0框架利用mpdf导出pdf

时间:2022-10-20 20:37:29

安装:

composer require mpdf/mpdf

 使用:

use Mpdf\Mpdf;//(php7以前)


        //获取页面内容
        $res = $this->controller->render('tb-plan/clonePdf', [
            'cms_intro'=>$cms_intro,
            'corp_name'=>$corp_name,
            'order'=>$order,
            'intro'=>$intro,
            'cmsDaily'=>$val,
            'priceConf'=>$priceConf,
            'people'=>$people
        ]);
        $mpdf=new  Mpdf(['zh-CN','A4','','',23,23,40,'setAutoTopMargin' => 'stretch']);
        //设置中文字符集
        $mpdf->useAdobeCJK = true;
        $mpdf->autoScriptToLang = true;
        $mpdf->autoLangToFont = true;
 
        //设置PDF页眉内容
        $header='<table width="95%" style="margin:0 auto; vertical-align: middle; font-family:
        serif; font-size: 9pt; color: #000088;"><tr>
        <td width="10%"><img src="//i0.h34n32i1u.cn/a123/123/pdfLogo.jpg" alt="" width="30%"></td>
        <td width="40%" align="center"></td>
        <td width="50%" style="text-align: right;font-size: 20px;color: #1a1a1a;">订单ID:'.$order_id.'</td>
        </tr></table>';
        //设置PDF页脚内容
        $footer='<table width="100%" style=" vertical-align: bottom; font-family:
        serif; font-size: 9pt; color: #000088;"><tr style="height:30px"></tr><tr>
        <td width="48%" style="font-size:14px;color:#A0A0A0">ADD:上海市联航路1688弄</td>
        <td width="4%" align="center"></td>
        <td width="48%" style="text-align: right;font-size:14px;color:#A0A0A0">TEL:0000-000-000</td>
        </tr></table>';
 
        //添加页眉和页脚到pdf中
        $mpdf->SetHTMLHeader($header);
        $mpdf->SetHTMLFooter($footer);
        //设置pdf显示方式
        $mpdf->SetDisplayMode('fullpage');
        //设置水印
        $mpdf->SetWatermarkImage('//i0.hiniu.cn/a3/180121/Watermark.png',1,'F');
        $mpdf->showWatermarkImage = true;
 
        //加载css
        $style1=file_get_contents(Html::loadFile('tj-pc-3.0/dist/all.min.css'));
        $style2=file_get_contents(Html::loadFile('vendor/bootstrap/2.3.2/css/bootstrap.min.css'));
        $mpdf->WriteHTML($style1,1);
        $mpdf->WriteHTML($style2,1);
        //写入数据
        $mpdf->WriteHTML($res);
        $userName=$admin->real_name;
        unset($res);
        // 记录修改历史
        AdminLogDataOperate::add('用户【'.$userName.'】为【'.$corp_name.'】生成PDF', '系统', AdminLog::TYPE_ORDER_PDF, $order_id, AdminLog::ID2_T1);
        AdminLogDataOperate::add('用户【'.$userName.'】为【'.$corp_name.'】生成PDF', 'now_admin', AdminLog::TYPE_ORDER_PDF, $order_id, AdminLog::ID2_T2);
        $fileName=date('YmdHis',time()).'.pdf';
        //下载pdf
        $mpdf->Output($fileName,'D');
<?php

require_once __DIR__ . '/vendor/autoload.php';//(php7)

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('<h1>Hello world!</h1>');
$mpdf->Output();//$mpdf->Output('php://output');
require_once Yii::getAlias('@vendor/mpdf/autoload.php');
header('Content-Type: application/pdf'); // PDF文件
header('Content-Transfer-Encoding: binary');
header('Cache-Control: max-age=1');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: cache, must-revalidate');
header('Pragma: public');
header('Content-Disposition: attachment; filename=' . $title . '.pdf'); //指定文件名称
header("Expires: 0");
$mpdf = new \Mpdf\Mpdf();
$mpdf->SetDisplayMode('fullpage');
$mpdf->SetWatermarkImage($url,1,'F');
$mpdf->showWatermarkImage = true;
$mpdf->Output('php://output');