美文网首页
基于mPDF6.0配置生成PDF文件

基于mPDF6.0配置生成PDF文件

作者: 你我的微笑 | 来源:发表于2019-12-10 16:31 被阅读0次

一、下载PDF文件,官网网址:https://github.com/mpdf/mpdf.github.io

二、引用
//引入mpdf类 require_once dirname(__FILE__) . ("../mpdf60/mpdf.php");

字符串进行拼接就可以加载使用了。

        //字段含义按顺序分别为:
        //$mode,$format,默认字体大小,默认字体,左页边距25(默认),右页边距(25),上页边距16,下页边距16,mgh:16,mgf:13,orientation
        $mpdf=new mPDF('utf-8','A4','','',10,10,16,16); //'utf-8' 或者 '+aCJK' 或者 'zh-CN'都可以显示中文
        //设置字体,解决中文乱码
        $mpdf -> useAdobeCJK = TRUE;
        $mpdf ->autoScriptToLang = true;
        $mpdf -> autoLangToFont = true;

        //设置PDF页眉内容
        $header='<table width="100%" style="margin:0 auto;border-bottom: 1px solid #4F81BD; vertical-align: middle; font-family:serif; font-size: 9pt; color: #000088;"><tr>'
        .'<td width="80%" align="left" style="font-size:13px;color:#A0A0A0">报告创建时间&nbsp;:&nbsp;'.date("Y-m-d H:i:s").'</td>'
        .'<td width="10%" style="text-align: right;"></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="10%"></td>'
        .'<td width="80%" align="center" style="font-size:14px;color:#A0A0A0">页码:{PAGENO}/{nb}</td>'
        .'<td width="10%" style="text-align: left;"></td>'
        .'</tr></table>';

        //添加页眉和页脚到pdf中

        $mpdf->SetHTMLHeader($header);
        $mpdf->SetHTMLFooter($footer);

        //$mpdf-> showImageErrors = true; //显示图片无法加载的原因,用于调试,注意的是,我的机子上gif格式的图片无法加载出来。
        //设置pdf显示方式
        $mpdf->SetDisplayMode('fullpage');
        //目录相关设置:
        //Remember bookmark levels start at 0(does not work inside tables)H1 - H6 must be uppercase
        //$this->h2bookmarks = array('H1'=>0, 'H2'=>1, 'H3'=>2);
        $mpdf->h2toc = array('H3'=>0,'H4'=>1,'H5'=>2);
        $mpdf->h2bookmarks = array('H3'=>0,'H4'=>1,'H5'=>2);
        $mpdf->mirrorMargins = 1;
        //是否缩进列表的第一级
        $mpdf->list_indent_first_level = 0;

        //导入外部css文件:
        $stylesheet1 = file_get_contents(CSS_PATH.'target.css');
        $mpdf->WriteHTML($stylesheet1,1);
        $mpdf->WriteHTML($html);  //$html中的内容即为变成pdf格式的html内容。

        $mpdf->AddPage();//新增一页
        $html = $header.$content;
        $mpdf->WriteHTML($html);

        if(!empty($waringData)||!empty($negativeData)){
            $mpdf->AddPage();//新增一页
            $html = $waringStr.$negativeStr;
            $mpdf->WriteHTML($html);
        }

        $fileName = $fromDataInfo["title"];
        //输出pdf文件
        $mpdf->Output($fileName,'I'); //'I'表示在线展示 'D'则显示下载窗口

相关文章

网友评论

      本文标题:基于mPDF6.0配置生成PDF文件

      本文链接:https://www.haomeiwen.com/subject/rsuggctx.html