【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、效果:

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用第三方库如TCPDFmPDF来实现在PHP中将网页导出PDF文件。 以下是使用TCPDF实现的示例代码: ```php <?php require_once('tcpdf/tcpdf.php'); // 创建PDF对象 $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // 设置文档信息 $pdf->SetCreator(PDF_CREATOR); $pdf->SetAuthor('作者'); $pdf->SetTitle('标题'); $pdf->SetSubject('主题'); $pdf->SetKeywords('关键词'); // 设置页眉和页脚信息 $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING); $pdf->setFooterData(array(0,64,0), array(0,64,128)); // 设置页眉和页脚字体 $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); // 设置默认字体 $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); // 设置间距 $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); $pdf->SetHeaderMargin(PDF_MARGIN_HEADER); $pdf->SetFooterMargin(PDF_MARGIN_FOOTER); // 设置自动分页 $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); // 设置图像比例因子 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); // 设置字体 $pdf->SetFont('stsongstdlight', '', 14); // 将HTML转换为PDF $pdf->AddPage(); $html = '<h1>Hello, world!</h1><p>This is a sample PDF document created with TCPDF.</p>'; $pdf->writeHTML($html, true, false, true, false, ''); // 输出PDF文件 $pdf->Output('example.pdf', 'D'); ``` 以上代码将在当前目录中生名为"example.pdf"的PDF文件,可以在浏览器中下载。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值