跳轉到

二維條碼

NextPDF 內建 9 種二維條碼編碼器,與一維條碼相同,全部使用 PDF 原生向量圖形渲染(矩形填充路徑),確保任意縮放下的印刷品質。

基本用法

use NextPDF\Barcode\BarcodeType;
use NextPDF\ValueObjects\Rectangle;

// QR Code
$document->barcode()->render(
    type: BarcodeType::QrCode,
    data: 'https://nextpdf.dev',
    position: Rectangle::fromXY(x: 20.0, y: 50.0, width: 30.0, height: 30.0),
);

支援的二維條碼格式

格式 BarcodeType 常數 最大資料量 應用場景
QR Code QrCode ~7,000 字元 行動支付、URL 分享
Micro QR Code MicroQr ~35 字元 小型空間
DataMatrix DataMatrix ~3,116 字元 電子元件、醫療器械
DataMatrix GS1 DataMatrixGs1 GS1 格式 醫藥供應鏈(GS1)
PDF417 Pdf417 ~1,800 字元 身分證、駕照
Micro PDF417 MicroPdf417 ~250 字元 小型 PDF417
Aztec Code Aztec ~3,000 字元 交通票券
MaxiCode Maxicode ~93 字元 UPS 快遞
DotCode DotCode ~450 字元 高速噴墨印刷

QR Code 詳細設定

use NextPDF\Barcode\QrOptions;
use NextPDF\Barcode\QrErrorCorrection;
use NextPDF\Barcode\QrEncoding;

$document->barcode()->render(
    type: BarcodeType::QrCode,
    data: 'https://nextpdf.dev/docs',
    position: Rectangle::fromXY(x: 20.0, y: 50.0, width: 40.0, height: 40.0),
    options: QrOptions::create(
        errorCorrection: QrErrorCorrection::H,  // L | M | Q | H (7%/15%/25%/30%)
        encoding: QrEncoding::Auto,             // Numeric | Alphanumeric | Binary | Kanji | Auto
        version: null,                          // null=自動選擇最小版本
        quietZone: 4,                           // 靜區模組數(QR 規範:至少 4)
        maskPattern: null,                      // null=自動選擇最佳遮罩模式
    ),
);

DataMatrix 設定

use NextPDF\Barcode\DataMatrixOptions;
use NextPDF\Barcode\DataMatrixShape;

$document->barcode()->render(
    type: BarcodeType::DataMatrix,
    data: 'LOT:A2026-001;QTY:500;EXP:20270101',
    position: Rectangle::fromXY(x: 20.0, y: 50.0, width: 20.0, height: 20.0),
    options: DataMatrixOptions::create(
        shape: DataMatrixShape::Square,  // Square | Rectangle
        version: 'auto',                // 自動選擇尺寸
    ),
);

PDF417 設定

use NextPDF\Barcode\Pdf417Options;
use NextPDF\Barcode\Pdf417ErrorCorrection;

$document->barcode()->render(
    type: BarcodeType::Pdf417,
    data: json_encode($idCardData),
    position: Rectangle::fromXY(x: 20.0, y: 50.0, width: 80.0, height: 30.0),
    options: Pdf417Options::create(
        errorCorrectionLevel: Pdf417ErrorCorrection::Level3,  // Level 0-8
        columns: 5,     // 資料欄數(1-30)
        rows: null,     // null=自動計算
        aspectRatio: 3, // 寬高比(建議 2-5)
    ),
);

彩色 QR Code

use NextPDF\Barcode\BarcodeOptions;

$document->barcode()->render(
    type: BarcodeType::QrCode,
    data: 'https://nextpdf.dev',
    position: Rectangle::fromXY(x: 20.0, y: 50.0, width: 35.0, height: 35.0),
    options: BarcodeOptions::create(
        foregroundColor: Color::rgb(30, 58, 138),   // 深藍
        backgroundColor: Color::rgb(255, 255, 255), // 白色
    ),
);

參見