Skip to content

条码

Barcode 模块(BarcodeGeneratorBarcodeRenderer)能直接在 PDF 中以向量方式绘制一维与二维条码,不需要外部图片。条码类型由 BarcodeTypeBarcode2DType 枚举定义。所有方法皆返回 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);

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 = [],     // 样式选项
);

支持的 1D 条码类型(BarcodeType)

类型字符串枚举值说明
C39CODE_39Code 39
C93CODE_93Code 93
C128CODE_128Code 128(自动模式)
C128ACODE_128ACode 128 子集 A
C128BCODE_128BCode 128 子集 B
C128CCODE_128CCode 128 子集 C
EAN8EAN_8EAN-8
EAN13EAN_13EAN-13
UPCAUPC_AUPC-A
UPCEUPC_EUPC-E
I25I25Interleaved 2 of 5(交错式二五码)
S25S25Standard 2 of 5(标准二五码)
CODABARCODABARCodabar
CODE11CODE_11Code 11
MSIMSIMSI Plessey
POSTNETPOSTNETPOSTNET(美国邮政)
PLANETPLANETPLANET(美国邮政)
IMBIMBIntelligent Mail Barcode
PHARMAPHARMACODEPharmacode(制药业)

常用 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,
]);

2D 条码

write2DBarcode()

php
$pdf->write2DBarcode(
    string $code,           // 要编码的数据
    string $type,           // 条码类型字符串
    float  $x,              // X 坐标
    float  $y,              // Y 坐标
    float  $w,              // 宽度
    float  $h,              // 高度
    array  $style = [],     // 样式选项
);

支持的 2D 条码类型(Barcode2DType)

类型字符串枚举值说明
QRCODE,LQR_CODEQR Code — 低容错(约 7%)
QRCODE,MQR_CODEQR Code — 中容错(约 15%)
QRCODE,QQR_CODEQR Code — 中高容错(约 25%)
QRCODE,HQR_CODEQR Code — 高容错(约 30%)
DATAMATRIXDATAMATRIXDataMatrix
PDF417PDF417PDF417

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);

样式选项

$style 数组控制 1D 与 2D 条码的外观:

类型说明
borderbool是否在条码周围绘制边框
paddingfloat|array边框内的间距
fgcolorarray前景色(条码线条)[r, g, b]
bgcolorarray背景色 [r, g, b]
textbool是否在 1D 条码下方显示可读文字
stretchbool是否拉伸条码以填满指定宽度

自定义样式范例

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],
]);

完整范例:产品标签

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],
    ]);

提示

  • 当 QR Code 上方需要叠加 Logo 时,请使用容错等级 H(高),以确保即使部分被遮挡仍然能扫描。
  • 1D 条码的 $xres 参数控制最细条的宽度,数值越小条码越紧密,但过小可能导致扫描困难。
  • 所有条码皆以向量方式绘制,放大后不会失真,非常适合用于高品质打印。
  • DataMatrix 特别适合在小空间内编码大量数据,例如电子零件标签。

以 LGPL-3.0-or-later 许可证发布。