一維條碼¶
NextPDF 內建 23 種一維條碼編碼器,全部以 PDF 原生向量圖形(路徑操作符)渲染——不依賴圖片,縮放不失真,印刷品質無損。
基本用法¶
use NextPDF\Barcode\BarcodeRenderer;
use NextPDF\Barcode\BarcodeType;
use NextPDF\ValueObjects\Rectangle;
$document->barcode()->render(
type: BarcodeType::Code128,
data: 'NEXTPDF-2026-001',
position: Rectangle::fromXY(x: 20.0, y: 50.0, width: 80.0, height: 20.0),
);
支援的一維條碼格式¶
| 格式 | BarcodeType 常數 | 字符集 | 應用場景 |
|---|---|---|---|
| Code 128 A | Code128A | ASCII 00–95 | 工業、物流 |
| Code 128 B | Code128B | ASCII 32–127 | 一般用途 |
| Code 128 C | Code128C | 數字對 00–99 | 數字高密度 |
| Code 128 Auto | Code128 | 自動子集選擇 | 推薦預設 |
| EAN-13 | Ean13 | 13 位數字 | 零售商品 |
| EAN-8 | Ean8 | 8 位數字 | 小型零售品 |
| UPC-A | UpcA | 12 位數字 | 北美零售 |
| UPC-E | UpcE | 壓縮 UPC | 小包裝 |
| ITF-14 | Itf14 | 14 位數字 | 物流外箱 |
| ITF-6 | Itf6 | 6 位數字 | 物流補充碼 |
| Interleaved 2 of 5 | I25 | 偶數位數字 | 倉儲 |
| Standard 2 of 5 | S25 | 數字 | 航空行李 |
| GS1-128 | Gs1128 | GS1 Application Identifier | 供應鏈 |
| GS1-128 CC-A | Gs1128CcA | GS1 複合碼 | 醫療供應鏈 |
| Code 39 | Code39 | 大寫英數 + 特殊字符 | 汽車、國防 |
| Code 39 Extended | Code39Ext | 完整 ASCII | 醫療 |
| Code 93 | Code93 | 大寫英數 + 特殊字符 | 加拿大郵政 |
| Codabar | Codabar | 數字 + 特殊字符 | 血庫、圖書館 |
| MSI / Plessey | Msi | 數字 | 倉儲 |
| Pharmacode | Pharmacode | 數字(3–131070) | 製藥包裝 |
| PostNet | Postnet | 5/9/11 位數字 | 美國郵政 |
| Planet | Planet | 12 位數字 | 美國郵政追蹤 |
| Royal Mail 4-State | Rm4scc | 英數 | 英國郵政 |
Code 128 自動子集選擇¶
// Code128 自動在 A/B/C 子集間切換,最大化編碼密度
$document->barcode()->render(
type: BarcodeType::Code128, // 自動模式
data: '0102030405060708', // 純數字 → 自動使用 Code 128 C(最高密度)
position: Rectangle::fromXY(x: 20.0, y: 50.0, width: 60.0, height: 15.0),
);
EAN 與 UPC 系列¶
// EAN-13 含檢查碼計算
$document->barcode()->render(
type: BarcodeType::Ean13,
data: '590123412345', // 12 位,第 13 位自動計算
position: Rectangle::fromXY(x: 20.0, y: 50.0, width: 37.0, height: 26.0),
showText: true, // 顯示人類可讀文字
);
條碼外觀設定¶
use NextPDF\Barcode\BarcodeOptions;
$document->barcode()->render(
type: BarcodeType::Code128,
data: 'HELLO-WORLD',
position: Rectangle::fromXY(x: 20.0, y: 50.0, width: 80.0, height: 20.0),
options: BarcodeOptions::create(
foregroundColor: Color::rgb(0, 0, 0),
backgroundColor: Color::rgb(255, 255, 255),
showText: true,
textFontSize: 8.0,
textPosition: TextPosition::Below, // Above | Below
quietZone: 10, // 靜區模組數
barHeightRatio: 0.7, // 條碼高度佔框高比例
),
);