Skip to content

Font

TCPDF-Next hỗ trợ font TrueType, OpenType và Type1 với subsetting tự động, render CJK và văn bản hai chiều. Font được tải qua method addFont() trên Document hoặc quản lý trực tiếp qua FontManager.

Loại font hỗ trợ

Định dạngExtensionGhi chú
TrueType.ttfHỗ trợ đầy đủ glyph outline, định dạng phổ biến nhất
OpenType.otfCả biến thể CFF và TrueType outline đều được hỗ trợ
Type1.pfb + .afmFont PostScript cũ, cần file metric

Font Core

Đặc tả PDF định nghĩa 14 font chuẩn ("Base 14") mà mọi trình xem tương thích phải hỗ trợ. Chúng không cần nhúng, giữ kích thước file tối thiểu.

HọStyle
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);

Tải font tùy chỉnh

addFont()

Đăng ký file font TrueType hoặc OpenType và dùng ngay.

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

Họ font với nhiều style

Đăng ký mỗi biến thể style dưới cùng tên họ. TCPDF-Next chọn file đúng khi bạn gọi setFont() với chuỗi style.

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

Mặc định, TCPDF-Next chỉ nhúng glyph xuất hiện trong tài liệu. FontSubsetter xử lý tự động khi serialize PDF.

php
// Font subsetting tự động — chỉ glyph dùng được nhúng
$pdf = Document::create()
    ->addPage()
    ->setFont('DejaVuSans', '', 12)
    ->cell(0, 10, 'Only these glyphs are embedded');
// Kết quả: kích thước file PDF nhỏ hơn

Tại sao subsetting quan trọng

Font CJK đầy đủ có thể vượt 15 MB. Nếu tài liệu chỉ dùng vài ký tự, subsetting giảm dữ liệu font nhúng xuống vài kilobyte. Điều này rất quan trọng cho phân phối web và đính kèm email.

Tắt subsetting

Trong trường hợp hiếm (ví dụ: trường form có thể chỉnh sửa nơi người dùng có thể gõ bất kỳ ký tự nào), bạn có thể cần font đầy đủ. Truyền false cho tham số subsetting khi đăng ký:

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

Hỗ trợ CJK

Văn bản Trung, Nhật và Hàn cần font với tập glyph lớn. TCPDF-Next xác thực nội dung CJK qua CjkValidator và đảm bảo encoding chính xác trong 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);

Font CJK luôn được subset trừ khi tắt rõ ràng — tiết kiệm kích thước đáng kể.

Văn bản hai chiều (BiDi)

BiDiResolver implement Unicode Bidirectional Algorithm để render đúng script phải-sang-trái như Arabic và Hebrew, bao gồm nội dung trộn LTR/RTL.

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

Sắp xếp lại BiDi được áp dụng tự động khi văn bản chứa ký tự RTL. Không cần cấu hình thủ công.

ToUnicode CMap

TCPDF-Next tạo ToUnicode CMap cho mỗi font nhúng. Ánh xạ này cho phép trình xem PDF trích xuất văn bản Unicode đúng khi người dùng copy/paste hoặc khi công cụ tìm kiếm index tài liệu. Nó được tạo tự động khi serialize.

Font Metric

Class FontInfoFontMetrics cung cấp phép đo typography chi tiết.

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

// Đo chuỗi cụ thể trong font hiện tại
$width = $pdf->getStringWidth('Hello, World!');

// Dùng metric cho layout chính xác
$cellWidth = $pdf->getStringWidth('Total:') + 4; // thêm padding 4mm
$pdf->cell($cellWidth, 8, 'Total:');

Metric bao gồm ascender, descender, line gap, cap height, x-height và advance width mỗi glyph — tất cả lấy từ bảng nội bộ font.

Ví dụ đầy đủ

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    // Đăng ký font
    ->addFont('Roboto', '', '/fonts/Roboto-Regular.ttf')
    ->addFont('Roboto', 'B', '/fonts/Roboto-Bold.ttf')
    ->addFont('NotoSansTC', '', '/fonts/NotoSansTC-Regular.ttf')

    ->addPage()

    // Tiêu đề tiếng Anh
    ->setFont('Roboto', 'B', 18)
    ->cell(0, 12, 'Multilingual Invoice', newLine: true)
    ->ln(3)

    // Nội dung tiếng Anh
    ->setFont('Roboto', '', 11)
    ->cell(0, 8, 'Customer: Acme Corporation', newLine: true)
    ->cell(0, 8, 'Date: 2026-02-16', newLine: true)
    ->ln(5)

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

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

Phân phối theo giấy phép LGPL-3.0-or-later.