Skip to content

Barcode

Modul Barcode (BarcodeGenerator, BarcodeRenderer) me-render barcode 1D dan 2D langsung ke dalam PDF. Tipe barcode didefinisikan oleh enum BarcodeType dan Barcode2DType. Semua method mengembalikan static, sehingga setiap panggilan bisa di-chain.

Contoh Dasar

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->addPage()
    ->setFont('Helvetica', '', 12)
    ->cell(0, 10, 'Barcode Produk', 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);

Barcode 1D

php
$pdf->write1DBarcode(
    string $code,           // Data untuk di-encode
    string $type,           // String tipe barcode
    float  $x,              // Posisi X
    float  $y,              // Posisi Y
    float  $w,              // Lebar
    float  $h,              // Tinggi
    float  $xres = 0.4,    // Lebar bar paling tipis dalam unit pengguna
    array  $style = [],     // Opsi style
);

Tipe 1D yang Didukung (BarcodeType)

String TipeNilai EnumDeskripsi
C39CODE_39Code 39
C93CODE_93Code 93
C128CODE_128Code 128 (mode otomatis)
C128ACODE_128ACode 128 Subset A
C128BCODE_128BCode 128 Subset B
C128CCODE_128CCode 128 Subset C
EAN8EAN_8EAN-8
EAN13EAN_13EAN-13
UPCAUPC_AUPC-A
UPCEUPC_EUPC-E
I25I25Interleaved 2 of 5
S25S25Standard 2 of 5
CODABARCODABARCodabar
CODE11CODE_11Code 11
MSIMSIMSI Plessey
POSTNETPOSTNETPOSTNET (pos AS)
PLANETPLANETPLANET (pos AS)
IMBIMBIntelligent Mail Barcode
PHARMAPHARMACODEPharmacode

Barcode 2D

php
$pdf->write2DBarcode(
    string $code,           // Data untuk di-encode
    string $type,           // String tipe barcode
    float  $x,              // Posisi X
    float  $y,              // Posisi Y
    float  $w,              // Lebar
    float  $h,              // Tinggi
    array  $style = [],     // Opsi style
);

Tipe 2D yang Didukung (Barcode2DType)

String TipeNilai EnumDeskripsi
QRCODE,LQR_CODEQR Code — Koreksi error rendah (~7%)
QRCODE,MQR_CODEQR Code — Sedang (~15%)
QRCODE,QQR_CODEQR Code — Kuartil (~25%)
QRCODE,HQR_CODEQR Code — Tinggi (~30%)
DATAMATRIXDATAMATRIXDataMatrix
PDF417PDF417PDF417

Opsi Style

Array $style mengontrol tampilan barcode untuk method 1D dan 2D:

KeyTipeDeskripsi
borderboolGambar border di sekitar barcode
paddingfloat|arrayPadding di dalam border
fgcolorarrayWarna foreground (bar) sebagai [r, g, b]
bgcolorarrayWarna background sebagai [r, g, b]
textboolTampilkan teks human-readable di bawah barcode 1D
stretchboolRegangkan barcode untuk mengisi lebar yang diberikan

Contoh Label Produk

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],
    ]);

Gunakan level koreksi error QR Code H (tinggi) saat kode mungkin terhalang sebagian oleh overlay logo.

Didistribusikan di bawah lisensi LGPL-3.0-or-later.