上次,简单的提了使用PHPWord生成Word文档,这次,就简单的讲下tcpdf生成PDF文档。效果图如下:
首先,引入tcpdf类库,使用composer
composer require tecnickcom/tcpdf
代码示例 :
// 设置文档信息
$pdf->SetCreator('Hello world');
$pdf->SetAuthor('dyk');
$pdf->SetTitle('TCPDF示例');
$pdf->SetSubject('TCPDF示例');
$pdf->SetKeywords('TCPDF, PDF, PHP');
// 设置页眉和页脚信息
$pdf->SetHeaderData('tcpdf_logo.jpg', 30, 'www.marchsoft.cn', '三月软件!', [0, 64, 255], [0, 64, 128]);
$pdf->setFooterData([0, 64, 0], [0, 64, 128]);
// 设置页眉和页脚字体
$pdf->setHeaderFont(['stsongstdlight', '', '10']);
$pdf->setFooterFont(['helvetica', '', '8']);
// 设置默认等宽字体
$pdf->SetDefaultMonospacedFont('courier');
// 设置间距
$pdf->SetMargins(15, 15, 15);//页面间隔
$pdf->SetHeaderMargin(5);//页眉top间隔
$pdf->SetFooterMargin(10);//页脚bottom间隔
// 设置分页
$pdf->SetAutoPageBreak(true, 25);
// 设置自动换页
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// 设置图像比例因子
$pdf->setImageScale(1.25);
// 设置默认字体构造子集模式
$pdf->setFontSubsetting(true);
// 设置字体 stsongstdlight支持中文
$pdf->SetFont('stsongstdlight', '', 14);
// 添加一页
$pdf->AddPage();
$pdf->Ln(5);//换行符
$html = '
<table width="400" border="1">
<tr>
<th align="left">消费项目</th>
<th align="right">一月</th>
<th align="right">二月</th>
</tr>
<tr>
<td align="left">衣服</td>
<td align="right">$241.10</td>
<td align="right">$50.20</td>
</tr>
<tr>
<td align="left">化妆品</td>
<td align="right">$30.00</td>
<td align="right">$44.45</td>
</tr>
<tr>
<td align="left">食物</td>
<td align="right">$730.40</td>
<td align="right">$650.00</td>
</tr>
<tr>
<th align="left">总计</th>
<th align="right">$1001.50</th>
<th align="right">$744.65</th>
</tr>
</table>
';
$pdf->writeHTML($html, true, false, true, false, '');
//输出PDF
$pdf->Output('t.pdf', 'I');//I输出、D下载
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
官方文档上有示例,附:https://tcpdf.org/examples/
官方文档:https://tcpdf.org/
有兴趣的话,可以深入的了解一下