【php导出pdf文件】php将html 导出成pdf文件(MPDF60),支持完美分页,注意是完美!!

1、使用 MPDF60 包

2、防止中文乱码:修改MPDF/MPDF60/config.php中 

$this->autoLangToFont = true;
$this->autoScriptToLang = true;

3、引入类文件:mpdf.php

方式一:直接放在Application同级,通过include_once 引入

方式二:放在vendor/ 下面,引入

两种方式均可以,我这里以第一种示例,因为它在其他框架也可以使用

 

4、代码:(tp3.2.X示例)

//PC端,利用php服务端打印pdf文件 -- addBy 徐正宗 2018/07/18
    public function printPdf(){
        header("Content-type: text/html; charset=utf-8");
        
        $this->assign('title','下载文件');
        $id = I('id');
        if(!$id || !is_numeric($id)){
            $this->error('参数丢失');
        }else{
                
            //产品详情
            $list = M("product")->where('status in(1,-1,-2) and id='.intval($id))->find();
            if($list){
                //获取行程安排
                $xcap = M('product_plan')->where('pro_id='.intval($id))->select();
                 
                //html解码
                $list['pic'] = 'http://'.$_SERVER['HTTP_HOST'].'/'.$list['pic'];
                $list['detail'] = htmlspecialchars_decode($list['detail']);
                $list['fee_desc'] = htmlspecialchars_decode($list['fee_desc']);
                $list['notice'] = htmlspecialchars_decode($list['notice']);
                $list['shopping_notice'] = htmlspecialchars_decode($list['shopping_notice']);
                $list['before_buy'] = htmlspecialchars_decode($list['before_buy']); 
            }else{
                $list = array();
                $xcap = array(array());
            }
            $this->assign('_list',$list);
            $this->assign('_xcap',$xcap);
            
            
            //执行pdf文件生成            
            include_once  C('S_ROOT').'/../MPDF/MPDF60/mpdf.php'; //实际路径 /www/项目名/Application/../MPDF/MPDF60/mpdf.php
            //实例化mpdf
            $mpdf=new \mPDF('utf-8','A4','','宋体',0,0,20,10);
            
            //设置字体,解决中文乱码,前提是:修改MPDF/MPDF60/config.php中autoScriptToLang 和 autoLangToFont 均为true
            $mpdf->useAdobeCJK = true;
            //$mpdf->SetAutoFont(AUTOFONT_ALL);//使用6.0以上版本不需要
            
//             $mpdf=new \mPDF('+aCJK','A4','','',32,25,27,25,16,13);
//             $mpdf->autoLangToFont = true;
//             $mpdf->useAdobeCJK = true;
            //获取要生成的静态文件
            $html=$this->fetch('Product/detail_fetch');
            //$html = '中国';
            
            //echo $html;exit;
            

            //设置PDF页眉内容
            $header='<table width="95%" style="margin:0 auto;border-bottom: 1px solid #4F81BD; vertical-align: middle; font-family:
serif; font-size: 9pt; color: #000088;"><tr>
<td width="10%"></td>
<td width="80%" align="center" style="font-size:16px;color:#A0A0A0"></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"></td>
<td width="10%" style="text-align: left;">页码:{PAGENO}/{nb}</td>
</tr></table>';
            
            //添加页眉和页脚到pdf中
            $mpdf->SetHTMLHeader($header);
            $mpdf->SetHTMLFooter($footer);
            
            //设置pdf显示方式
            $mpdf->SetDisplayMode('fullpage');
            
            //设置pdf的尺寸为270mm*397mm
            //$mpdf->WriteHTML('<pagebreak sheet-size="270mm 397mm" />');
            
            //创建pdf文件
            $mpdf->WriteHTML($html);
            
            //删除pdf第一页(由于设置pdf尺寸导致多出了一页)
            //$mpdf->DeletePages(1,1);
            
            //输出pdf
            $mpdf->Output('旅游行程单.pdf','D');//可以写成下载此pdf   $mpdf->Output('文件名','D');
            
            exit;
            
        }
    }

5、效果:

 

posted @ 2018-07-18 17:19  PHP急先锋  阅读(6592)  评论(0编辑  收藏  举报