插件窝 干货文章 PHP使用mpdf实现导出pdf文件功能

PHP使用mpdf实现导出pdf文件功能

gt lt style border 741    来源:    2024-10-18

mpdf的开发文档地址:

Supported CSS – CSS & Stylesheets – mPDF Manual

1.加载依赖库

composer require mpdf/mpdf

2.页面

$html = <<<EOD
 
 
<body style="background:url($img1);"  lang="zh-CN">
    <div style="background:rgba(null,255,255,0.3);">
        <div style="width: 12rem;height: 15rem;float: right;">
            $avatar
        </div>
        <div style="padding-top: 5rem;padding-left: 2rem;">
            <span style="font-size: 20pt;font-weight: bold;">$strTitle</span>
<table  style="color: #666666;padding-top: 1rem;font-size: 14pt;line-height: 16pt;" >
                <tr>
                    <td style="">
                    姓名:$studentName
                    </td>
                    <td style="padding-left: 2rem;">
                    性别:$sex
                    </td>
                    <td style="">
                    出生年月:$birthday
                    </td>
                </tr>
                <tr>
                    <td>
                    民族:$nationality
                    </td>
                    <td style="">
                    政治面貌:$political
                    </td>
                    <td style="padding-left: 2rem;">
                    电话:$phone
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div style="background-color: white;padding-bottom: initial;">
 
    <table   style="text-align:center;font-size:20pt;"  >
    </table>
    
 
 
<h2></h2><h2></h2>
<h2 style="padding-left: 2rem">技能特长</h2>
<table  style="text-align:left;line-height:200%;font-size:14pt;color: #666666;" >
$strSkills
</table>
 
 
<h2></h2>
<h2 style="padding-left: 2rem">成绩单</h2>
<table  style="text-align:left;line-height:200%;font-size:12pt;border-collapse: collapse;margin-left: 2rem;width: 95%;" >
<tr >
<th style="border: 1px solid black;border-right: 0px;">总分:$scoreTotal</th>
<th style="border: 1px solid black;border-right: 0px;border-left: 0px;"></th>
<th style="border: 1px solid black;border-right: 0px;border-left: 0px;"></th>
<th style="border: 1px solid black;border-right: 0px;border-left: 0px;"></th>
<th style="border: 1px solid black;border-left: 0px;"></th>
</tr>
$strActivity
</table>
 
 
 
<h2></h2>
<h2 style="padding-left: 2rem;">获奖证书</h2>
<table  style="text-align:left;line-height:200%;font-size:12pt;border-collapse: collapse;margin-left: 2rem;width: 95%;" >
<tr>
<th style="border: 1px solid black;color: #333333;">项目名称</th>
<th style="border: 1px solid black;color: #333333;">等级</th>
<th style="border: 1px solid black;color: #333333;">获得时间</th>
<th style="border: 1px solid black;color: #333333;">经历描述</th>
</tr>
$strCertificate
 
</table>
 
 
 
 
<h2></h2>
<h2 style="padding-left: 2rem;">实践经历</h2>
$strTraining
 
<h2></h2>
<h2 style="padding-left: 2rem;">个人风采</h2>
$strPersona
 
 
    </div>
</body>
            
EOD;

3.导出

require_once 'vendor/autoload.php';
        $mpdf=new Mpdf([
            'mode' => '',
            'format' => 'A4',
            'default_font_size' => 0,
            'default_font' => 'sans',
            'margin_left' => 10,
            'margin_right' => 10,
            'margin_top' => 10,
            'margin_bottom' => 10,
            'margin_header' => 9,
            'margin_footer' => 9,
            'orientation' => 'P',
        ]);
        $mpdf->useAdobeCJK = true;
        $mpdf->autoScriptToLang = true;//支持中文设置
        $mpdf->autoLangToFont = true;//支持中文设置
        $mpdf->setAutoTopMargin = 'stretch';//设置自动顶部边距
        $mpdf->setAutoBottomMargin = 'stretch';//设置自动低部边距
        $mpdf->AddPage();
 
        //获取配置文字或者logo
        $conf=M("confScoreList")->find();
        //设置页眉
        $SetHeader = '<table class="header" style="text-align: right;width: 100%;"> 
<tr>
   <td width="33%" style="text-align: left;font-size: 10pt;color: #999999;"> <img src="'.K_PATH_IMAGES.$conf['logo'].'" alt=""> </td>
   <td width="33%" style="text-align: right;color: #999999;">'.$conf['headerRight'].'</td>
   </tr>
</table>';
 
        $mpdf->SetHeader($SetHeader);
        //这是一个页脚的范例{PAGENO}是当前的页数,{nb}是总共的页数
        //<td width="33%" style="text-align: center;font-size: 10pt;">第 {PAGENO} 页&nbsp;&nbsp;&nbsp;&nbsp;共 {nb} 页</td>
        $setFooter = '<table class="footer" style="text-align: right;width: 100%;"> 
<tr>
   <td width="33%" style="text-align: left;font-size: 10pt;color: #999999;">'.$conf['footerLeft'].'</td>
   <td width="33%" style="text-align: right;color: #999999;">'.$conf['footerRight'].'</td>
   </tr>
</table>';
 
        $mpdf->setFooter($setFooter);
        //设置中文编码
        $mpdf->WriteHTML($html);
        //导出pdf文件重命名
//        $mpdf->Output($dataResume['resumeName'] . '.pdf', true);
        $mpdf->Output();
        exit;

到此这篇关于PHP使用mpdf实现导出pdf文件功能的文章就介绍到这了,更多相关PHP mpdf导出pdf内容请搜索插件窝以前的文章或继续浏览下面的相关文章希望大家以后多多支持插件窝!