バーコード
バーコードモジュール(BarcodeGenerator、BarcodeRenderer)は、1Dおよび2Dバーコードを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バーコード
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 | インターリーブド 2 of 5 |
S25 | S25 | スタンダード 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 |
2Dバーコード
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コード — 低誤り訂正(約7%) |
QRCODE,M | QR_CODE | QRコード — 中誤り訂正(約15%) |
QRCODE,Q | QR_CODE | QRコード — 四分位誤り訂正(約25%) |
QRCODE,H | QR_CODE | QRコード — 高誤り訂正(約30%) |
DATAMATRIX | DATAMATRIX | DataMatrix |
PDF417 | PDF417 | PDF417 |
スタイルオプション
$style 配列は、1Dおよび2Dメソッドの両方でバーコードの外観を制御します:
| キー | 型 | 説明 |
|---|---|---|
border | bool | バーコードの周囲に枠線を描画 |
padding | float|array | 枠線内側のパディング |
fgcolor | array | 前景(バー)色を [r, g, b] で指定 |
bgcolor | array | 背景色を [r, g, b] で指定 |
text | bool | 1Dバーコードの下に人間が読めるテキストを表示 |
stretch | bool | バーコードを指定幅いっぱいに引き伸ばす |
製品ラベルの例
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)
->write1DBarcode('4006381333931', 'EAN13', 10, 35, 60, 25, 0.4, [
'text' => true,
'fgcolor' => [0, 0, 0],
])
->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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ロゴオーバーレイによってコードが部分的に隠れる可能性がある場合は、QRコードの誤り訂正レベル H(高)を使用してください。