使用TCPDF操作pdf
# 按转依赖
composer require tecnickcom/tcpdf
1
# 基本操作
// 创建PDF实例,第三个参数设置为'L'表示横向
$pdf = new TCPDF('L', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// 设置文档信息
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('mrcdh');
$pdf->SetTitle('title');
// 设置默认字体
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// 设置边距
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
// 设置自动分页
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// 设置图像比例因子
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// 添加一页
$pdf->AddPage();
//stsongstdlight为支持中文的内置字体
// --- 添加水印 ----
$currentX = $pdf->GetX();
$currentY = $pdf->GetY();
// 设置水印样式
$pdf->SetFont('stsongstdlight', 'B', 60); // 字体、粗体、大小
$pdf->SetTextColor(235, 235, 235); // 浅灰色(透明度通过颜色深浅模拟)
// 获取页面宽度和高度(用于居中水印)
$pageWidth = $pdf->getPageWidth();
$pageHeight = $pdf->getPageHeight();
// 计算水印位置(居中)
$x = $pageWidth / 2;
$y = $pageHeight / 3;
// 绘制水印(旋转45度,居中显示)
$pdf->StartTransform(); // 开始变换
$pdf->Rotate(45, $x, $y); // 旋转45度,中心点为页面中心
$pdf->Text($x - 100, $y, '水印'); // 文本内容(x-100 是为了居中调整)
$pdf->StopTransform(); // 结束变换
$pdf->SetX($currentX); // 恢复X位置
$pdf->SetY($currentY); // 恢复Y位置
$pdf->SetTextColor(0, 0, 0); // 回复颜色
// --- 添加水印结束 ----
// 表格数据
$data = [
['姓名', '年龄', '性别', '手机号'],
['张三', '18', '男', '12345678901'],
['王五', '17', '女', '12345678901'],
];
// 表格列宽(总宽度根据页面宽度调整),好像总宽度为250
$colWidths = [50, 50, 50, 100];
// 表格边框样式
$border = 1; // 1=显示边框,0=不显示
// 单元格内边距(通过Cell方法的参数控制)
$padding = 5; // 内边距值(单位:mm)
// 设置字体:字体名称、样式(B=粗体)、大小(20pt)
$pdf->SetFont('stsongstdlight', 'B', 22);
// Cell(宽度, 高度, 内容, 边框, 换行, 对齐方式, 是否填充, 链接)
// 宽度0表示自动适应内容,对齐方式:C=居中, L=左对齐, R=右对齐
$pdf->Cell(0, 15, "头部信息", 0, 1, 'C');
$pdf->SetFont('stsongstdlight', 'B', 16);
$pdf->Cell(0, 15, "信息", 0, 1, 'L');
// 设置字体
$pdf->SetFont('stsongstdlight', '', 14);
// 绘制表格内容
$pdf->SetFillColor(255, 255, 255); // 内容行背景色
for ($i = 0; $i < count($data); $i++) {
foreach ($data[$i] as $j => $cellValue) {
$width = $colWidths[$j];
if($i%2 == 1){
$pdf->MultiCell(
$width,
16,
$cellValue,
1,
'C', // 对齐方式
false,
0, // 不换行(0表示继续在同一行)
'', // 不指定X坐标(自动计算)
'', // 不指定Y坐标(自动计算)
true, // 允许自动调整高度
0,
false,
true,
16, // 垂直对齐时要设置,一般=H高度
'M' // 垂直对齐方式
);
}else{
$pdf->Cell(
$width,
10, // 高度包含上下内边距
$cellValue,
$border,
0,
'C', // 左对齐
false,
0,
0,
false,
0,
false
);
}
}
$pdf->Ln(); // 换行
}
$pdf->SetFont('stsongstdlight', 'B', 20);
$pdf->Cell(0, 15, "这个个cell", 0, 1, 'C');
// 关闭并输出PDF文档
// -F 表示保存为文件,不输出到浏览器
// -I 表示输出到浏览器
$pdf->Output("example.pdf", 'F');
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118