Skip to content

Typography (HasTypography)

Trait HasTypography kiểm soát giao diện của văn bản: họ font, style, kích thước, khoảng cách, co giãn, màu sắc, bóng đổ và chế độ render. Mọi method trả về static để fluent chaining.

Đặt Font

setFont()

Đặt họ font, style và kích thước hiện tại trong một lệnh gọi.

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->addPage()
    ->setFont('Helvetica', '', 12)
    ->cell(0, 10, 'Normal text', newLine: true)
    ->setFont('Helvetica', 'B', 14)
    ->cell(0, 10, 'Bold text', newLine: true)
    ->setFont('Helvetica', 'BI', 16)
    ->cell(0, 10, 'Bold Italic text', newLine: true);

Font Style

Kết hợp ký tự style để áp dụng nhiều hiệu ứng:

Style
''Regular
'B'Bold
'I'Italic
'BI'Bold Italic
'U'Underline
'D'Strikethrough
'O'Overline

Style có thể kết hợp tự do: 'BIU' tạo văn bản bold, italic, gạch chân.

php
$pdf->setFont('Helvetica', 'BU', 12)
    ->cell(0, 10, 'Bold + Underline', newLine: true)
    ->setFont('Helvetica', 'ID', 12)
    ->cell(0, 10, 'Italic + Strikethrough', newLine: true)
    ->setFont('Helvetica', 'BO', 12)
    ->cell(0, 10, 'Bold + Overline', newLine: true);

setFontSize()

Đổi kích thước font mà không đổi họ font hoặc style.

php
$pdf->setFont('Helvetica', 'B', 12)
    ->cell(0, 10, '12pt heading', newLine: true)
    ->setFontSize(10)
    ->cell(0, 10, '10pt body text', newLine: true)
    ->setFontSize(8)
    ->cell(0, 10, '8pt footnote', newLine: true);

Khoảng cách và co giãn

setFontSpacing()

Điều chỉnh khoảng cách thêm (tính bằng point) chèn giữa mỗi cặp ký tự.

php
$pdf->setFont('Helvetica', '', 12)
    ->setFontSpacing(0)
    ->cell(0, 10, 'Normal spacing', newLine: true)
    ->setFontSpacing(1.5)
    ->cell(0, 10, 'Expanded spacing', newLine: true)
    ->setFontSpacing(-0.5)
    ->cell(0, 10, 'Tight spacing', newLine: true)
    ->setFontSpacing(0);

setFontStretching()

Áp dụng scale ngang cho glyph. Giá trị 100 là chiều rộng bình thường.

php
$pdf->setFont('Helvetica', '', 14)
    ->setFontStretching(100)
    ->cell(0, 10, 'Normal width (100%)', newLine: true)
    ->setFontStretching(130)
    ->cell(0, 10, 'Stretched (130%)', newLine: true)
    ->setFontStretching(75)
    ->cell(0, 10, 'Condensed (75%)', newLine: true)
    ->setFontStretching(100);

Đo văn bản

getStringWidth()

Trả về chiều rộng (tính bằng đơn vị người dùng) của chuỗi render trong font và kích thước hiện tại. Hữu ích cho tính toán layout trước khi vẽ.

php
$pdf->setFont('Helvetica', '', 12);
$width = $pdf->getStringWidth('Invoice Total: $1,250.00');

// Dùng chiều rộng đo được để căn phải cell
$pageWidth = $pdf->getPageWidth();
$rightMargin = $pdf->getRightMargin();
$pdf->setX($pageWidth - $rightMargin - $width)
    ->cell($width, 10, 'Invoice Total: $1,250.00');

Chế độ render văn bản

PDF định nghĩa 8 chế độ render văn bản qua enum TextRenderer. Chúng kiểm soát văn bản được fill, stroke, dùng làm clipping path hoặc ẩn.

Chế độGiá trịHiệu ứng
Fill0Văn bản fill bình thường (mặc định)
Stroke1Chỉ outline
FillStroke2Fill với outline
Invisible3Ẩn nhưng có thể chọn/tìm kiếm
FillClip4Fill, rồi thêm vào clipping path
StrokeClip5Stroke, rồi thêm vào clipping path
FillStrokeClip6Fill + stroke, rồi clip
Clip7Chỉ thêm vào clipping path
php
$pdf->setFont('Helvetica', 'B', 36)
    ->setTextRenderingMode(0)
    ->cell(0, 15, 'Filled text', newLine: true)
    ->setTextRenderingMode(1)
    ->cell(0, 15, 'Outlined text', newLine: true)
    ->setTextRenderingMode(2)
    ->cell(0, 15, 'Fill + Stroke', newLine: true)
    ->setTextRenderingMode(3)
    ->cell(0, 15, 'Invisible (but searchable)', newLine: true)
    ->setTextRenderingMode(0);

Chế độ Invisible (3) đặc biệt hữu ích cho OCR overlay — bạn đặt văn bản nhận diện lên trên hình ảnh quét để PDF tìm kiếm được trong khi hình ảnh vẫn hiển thị.

Màu văn bản

setTextColor()

Đặt màu văn bản dùng giá trị RGB.

php
$pdf->setTextColor(0, 0, 0)          // Đen
    ->cell(0, 10, 'Black text', newLine: true)
    ->setTextColor(220, 50, 50)       // Đỏ
    ->cell(0, 10, 'Red text', newLine: true)
    ->setTextColor(0, 102, 204)       // Xanh dương
    ->cell(0, 10, 'Blue text', newLine: true)
    ->setTextColor(0, 0, 0);          // Reset về đen

Khi gọi với một tham số, đặt giá trị grayscale (0 = đen, 255 = trắng).

php
$pdf->setTextColor(128)
    ->cell(0, 10, 'Gray text', newLine: true);

Bóng đổ văn bản

setTextShadow()

Áp dụng hiệu ứng bóng đổ dưới văn bản. Bóng được định nghĩa bằng mảng kết hợp.

php
$pdf->setFont('Helvetica', 'B', 28)
    ->setTextShadow([
        'enabled'    => true,
        'depth_w'    => 0.4,    // Offset ngang (mm)
        'depth_h'    => 0.4,    // Offset dọc (mm)
        'color'      => [180, 180, 180],
        'opacity'    => 0.5,
        'blend_mode' => 'Normal',
    ])
    ->cell(0, 15, 'Heading with Shadow', newLine: true)
    ->setTextShadow(['enabled' => false]);

Ví dụ đầy đủ

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->addPage()

    // Tiêu đề — lớn, bold, xanh dương
    ->setFont('Helvetica', 'B', 24)
    ->setTextColor(0, 51, 102)
    ->cell(0, 14, 'Monthly Report', newLine: true)

    // Phụ đề — regular, xám, khoảng cách thêm
    ->setFont('Helvetica', '', 12)
    ->setTextColor(100, 100, 100)
    ->setFontSpacing(1.0)
    ->cell(0, 10, 'February 2026', newLine: true)
    ->setFontSpacing(0)
    ->ln(5)

    // Nội dung — đen, bình thường
    ->setFont('Times', '', 11)
    ->setTextColor(0, 0, 0)
    ->multiCell(0, 6, 'Revenue increased 12% compared to the previous quarter. Operating costs remained stable, and net margin improved by 3 percentage points.')
    ->ln(3)

    // Chú thích — nhỏ, italic, xám
    ->setFont('Times', 'I', 8)
    ->setTextColor(150, 150, 150)
    ->cell(0, 5, '* All figures are unaudited.')

    ->save('report.pdf');

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