php 生成pdf之html2pdf

用这个有点难,做一些简单的笔记

https://github.com/spipu/html2pdf/blob/master/doc/install.md

页码和时间代码,在html代码中插入即可

[[date_y]]-[[date_m]]-[[date_d]] [[date_h]]:[[date_i]]
[[page_cu]]/[[page_nb]]

支持自带中文的配置

require __DIR__.'/vendor/autoload.php';

use Spipu\Html2Pdf\Html2Pdf;
use Spipu\Html2Pdf\Exception\Html2PdfException;
use Spipu\Html2Pdf\Exception\ExceptionFormatter;
//https://github.com/spipu/html2pdf/blob/master/doc/basic.md
$html2pdf = new Html2Pdf('L', 'A4', 'cn',true,'UTF-8',array(7.2, 12, 7.2, 8),false);

$html2pdf->setDefaultFont('stsongstdlight'); //add this line
$html2pdf->pdf->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM + 5);
//$html2pdf->pdf->setImageScale(1.25);
$html2pdf->pdf->setFontSubsetting(true);
$html2pdf->pdf->SetDisplayMode('fullpage');
$html2pdf->pdf->SetTitle('网页顶上标题');
$html2pdf->pdf->SetDefaultMonospacedFont('courier');
$html2pdf->pdf->SetFont('stsongstdlight', '', 12);
//自定义页码
$PgNo = getPageinfo($html2pdf);
function getPageinfo($html2pdf){
   return "第  " . $html2pdf->pdf->getAliasNumPage() . "  页,共  " . $html2pdf->pdf->getAliasNbPages()."  页";
}
$htmlcode=<<<EOD
<style type="text/css">
*{
    font-family: stsongstdlight;
}
.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%;
}
.pdftable td{
    background-color:#ffffff;
    height:24px;
    line-height:100%;
    padding:0;
    font-family:stsongstdlight;
    font-weight: bold;
    vertical-align:middle;
    text-align:center;
    word-break:break-all;
    word-wrap:break-word;
}
</style>

<div style="width:100%;">
<table style="width: 100%;" class="pdfhead">
<tr>
<td style="border:1px; text-align: center;width:100%;font-size: 24px;">
大标题就是我
</td>
</tr>
</table>
</div>
<br>
<div style="width:100%;" class="pdfhead">
<table style="width: 99%; 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: 72%">&nbsp;&nbsp;&nbsp;&nbsp;001</td>
    <td style="border:0px; text-align: left; width: 18%">第&nbsp;&nbsp;[[page_cu]]&nbsp;&nbsp;页,共&nbsp;&nbsp;[[page_nb]]&nbsp;&nbsp;页</td>
  </tr>

</table>
</div>

<div style="width:100%;" class="pdftable">
<table>
<thead>
  <tr style="width:100%;">
    <th rowspan="2" style="width: 5.6%">序号</th>
    <th rowspan="2" style="width: 16.82%">工号</th>
    <th rowspan="2" style="width: 7.6%">姓名</th>
    <th rowspan="2" style="width: 7.14%">绩效</th>
    <th rowspan="2" style="width: 7.22%">底薪</th>
    <th colspan="2" style="width: 15%">类别</th>
    <th rowspan="2" style="width: 6.3%">总计</th>
  </tr>
  <tr>
    <th>夜班</th>
    <th>双休</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>
</tbody>
<tfoot>
<tr>
<td colspan="8" style="text-align:left;border:0;overflow:hidden;">
我是重复出现的表格底
</td>
</tr>

</tfoot>
</table>
</div>
EOD;

$html2pdf->writeHTML($htmlcode);
$html2pdf->output();

以上就是整个代码的过程了,且看且学习。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注