Skip to content

Fonts

TCPDF-Next supports TrueType, OpenType, and Type1 fonts with automatic subsetting, CJK rendering, and bidirectional text. Fonts are loaded through the addFont() method on Document or managed directly via the FontManager.

Supported Font Types

FormatExtensionsNotes
TrueType.ttfFull glyph outline support, most common format
OpenType.otfCFF and TrueType outline variants both supported
Type1.pfb + .afmLegacy PostScript fonts, metric file required

Core Fonts

The PDF specification defines 14 standard fonts (the "Base 14") that every conforming viewer must support. These do not require embedding, which keeps file size minimal.

FamilyStyles
HelveticaRegular, Bold, Italic, Bold Italic
TimesRegular, Bold, Italic, Bold Italic
CourierRegular, Bold, Italic, Bold Italic
SymbolRegular
ZapfDingbatsRegular
php
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);

Custom Font Loading

addFont()

Register a TrueType or OpenType font file and use it immediately.

php
$pdf = Document::create()
    ->addFont('NotoSansTC', '', '/path/to/NotoSansTC-Regular.ttf')
    ->addPage()
    ->setFont('NotoSansTC', '', 12)
    ->cell(0, 10, '繁體中文文字', newLine: true);

Font Families with Multiple Styles

Register each style variant under the same family name. TCPDF-Next selects the correct file when you call setFont() with a style string.

php
$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);

Font Subsetting

By default, TCPDF-Next embeds only the glyphs that appear in the document. The FontSubsetter handles this automatically during PDF serialization.

php
// Font subsetting is automatic — only used glyphs are embedded
$pdf = Document::create()
    ->addPage()
    ->setFont('DejaVuSans', '', 12)
    ->cell(0, 10, 'Only these glyphs are embedded');
// Result: smaller PDF file size

Why Subsetting Matters

A full CJK font can exceed 15 MB. If your document uses only a handful of characters, subsetting reduces the embedded font data to a few kilobytes. This is critical for web delivery and email attachments.

Disabling Subsetting

In rare cases (e.g., editable form fields where the user may type any character), you may need the full font. Pass false to the subsetting parameter when registering:

php
$pdf->addFont('NotoSans', '', '/fonts/NotoSans-Regular.ttf', subset: false);

CJK Support

Chinese, Japanese, and Korean text requires fonts with large glyph sets. TCPDF-Next validates CJK content through the CjkValidator and ensures correct encoding in the PDF output.

php
$pdf = Document::create()
    ->addFont('NotoSansCJK', '', '/fonts/NotoSansCJKtc-Regular.otf')
    ->addPage()
    ->setFont('NotoSansCJK', '', 12)
    ->cell(0, 10, '中文:你好世界', newLine: true)
    ->cell(0, 10, '日本語:こんにちは世界', newLine: true)
    ->cell(0, 10, '한국어:안녕하세요 세계', newLine: true);

CJK fonts are always subsetted unless explicitly disabled — the size savings are substantial.

Bidirectional Text (BiDi)

The BiDiResolver implements the Unicode Bidirectional Algorithm for correct rendering of right-to-left scripts such as Arabic and Hebrew, including mixed LTR/RTL content.

php
$pdf = Document::create()
    ->addFont('NotoSansArabic', '', '/fonts/NotoSansArabic-Regular.ttf')
    ->addPage()
    ->setFont('NotoSansArabic', '', 14)
    ->cell(0, 10, 'مرحبا بالعالم', newLine: true)   // Arabic
    ->write(10, 'Mixed: Hello مرحبا World');          // LTR + RTL

BiDi reordering is applied automatically when the text contains RTL characters. No manual configuration is required.

ToUnicode CMap

TCPDF-Next generates a ToUnicode CMap for every embedded font. This mapping allows PDF viewers to extract correct Unicode text when users copy/paste or when search engines index the document. It is generated automatically during serialization.

Font Metrics

The FontInfo and FontMetrics classes expose detailed typographic measurements.

php
$pdf->setFont('Helvetica', '', 12);

// Measure a specific string in the current font
$width = $pdf->getStringWidth('Hello, World!');

// Use metrics for precise layout
$cellWidth = $pdf->getStringWidth('Total:') + 4; // add 4mm padding
$pdf->cell($cellWidth, 8, 'Total:');

Metrics include ascender, descender, line gap, cap height, x-height, and per-glyph advance widths — all derived from the font's internal tables.

Complete Example

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    // Register fonts
    ->addFont('Roboto', '', '/fonts/Roboto-Regular.ttf')
    ->addFont('Roboto', 'B', '/fonts/Roboto-Bold.ttf')
    ->addFont('NotoSansTC', '', '/fonts/NotoSansTC-Regular.ttf')

    ->addPage()

    // English heading
    ->setFont('Roboto', 'B', 18)
    ->cell(0, 12, 'Multilingual Invoice', newLine: true)
    ->ln(3)

    // English body
    ->setFont('Roboto', '', 11)
    ->cell(0, 8, 'Customer: Acme Corporation', newLine: true)
    ->cell(0, 8, 'Date: 2026-02-16', newLine: true)
    ->ln(5)

    // Chinese section
    ->setFont('NotoSansTC', '', 11)
    ->cell(0, 8, '客戶名稱:台灣科技有限公司', newLine: true)
    ->cell(0, 8, '發票日期:2026年2月16日', newLine: true)

    ->save('multilingual-invoice.pdf');

Released under the LGPL-3.0-or-later License.