Codici a Barre
Il modulo Barcode (BarcodeGenerator, BarcodeRenderer) renderizza codici a barre 1D e 2D direttamente nel PDF. I tipi codice a barre sono definiti dagli enum BarcodeType e Barcode2DType. Tutti i metodi restituiscono static, quindi ogni chiamata può essere concatenata.
Esempio Base
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
Codici a Barre 1D
php
$pdf->write1DBarcode(
string $code, // Dati da codificare
string $type, // Stringa tipo codice a barre
float $x, // Posizione X
float $y, // Posizione Y
float $w, // Larghezza
float $h, // Altezza
float $xres = 0.4, // Larghezza barra più sottile in unità utente
array $style = [], // Opzioni stile
);1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
Tipi 1D Supportati (BarcodeType)
| Stringa Tipo | Valore Enum | Descrizione |
|---|---|---|
C39 | CODE_39 | Code 39 |
C93 | CODE_93 | Code 93 |
C128 | CODE_128 | Code 128 (modalità auto) |
C128A | CODE_128A | Code 128 Subset A |
C128B | CODE_128B | Code 128 Subset B |
C128C | CODE_128C | Code 128 Subset C |
EAN8 | EAN_8 | EAN-8 |
EAN13 | EAN_13 | EAN-13 |
UPCA | UPC_A | UPC-A |
UPCE | UPC_E | UPC-E |
I25 | I25 | Interleaved 2 of 5 |
S25 | S25 | Standard 2 of 5 |
CODABAR | CODABAR | Codabar |
CODE11 | CODE_11 | Code 11 |
MSI | MSI | MSI Plessey |
POSTNET | POSTNET | POSTNET (US postal) |
PLANET | PLANET | PLANET (US postal) |
IMB | IMB | Intelligent Mail Barcode |
PHARMA | PHARMACODE | Pharmacode |
Codici a Barre 2D
php
$pdf->write2DBarcode(
string $code, // Dati da codificare
string $type, // Stringa tipo codice a barre
float $x, // Posizione X
float $y, // Posizione Y
float $w, // Larghezza
float $h, // Altezza
array $style = [], // Opzioni stile
);1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
Tipi 2D Supportati (Barcode2DType)
| Stringa Tipo | Valore Enum | Descrizione |
|---|---|---|
QRCODE,L | QR_CODE | QR Code — Correzione errori bassa (~7%) |
QRCODE,M | QR_CODE | QR Code — Media (~15%) |
QRCODE,Q | QR_CODE | QR Code — Quartile (~25%) |
QRCODE,H | QR_CODE | QR Code — Alta (~30%) |
DATAMATRIX | DATAMATRIX | DataMatrix |
PDF417 | PDF417 | PDF417 |
Opzioni Stile
L'array $style controlla l'aspetto codice a barre per entrambi metodi 1D e 2D:
| Chiave | Tipo | Descrizione |
|---|---|---|
border | bool | Disegna bordo attorno al codice a barre |
padding | float|array | Padding dentro il bordo |
fgcolor | array | Colore primo piano (barra) come [r, g, b] |
bgcolor | array | Colore sfondo come [r, g, b] |
text | bool | Mostra testo leggibile umano sotto codici a barre 1D |
stretch | bool | Allunga codice a barre per riempire larghezza data |
Esempio Etichetta Prodotto
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
Usa livello correzione errori QR Code H (alto) quando il codice potrebbe essere parzialmente oscurato da overlay logo.