条码
Barcode 模块(BarcodeGenerator、BarcodeRenderer)能直接在 PDF 中以向量方式绘制一维与二维条码,不需要外部图片。条码类型由 BarcodeType 与 Barcode2DType 枚举定义。所有方法皆返回 static,支持方法链。
基本范例
php
use Yeeefang\TcpdfNext\Core\Document;
$pdf = Document::create()
->addPage()
->setFont('Helvetica', '', 12)
->cell(0, 10, 'Product Barcodes', newLine: true)
// 1D:EAN-13
->write1DBarcode('4006381333931', 'EAN13', 10, 30, 80, 30, 0.4, [
'border' => false,
'text' => true,
'fgcolor' => [0, 0, 0],
])
// 1D:Code 128
->write1DBarcode('TCPDF-NEXT', 'C128', 10, 70, 80, 20, 0.4)
// 2D:QR Code
->write2DBarcode('https://tcpdf-next.dev', 'QRCODE,H', 10, 100, 50, 50, [
'fgcolor' => [0, 0, 0],
'bgcolor' => [255, 255, 255],
])
// 2D:DataMatrix
->write2DBarcode('Hello DataMatrix', 'DATAMATRIX', 70, 100, 40, 40);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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
1D 条码
write1DBarcode()
php
$pdf->write1DBarcode(
string $code, // 要编码的数据
string $type, // 条码类型字符串
float $x, // X 坐标
float $y, // Y 坐标
float $w, // 宽度
float $h, // 高度
float $xres = 0.4, // 最细条的宽度(用户单位)
array $style = [], // 样式选项
);1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
支持的 1D 条码类型(BarcodeType)
| 类型字符串 | 枚举值 | 说明 |
|---|---|---|
C39 | CODE_39 | Code 39 |
C93 | CODE_93 | Code 93 |
C128 | CODE_128 | Code 128(自动模式) |
C128A | CODE_128A | Code 128 子集 A |
C128B | CODE_128B | Code 128 子集 B |
C128C | CODE_128C | Code 128 子集 C |
EAN8 | EAN_8 | EAN-8 |
EAN13 | EAN_13 | EAN-13 |
UPCA | UPC_A | UPC-A |
UPCE | UPC_E | UPC-E |
I25 | I25 | Interleaved 2 of 5(交错式二五码) |
S25 | S25 | Standard 2 of 5(标准二五码) |
CODABAR | CODABAR | Codabar |
CODE11 | CODE_11 | Code 11 |
MSI | MSI | MSI Plessey |
POSTNET | POSTNET | POSTNET(美国邮政) |
PLANET | PLANET | PLANET(美国邮政) |
IMB | IMB | Intelligent Mail Barcode |
PHARMA | PHARMACODE | Pharmacode(制药业) |
常用 1D 条码范例
php
// EAN-13:零售商品条码
$pdf->write1DBarcode('4710088432001', 'EAN13', 10, 30, 60, 25, 0.4, [
'text' => true,
'fgcolor' => [0, 0, 0],
]);
// Code 128:物流与库存管理
$pdf->write1DBarcode('SHIP-2026-00042', 'C128', 10, 65, 80, 20, 0.4, [
'text' => true,
]);
// UPC-A:北美零售商品码
$pdf->write1DBarcode('012345678905', 'UPCA', 10, 95, 60, 25, 0.4, [
'text' => true,
]);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2D 条码
write2DBarcode()
php
$pdf->write2DBarcode(
string $code, // 要编码的数据
string $type, // 条码类型字符串
float $x, // X 坐标
float $y, // Y 坐标
float $w, // 宽度
float $h, // 高度
array $style = [], // 样式选项
);1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
支持的 2D 条码类型(Barcode2DType)
| 类型字符串 | 枚举值 | 说明 |
|---|---|---|
QRCODE,L | QR_CODE | QR Code — 低容错(约 7%) |
QRCODE,M | QR_CODE | QR Code — 中容错(约 15%) |
QRCODE,Q | QR_CODE | QR Code — 中高容错(约 25%) |
QRCODE,H | QR_CODE | QR Code — 高容错(约 30%) |
DATAMATRIX | DATAMATRIX | DataMatrix |
PDF417 | PDF417 | PDF417 |
QR Code 容错等级选择
| 等级 | 容错率 | 适用场景 |
|---|---|---|
L | 约 7% | 数据量最小,适合受控环境 |
M | 约 15% | 一般用途,平衡数据量与容错 |
Q | 约 25% | 可能会有部分遮挡的场景 |
H | 约 30% | 需要叠加 Logo 或恶劣扫描条件 |
常用 2D 条码范例
php
// QR Code:网址链接
$pdf->write2DBarcode('https://tcpdf-next.dev', 'QRCODE,H', 10, 30, 50, 50, [
'fgcolor' => [0, 0, 0],
'bgcolor' => [255, 255, 255],
]);
// DataMatrix:工业产品序号
$pdf->write2DBarcode('01034531200000111719112510ABCD1234', 'DATAMATRIX', 70, 30, 40, 40);
// PDF417:身份证件或运输标签
$pdf->write2DBarcode('PASSPORT|WANG|DA-MING|M|1990-01-15|A123456789', 'PDF417', 10, 90, 80, 30);1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
样式选项
$style 数组控制 1D 与 2D 条码的外观:
| 键 | 类型 | 说明 |
|---|---|---|
border | bool | 是否在条码周围绘制边框 |
padding | float|array | 边框内的间距 |
fgcolor | array | 前景色(条码线条)[r, g, b] |
bgcolor | array | 背景色 [r, g, b] |
text | bool | 是否在 1D 条码下方显示可读文字 |
stretch | bool | 是否拉伸条码以填满指定宽度 |
自定义样式范例
php
// 深蓝色前景搭配浅色背景
$pdf->write1DBarcode('CUSTOM-001', 'C128', 10, 30, 80, 25, 0.4, [
'border' => true,
'padding' => 4,
'text' => true,
'fgcolor' => [0, 51, 102],
'bgcolor' => [240, 248, 255],
]);1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
完整范例:产品标签
php
use Yeeefang\TcpdfNext\Core\Document;
$pdf = Document::create()
->addPage()
->setFont('Helvetica', 'B', 14)
->cell(0, 10, 'Widget Pro X1', newLine: true)
->setFont('Helvetica', '', 10)
->cell(0, 8, 'SKU: WPX1-2026', newLine: true)
// EAN-13 商品条码
->write1DBarcode('4006381333931', 'EAN13', 10, 35, 60, 25, 0.4, [
'text' => true,
'fgcolor' => [0, 0, 0],
])
// QR Code 链接到产品页面
->write2DBarcode('https://example.com/product/123', 'QRCODE,H', 80, 30, 30, 30, [
'fgcolor' => [33, 37, 41],
'bgcolor' => [255, 255, 255],
]);1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
提示
- 当 QR Code 上方需要叠加 Logo 时,请使用容错等级
H(高),以确保即使部分被遮挡仍然能扫描。 - 1D 条码的
$xres参数控制最细条的宽度,数值越小条码越紧密,但过小可能导致扫描困难。 - 所有条码皆以向量方式绘制,放大后不会失真,非常适合用于高品质打印。
- DataMatrix 特别适合在小空间内编码大量数据,例如电子零件标签。