產品標籤範例¶
需求套件:nextpdf/core 難度:中級
完整程式碼¶
<?php
declare(strict_types=1);
use NextPDF\Core\Document;
use NextPDF\Core\Barcode\Encoders\Ean13Encoder;
use NextPDF\Core\Barcode\Encoders\QrCodeEncoder;
use NextPDF\Core\Barcode\ValueObjects\BarcodeOptions;
use NextPDF\Core\ValueObjects\PageSize;
use NextPDF\Core\ValueObjects\Margin;
use NextPDF\Core\ValueObjects\Position;
use NextPDF\Core\ValueObjects\Dimension;
use NextPDF\Core\ValueObjects\Color;
// ─── 產品資料 ─────────────────────────────────────────────────
$products = [
[
'name' => 'NextPDF Pro 授權卡',
'sku' => 'NXT-PRO-001',
'ean13' => '4901234567890', // 12 位數(第 13 位自動計算)
'price' => 'NT$ 9,900',
'url' => 'https://nextpdf.dev/activate/NXT-PRO-001',
'batch' => 'B2024031501',
],
[
'name' => 'NextPDF Enterprise 授權卡',
'sku' => 'NXT-ENT-001',
'ean13' => '4901234567906',
'price' => 'NT$ 49,900',
'url' => 'https://nextpdf.dev/activate/NXT-ENT-001',
'batch' => 'B2024031502',
],
];
// ─── 標籤尺寸設定(每頁 4x2 = 8 個標籤)────────────────────
const LABEL_WIDTH = 90.0; // mm
const LABEL_HEIGHT = 55.0; // mm
const COLS = 2;
const ROWS = 4;
const PAGE_MARGIN = 10.0;
const GAP = 5.0;
$document = Document::createStandalone(
pageSize: PageSize::A4,
margin: Margin::uniform(PAGE_MARGIN),
);
$text = $document->text();
$drawing = $document->drawing();
$barcodes = $document->barcodes();
$currentPage = $document->pages()->add();
$col = 0;
$row = 0;
foreach ($products as $product) {
// 計算標籤左上角座標
$x = PAGE_MARGIN + $col * (LABEL_WIDTH + GAP);
$y = PAGE_MARGIN + $row * (LABEL_HEIGHT + GAP);
// 檢查是否需要換頁
if ($y + LABEL_HEIGHT > 297.0 - PAGE_MARGIN) {
$currentPage = $document->pages()->add();
$col = 0;
$row = 0;
$x = PAGE_MARGIN;
$y = PAGE_MARGIN;
}
renderLabel(
document: $document,
product: $product,
x: $x,
y: $y,
);
$col++;
if ($col >= COLS) {
$col = 0;
$row++;
}
}
// ─── 標籤渲染函式 ─────────────────────────────────────────────
function renderLabel(Document $document, array $product, float $x, float $y): void
{
$text = $document->text();
$drawing = $document->drawing();
$barcodes = $document->barcodes();
// 標籤外框
$drawing->rectangle(
x: $x,
y: $y,
width: LABEL_WIDTH,
height: LABEL_HEIGHT,
stroke: Color::fromHex('#D1D5DB'),
strokeWidth: 0.3,
cornerRadius: 1.5,
);
// 頂部色塊
$drawing->rectangle(
x: $x,
y: $y,
width: LABEL_WIDTH,
height: 10.0,
fill: Color::fromHex('#1E3A8A'),
cornerRadius: 1.5,
);
// 產品名稱(頂部白字)
$text->write(
text: $product['name'],
position: Position::at(x: $x + 3.0, y: $y + 2.5),
fontSize: 7.0,
fontName: 'NotoSansCJKtc-Bold',
color: Color::fromHex('#FFFFFF'),
maxWidth: LABEL_WIDTH - 6.0,
);
// SKU
$text->write(
text: $product['sku'],
position: Position::at(x: $x + 3.0, y: $y + 12.0),
fontSize: 6.5,
fontName: 'Courier',
color: Color::fromHex('#6B7280'),
);
// 價格
$text->write(
text: $product['price'],
position: Position::at(x: $x + LABEL_WIDTH - 3.0, y: $y + 12.0),
fontSize: 8.0,
fontName: 'NotoSansCJKtc-Bold',
color: Color::fromHex('#1E3A8A'),
alignment: 'right',
maxWidth: 35.0,
);
// EAN-13 條碼
$barcodes->render(
encoder: Ean13Encoder::create(data: substr($product['ean13'], 0, 12)),
position: Position::at(x: $x + 3.0, y: $y + 18.0),
dimension: Dimension::create(width: 55.0, height: 22.0),
options: BarcodeOptions::create()
->withHumanReadable(true)
->withFontSize(5.5),
);
// QR Code(啟動 URL)
$barcodes->render(
encoder: QrCodeEncoder::create(
data: $product['url'],
errorLevel: 'M',
quietZone: 2,
),
position: Position::at(x: $x + 62.0, y: $y + 17.0),
dimension: Dimension::square(size: 25.0),
);
// QR 提示文字
$text->write(
text: '掃碼啟動',
position: Position::at(x: $x + 74.5, y: $y + 43.5),
fontSize: 5.5,
fontName: 'NotoSansCJKtc',
color: Color::fromHex('#6B7280'),
alignment: 'center',
maxWidth: 25.0,
);
// 批次號
$text->write(
text: "批次:{$product['batch']}",
position: Position::at(x: $x + 3.0, y: $y + 48.0),
fontSize: 5.5,
fontName: 'Courier',
color: Color::fromHex('#9CA3AF'),
);
}
// ─── 輸出 ────────────────────────────────────────────────────
$document->save('/output/product-labels.pdf');
程式碼說明¶
標籤尺寸參考¶
延伸閱讀¶
- 條碼指南 — 所有條碼格式完整說明
- API 參考:Barcode — 條碼 API 完整文件