特殊條碼格式¶
NextPDF Core 已內建 QR Code、Code 128、EAN-13、EAN-8 與 UPC-A 等常用條碼。Pro 套件額外提供 12 種工業及政府場景所需的特殊條碼格式,全部以向量圖形渲染,確保在任何列印解析度下保持最佳可讀性。
Pro 條碼格式總覽¶
| 格式 | 標準 | 資料類型 | 最大容量 | 典型場景 |
|---|---|---|---|---|
| DotCode | ISO/IEC 24723 | 全字元 | 可變 | 高速工業印刷、醫藥包裝 |
| GS1 DataMatrix | ISO/IEC 16022 + GS1 | 全字元 | 3,116 字元 | 供應鏈、醫療器材追蹤 |
| GS1 QR Code | ISO/IEC 18004 + GS1 | 全字元 | 7,089 字元 | 消費品行銷、GS1 數位連結 |
| HanXin Code | GB/T 33993-2017 | 全字元(含漢字) | 7,827 數字 | 中國政府文件、中文編碼 |
| Intelligent Mail | USPS IMb | 數字 | 31 位數 | 美國郵政信件追蹤 |
| JabCode | ISO 23634:2022 | 全字元 | 33,000+ 字元 | 文件嵌入、高密度資料 |
| Micro QR Code | ISO/IEC 18004 附錄E | 全字元 | 35 字元 | 小型元件標記、PCB |
| MaxiCode | ISO/IEC 16023 | 全字元 | 93 字元 | UPS 物流標籤 |
| PDF417 | ISO/IEC 15438 | 全字元 | 1,850 字元 | 身分證件、航班登機證 |
| Data Matrix | ISO/IEC 16022 | 全字元 | 3,116 字元 | 工業製造、電子元件 |
| Aztec Code | ISO/IEC 24778 | 全字元 | 3,832 字元 | 交通票務、身分驗證 |
| Code 93 | — | 43 字元集 | 可變 | 零售輔助,向後相容 |
DotCode¶
DotCode 專為高速噴墨印刷機設計,圓點陣列結構在高速移動的包裝線上具有極高的掃描可靠性:
use NextPDF\Pro\Barcode\DotCode\DotCodeGenerator;
use NextPDF\Pro\Barcode\DotCode\DotCodeOptions;
use NextPDF\Pro\Document\ProDocument;
$doc = ProDocument::createStandalone();
$doc->addPage();
$generator = new DotCodeGenerator(
DotCodeOptions::create(
data: 'BATCH-2026-03-001;LOT-A;EXP-2028-12',
columns: 30, // 欄數(奇數)
moduleSize: 0.5, // 點的直徑(mm)
aspectRatio: 3.0, // 高寬比
encapsulated: false, // 是否加入靜止區邊框
)
);
$generator->render($doc, x: 20.0, y: 50.0);
GS1 DataMatrix 與 GS1 QR Code¶
GS1 條碼使用應用識別碼(AI,Application Identifier)標準化資料格式,確保跨供應鏈系統的互通性:
use NextPDF\Pro\Barcode\Gs1\Gs1DataMatrixGenerator;
use NextPDF\Pro\Barcode\Gs1\Gs1QrCodeGenerator;
use NextPDF\Pro\Barcode\Gs1\Gs1Element;
use NextPDF\Pro\Barcode\Gs1\Gs1DataBuilder;
// 使用 AI 碼建構 GS1 資料字串
$gs1Data = Gs1DataBuilder::create()
->add(Gs1Element::GTIN, '09501101020917') // AI (01) 全球商品識別碼
->add(Gs1Element::BATCH_LOT, 'BATCH-2026-001') // AI (10) 批次/批號
->add(Gs1Element::EXPIRY_DATE, '291231') // AI (17) 到期日 YYMMDD
->add(Gs1Element::SERIAL_NUMBER, 'SN-001234') // AI (21) 序號
->build();
// GS1 DataMatrix(醫療器材常用)
$dataMatrix = new Gs1DataMatrixGenerator();
$dataMatrix->setData($gs1Data)
->setSize(10.0) // 符號尺寸(mm)
->setErrorCorrection('ECC200');
$dataMatrix->render($doc, x: 20.0, y: 80.0);
// GS1 QR Code(消費品行銷)
$qrCode = new Gs1QrCodeGenerator();
$qrCode->setData($gs1Data)
->setSize(20.0)
->setErrorCorrectionLevel('M'); // L/M/Q/H
$qrCode->render($doc, x: 60.0, y: 80.0);
HanXin Code¶
HanXin Code(漢信碼)是中國國家標準 GB/T 33993-2017 定義的二維條碼,原生支援漢字編碼,儲存相同中文內容所需空間比 QR Code 小約 30%:
use NextPDF\Pro\Barcode\HanXin\HanXinCodeGenerator;
use NextPDF\Pro\Barcode\HanXin\HanXinOptions;
use NextPDF\Pro\Barcode\HanXin\HanXinErrorCorrection;
use NextPDF\Pro\Barcode\HanXin\HanXinEncoding;
$generator = new HanXinCodeGenerator(
HanXinOptions::create(
data: '中華民國政府文件編號:A202600001',
version: 23, // 版本 1–84(影響容量)
errorCorrection: HanXinErrorCorrection::L2, // L1/L2/L3/L4
encoding: HanXinEncoding::Mixed, // 自動選擇最佳編碼模式
moduleSize: 2.0, // 模組尺寸(pt)
)
);
$generator->render($doc, x: 20.0, y: 120.0);
Intelligent Mail Barcode(IMb)¶
美國郵政服務(USPS)標準,編碼郵遞區號、追蹤號碼與服務類型:
use NextPDF\Pro\Barcode\IntelligentMail\IntelligentMailGenerator;
use NextPDF\Pro\Barcode\IntelligentMail\IntelligentMailOptions;
$generator = new IntelligentMailGenerator(
IntelligentMailOptions::create(
barcodeId: '00', // 2 位數條碼識別碼
serviceTypeId: '234', // 服務類型(3 位數)
mailerId: '567094', // 郵件寄送人 ID(6 或 9 位)
sequenceNumber: '987654321', // 序號(9 或 6 位)
deliveryPointZipCode: '902100001', // 投遞郵遞區號
)
);
// IMb 的高度與模組尺寸有固定規範
$generator->setBarHeight(3.175) // mm(USPS 規範:3.175mm)
->setModuleWidth(0.508) // mm(USPS 規範:0.508mm)
->render($doc, x: 20.0, y: 250.0);
JabCode¶
JabCode(Just Another Bar Code,ISO 23634:2022)使用多色編碼,容量是同尺寸 QR Code 的數倍。適合在文件上嵌入大量資料(如完整 JSON 或加密憑證):
use NextPDF\Pro\Barcode\JabCode\JabCodeGenerator;
use NextPDF\Pro\Barcode\JabCode\JabCodeOptions;
use NextPDF\Pro\Barcode\JabCode\JabCodeColorMode;
// 嵌入完整 JSON 資料(最大 33,000+ 字元)
$payload = json_encode([
'document_id' => 'DOC-2026-001234',
'version' => 3,
'hash' => hash('sha256', $documentContent),
'issuer' => 'NextPDF Demo Authority',
'issued_at' => '2026-03-04T10:00:00+08:00',
], JSON_UNESCAPED_UNICODE);
$generator = new JabCodeGenerator(
JabCodeOptions::create(
data: $payload,
colorMode: JabCodeColorMode::Color8, // 2/4/8 種顏色(顏色越多容量越大)
moduleSize: 2.0, // 每個模組的點陣大小(pt)
errorCorrectionLevel: 3, // 1–10(越高越耐損)
masterSymbol: true, // 是否需要 Master Symbol(多符號集)
)
);
$generator->render($doc, x: 20.0, y: 160.0);
Micro QR Code¶
Micro QR Code 是 QR Code 的緊湊版本,僅使用一個定位圖案(標準 QR Code 有三個),適合空間極為有限的應用:
use NextPDF\Pro\Barcode\MicroQr\MicroQrCodeGenerator;
use NextPDF\Pro\Barcode\MicroQr\MicroQrVersion;
use NextPDF\Pro\Barcode\MicroQr\MicroQrErrorCorrection;
$generator = new MicroQrCodeGenerator();
$generator
->setData('SN:A001234') // 最大 35 個英數字元(M4-M 版本)
->setVersion(MicroQrVersion::M4) // M1/M2/M3/M4(影響容量)
->setErrorCorrection(MicroQrErrorCorrection::M)
->setModuleSize(1.0) // pt(非常小的尺寸)
->render($doc, x: 20.0, y: 200.0);
Micro QR 版本容量對照¶
| 版本 | 最大數字 | 最大英數 | 最大字元 | 最大位元組 |
|---|---|---|---|---|
| M1 | 5 | — | — | — |
| M2 | 10 | 6 | — | — |
| M3 | 23 | 14 | 9 | — |
| M4 | 35 | 21 | 15 | 9 |
批次條碼生成¶
use NextPDF\Pro\Barcode\BatchBarcodeGenerator;
use NextPDF\Pro\Barcode\BarcodeType;
$batchGenerator = new BatchBarcodeGenerator();
$items = [
['sku' => 'PROD-001', 'serial' => 'SN-001', 'data' => 'PROD-001:SN-001:2026-03'],
['sku' => 'PROD-001', 'serial' => 'SN-002', 'data' => 'PROD-001:SN-002:2026-03'],
// ...
];
$doc = ProDocument::createStandalone();
$doc->addPage();
$x = 20.0;
$y = 20.0;
foreach ($items as $item) {
$batchGenerator->render(
document: $doc,
type: BarcodeType::DataMatrix,
data: $item['data'],
x: $x,
y: $y,
size: 15.0,
);
$doc->text($item['serial'], x: $x, y: $y + 18.0, fontSize: 6.0);
$x += 25.0;
if ($x > 550.0) {
$x = 20.0;
$y += 35.0;
}
}
$doc->save('/output/barcode-sheet.pdf');
條碼驗證¶
生成後可選擇性驗證條碼資料符合標準規範:
use NextPDF\Pro\Barcode\Validator\BarcodeValidator;
use NextPDF\Pro\Barcode\BarcodeType;
$validator = new BarcodeValidator();
$result = $validator->validate(
type: BarcodeType::Gs1DataMatrix,
data: $gs1Data,
);
if (!$result->isValid()) {
foreach ($result->getErrors() as $error) {
throw new \InvalidArgumentException(
sprintf('Barcode validation failed: %s', $error->getMessage())
);
}
}
相關資源¶
- Core 條碼文件 — QR Code、Code 128、EAN 使用指南
- 案例研究:政府表單數位化 — 條碼應用於表單處理
- BarcodeGenerator API 參考
- GS1 Application Identifiers
- ISO/IEC 24723:2010 DotCode
- GB/T 33993-2017 HanXin Code