php利用tcpdf生成签名盖章的pdf文件并支持中文
20 May 2025
前言
TCPDF 概述:基于 PHP 的开源库,用于服务器端动态生成 PDF 文档,自 2002 年由 Nicola Asuni 团队维护,支持多种页面格式、自定义布局和度量单位等,是 PHP 开发者生成 PDF 的常用工具。
仓库
https://github.com/tecnickcom/TCPDF
代码示例
php7.3以上
<?php
header('Content-type:text/html;charset=utf-8');
//引入工具
require_once('./TCPDF/tcpdf.php');
//新建一个PDF文档
//L 横排 P竖排
$orientation='P';
//Unit:设置页面的单位。pt:点为单位,mm:毫米为单位,cm:厘米为单位,in:英尺为单位
$unit='pt';
//Format:设置打印格式,一般设置为A4
$format='A4';
//Unicode:为true,输入的文本为Unicode字符文本
$unicode=true;
//Encoding:设置编码格式,默认为utf-8
$encoding='UTF-8';
//Diskcache:为true,通过使用文件系统的临时缓存数据减少RAM的内存使用
$diskcache=false;
$pdfa=false;
//实例化
$pdf = new TCPDF($orientation, $unit, $format, $unicode, $encoding, $diskcache, $pdfa);
// 设置中文字体
$pdf->SetFont('cid0cs', '', 12, '', true);
//设置文件信息
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor("System");
$pdf->SetTitle("授权作品部分清单");
$pdf->SetSubject('TCPDF Document');
$pdf->SetKeywords('TCPDF, PDF, document');
// 页眉距离顶部的距离
$pdf->SetHeaderMargin('0');
//删除预定义的打印 页眉/页尾
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
//设置默认等宽字体
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// 设置左、上、右的间距
$pdf->SetMargins('30', '30', '30');
$pdf->setCellPaddings(2, 2, 2, 2);
//添加一个页面
$pdf->AddPage();
// 表格内容
$html = '<h3 style="text-align:center;">附件一:授权作品部分清单</h3>';
$html .= '<table border="1" cellpadding="4" cellspacing="0" style="font-size:11pt;">
<tr style="background-color:#f5f5f5;">
<th width="30">序号</th>
<th width="170">作品名称</th>
<th width="90">作者名称</th>
<th width="60">字数</th>
<th width="40">字段</th>
<th width="80">授权开始时间</th>
<th width="80">授权结束时间</th>
</tr>';
$data = [
[1, '我,爽文大男主,能受楚总的气?', '爱喝冰可乐', '662094', '长篇', '2024/8/19', '2027/8/18'],
[2, '五年后,乖乖崽天才双宝炸了王府', '舒妖', '2280663', '长篇', '2024/8/19', '2027/8/18'],
[3, '大明:秋后问斩,太子是我女儿', '霸道总裁胖总', '595204', '长篇', '2024/8/19', '2027/8/18'],
[4, '让你穿越世界,你去霸霸理羊毛?', '猛更兽', '426662', '长篇', '2024/8/19', '2027/8/18'],
[5, '加钱就破解成败,毁于史中群神', '十四的梦', '440467', '长篇', '2024/8/19', '2027/8/18'],
[6, '修为万倍返还,徒儿谎装努力', '不才在下卜卜神', '292024', '长篇', '2024/8/19', '2027/8/18'],
[7, '大乾第一剑姬,女帝求我别造反', '九木', '408350', '长篇', '2024/8/19', '2027/8/18'],
[8, '让你下山娶妻,谁让你当天师的?', '拉姆哥', '474633', '长篇', '2024/8/19', '2027/8/18'],
[9, '离婚你哭强,我再娶你哭什么', '天降紫微星', '537897', '长篇', '2024/8/19', '2027/8/18'],
[10, '你说你去她干嘛,她刚从精神病院出来', '黎黎', '256141', '长篇', '2024/8/19', '2027/8/18'],
[11, '抗战:从成为上海港大学开始', '锦衣仰匡', '358957', '长篇', '2024/8/19', '2027/8/18'],
];
foreach ($data as $row) {
$html .= '<tr>';
foreach ($row as $i => $cell) {
$width = [30,170,90,60,40,80,80][$i];
$html .= '<td width="'.$width.'">'.$cell.'</td>';
}
$html .= '</tr>';
}
$html .= '</table>';
// 签章行内容,直接跟在表格后面
$html .= '<br><br><table width="100%"><tr>';
$html .= '<br><br><br><br><td width="50%" align="center"><span id="auth_seal">授权方(盖章):</span></td>';
$html .= '<td width="50%" align="center">被授权方(盖章):</td>';
$html .= '</tr></table>';
$pdf->writeHTML($html, true, false, true, false, '');
// 获取"授权方(盖章):"的Y坐标,盖章
$pageWidth = $pdf->getPageWidth();
$marginLeft = 30;
$cellWidth = ($pageWidth - 2 * $marginLeft) / 2;
$sealWidth = 100;
$sealHeight = 100;
// 获取当前Y坐标(签章行的顶部)
$sealY = $pdf->GetY() - 80; // 适当向上偏移让印章中心和文字重叠
$sealX = $marginLeft + ($cellWidth - $sealWidth) / 2;
$pdf->Image('./signature.png', $sealX, $sealY, $sealWidth, $sealHeight, 'PNG');
// 输出PDF
$pdf->Output('授权书单.pdf', 'I');