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

先貼出mpdf的官方手冊:https://mpdf.github.io/

 

安裝:

composer require mpdf/mpdf

 使用:

use Mpdf\Mpdf;

 代碼:

        //獲取頁面內容
        $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');

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章