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

安装:

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='
订单ID:'.$order_id.'
'; //设置PDF页脚内容 $footer='
ADD:上海市联航路1688弄 TEL:0000-000-000
'; //添加页眉和页脚到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('

Hello world!

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

 

转载于:https://www.cnblogs.com/liugp/p/10518024.html

你可能感兴趣的:(PHP yii2.0框架利用mpdf导出pdf)