
mpdfdemo.png
因为最近最一个案例,用到生成pdf,所以做一个笔记。
找了各种源码,比如:Tcpdf,html2pdf,dompdf等等,各种尝试,遇到各种不爽。
也就是大部分对html代码标签和css样式支持的不完全,当然各自有各自的优点,最后选择Mpdf
Mpdf 支持大部分的css样式,教程完整,表格的支持也略好,支持thead但是它不支持troot
其官网:
安装:
composer require mpdf/mpdf
请参照以下的脚手架,你可以自己修改合适的内容。
<?php
require __DIR__.'/vendor/autoload.php';
$tempDir = __DIR__.'/Cache/tmp';
if(!is_dir($tempDir)){
mkdir($tempDir,0777,true);
};
$mpdf = new \Mpdf\Mpdf([
'mode' => 'utf-8',
//'format' => 'A4-L', //横向A4
'debug' => true,
//'tabSpaces' => 4,
//'ignore_invalid_utf8' => true,
'orientation' => 'L', //横向
"autoScriptToLang"=>true, //自动适配语言(支持中文)
"autoLangToFont"=>true, //自动选字体
"default_font_size"=>10, //默认字号px
"default_font"=>'simsun', //自定义字体,这个后面讲解
//"tempDir"=>$tempDir, //缓存目录
'margin_top' => 0, //上边距
'margin_left' => 7, //左边距
'margin_right' => 7, //右边距
//'mirrorMargins' => true,
"setAutoTopMargin"=>"stretch", //自动适应顶边距
//"setAutoBottomMargin"=>"stretch",
"autoMarginPadding"=>0 //边距和pad
]);
//$mpdf->default_font='STFangsong'; //courier SimHei
$mpdf->defaultheaderline = 0; //删除header底线
$mpdf->defaultfooterline = 0; //删除footer顶线
$csscode=<<<EOD
<style type="text/css">
*{
}
.pdftable table{
display:table;
table-layout: fixed;
border:0px;
margin:0px;
border-collapse:collapse;
border-spacing:0;
width: 100%;
}
.pdftable table, td, th
{
border:1px solid black;
}
.pdftable tr{
width: 100%;
}
</style>
EOD;
$mpdf->WriteHTML($csscode,\Mpdf\HTMLParserMode::HEADER_CSS); //写样式
$headhtml=<<<EOD
<div style="width:100%;" class="pdfhead">
<table style="width: 100%;">
<tr>
<td style="border:1px; text-align: center;width:100%;font-size: 23px;font-family:simhei">
我是一个大标题
</td>
</tr>
</table>
<br /><br /><br />
<table style="width: 100%; border:0;border-spacing:0;border-collapse:collapse;">
<tr>
<td style="border:0px; text-align: right; width: 10%">我是文档:</td>
<td style="border:0px; text-align: left; width: 70.5%"> 001</td>
<td style="border:0px; text-align: left; width: 19.5%">第 {PAGENO} 页,共 {nbpg} 页</td>
</tr>
</table>
</div>
EOD;
$mpdf->SetHTMLHeader($headhtml); //写入头
$htmlcode=<<<EOD
<div style="width:100%;" class="pdftable">
<table style="width:100%;">
<thead>
<tr style="width:100%;">
<th rowspan="2" style="width: 16mm">序号</th>
<th rowspan="2" style="width: 48.3mm">工号</th>
<th rowspan="2" style="width: 22.1mm">姓名</th>
<th rowspan="2" style="width: 20.4mm">绩效</th>
<th rowspan="2" style="width: 21.1mm">底薪</th>
<th colspan="2" style="width: 43.1mm">分类</th>
<th rowspan="2" style="width: 17.7mm">总计</th>
</tr>
<tr>
<th style="border-left:0;width: 21.5mm">夜班</th>
<th style="border-left:0;width: 21.5mm">双休</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>007</td>
<td>张同学</td>
<td>5000</td>
<td>5000</td>
<td>2000</td>
<td>1500</td>
<td>13500</td>
</tr>
<tr><td colspan="8" style="text-align:left;border:0;width:100%;">我是底座</td></tr>
</tbody>
</table>
</div>
EOD;
$mpdf->WriteHTML($htmlcode,\Mpdf\HTMLParserMode::HTML_BODY); //写内容
$mpdf->SetHTMLFooter('<div>我是小尾巴</div>');
//文字水印(每页出现)
$mpdf->watermark_font='simsun';
$mpdf->SetWatermarkText('freebasic.cn',0.1);
$mpdf->showWatermarkText = true;
//图片水印 (每页出现)
$mpdf->SetWatermarkImage('test.png', 1, array(60,60), array(75,3));
//分别为图片路径、未知、宽高、坐标XY
$mpdf->showWatermarkImage = true;
//以下输出二选一
$mpdf->output(); //只输出到浏览器
$mpdf->output('document.pdf','D'); //下载
关于字体的设置,参考
php 生成PDF过程笔记mpdf
http://www.freebasic.cn/p/2263.html
生成PDF大概就这样,更多的教程,参考官方
https://mpdf.github.io/
有其它问题的话,请发言。一起研究看看。