Font
TCPDF-Next supporta font TrueType, OpenType e Type1 con subsetting automatico, rendering CJK e testo bidirezionale. I font vengono caricati tramite il metodo addFont() su Document o gestiti direttamente tramite il FontManager.
Tipi Font Supportati
| Formato | Estensioni | Note |
|---|---|---|
| TrueType | .ttf | Supporto outline glifo completo, formato più comune |
| OpenType | .otf | Varianti outline CFF e TrueType entrambe supportate |
| Type1 | .pfb + .afm | Font PostScript legacy, file metrica richiesto |
Font Core
La specifica PDF definisce 14 font standard (i "Base 14") che ogni visualizzatore conforme deve supportare. Questi non richiedono incorporamento, il che mantiene la dimensione file minimale.
| Famiglia | Stili |
|---|---|
| Helvetica | Regular, Bold, Italic, Bold Italic |
| Times | Regular, Bold, Italic, Bold Italic |
| Courier | Regular, Bold, Italic, Bold Italic |
| Symbol | Regular |
| ZapfDingbats | Regular |
use Yeeefang\TcpdfNext\Core\Document;
$pdf = Document::create()
->addPage()
->setFont('Helvetica', '', 12)
->cell(0, 10, 'Helvetica — the PDF workhorse', newLine: true)
->setFont('Times', 'I', 12)
->cell(0, 10, 'Times Italic — classic serif', newLine: true)
->setFont('Courier', 'B', 12)
->cell(0, 10, 'Courier Bold — monospaced', newLine: true);Caricamento Font Personalizzato
addFont()
Registra un file font TrueType o OpenType e usalo immediatamente.
$pdf = Document::create()
->addFont('NotoSansTC', '', '/path/to/NotoSansTC-Regular.ttf')
->addPage()
->setFont('NotoSansTC', '', 12)
->cell(0, 10, '繁體中文文字', newLine: true);Famiglie Font con Stili Multipli
Registra ogni variante stile sotto lo stesso nome famiglia. TCPDF-Next seleziona il file corretto quando chiami setFont() con una stringa stile.
$pdf = Document::create()
->addFont('Roboto', '', '/fonts/Roboto-Regular.ttf')
->addFont('Roboto', 'B', '/fonts/Roboto-Bold.ttf')
->addFont('Roboto', 'I', '/fonts/Roboto-Italic.ttf')
->addFont('Roboto', 'BI', '/fonts/Roboto-BoldItalic.ttf')
->addPage()
->setFont('Roboto', '', 11)
->cell(0, 10, 'Regular weight', newLine: true)
->setFont('Roboto', 'B', 11)
->cell(0, 10, 'Bold weight', newLine: true)
->setFont('Roboto', 'I', 11)
->cell(0, 10, 'Italic style', newLine: true)
->setFont('Roboto', 'BI', 11)
->cell(0, 10, 'Bold Italic', newLine: true);Subsetting Font
Per impostazione predefinita, TCPDF-Next incorpora solo i glifi che appaiono nel documento. Il FontSubsetter gestisce questo automaticamente durante la serializzazione PDF.
// Il subsetting font è automatico — solo i glifi usati vengono incorporati
$pdf = Document::create()
->addPage()
->setFont('DejaVuSans', '', 12)
->cell(0, 10, 'Only these glyphs are embedded');
// Risultato: dimensione file PDF più piccolaPerché il Subsetting è Importante
Un font CJK completo può superare 15 MB. Se il tuo documento usa solo una manciata di caratteri, il subsetting riduce i dati font incorporati a pochi kilobyte. Questo è critico per consegna web e allegati email.
Disabilitare il Subsetting
In rari casi (es. campi form editabili dove l'utente può digitare qualsiasi carattere), potresti aver bisogno del font completo. Passa false al parametro subsetting quando registri:
$pdf->addFont('NotoSans', '', '/fonts/NotoSans-Regular.ttf', subset: false);