Skip to content

Cấu hình

TCPDF-Next được cung cấp với mặc định hợp lý, sẵn sàng sử dụng ngay. Mọi thiết lập đều có thể ghi đè khi tạo document hoặc sau đó qua fluent setter.

Mặc định Document

Khi bạn gọi Document::create(), các mặc định sau áp dụng nếu bạn không chỉ định khác:

Thiết lậpMặc địnhMô tả
Kích thước trangPageSize::A4ISO A4 (210 × 297 mm)
HướngOrientation::PortraitChế độ dọc
Đơn vịUnit::MillimeterMọi phép đo tính bằng milimet
Margin trái15 mmMargin trang trái
Margin trên27 mmMargin trang trên
Margin phải15 mmMargin trang phải
Margin dưới25 mmMargin trang dưới
Auto page breaktrueTự động ngắt trang gần margin dưới
FontHelvetica, 12 ptHọ font và kích thước mặc định
php
use Yeeefang\TcpdfNext\Core\Document;
use Yeeefang\TcpdfNext\Core\Enums\PageSize;
use Yeeefang\TcpdfNext\Core\Enums\Orientation;
use Yeeefang\TcpdfNext\Core\Enums\Unit;

$doc = Document::create(
    pageSize: PageSize::Letter,
    orientation: Orientation::Landscape,
    unit: Unit::Inch,
);

Margin

Margin có thể đặt toàn cục hoặc theo trang:

php
use Yeeefang\TcpdfNext\Core\ValueObjects\Margin;

// Margin toàn cục
$doc->setMargins(new Margin(
    left: 20,
    top: 30,
    right: 20,
    bottom: 25,
));

// Ghi đè cho trang cụ thể
$doc->addPage(
    margin: new Margin(left: 10, top: 10, right: 10, bottom: 10),
);

Đường dẫn và thư mục Font

TCPDF-Next tìm file font trong tập hợp thư mục có thể cấu hình. Các font có sẵn (Helvetica, Courier, Times, Symbol, ZapfDingbats) luôn khả dụng. Để dùng font tùy chỉnh hoặc Unicode, đăng ký thêm đường dẫn:

php
use Yeeefang\TcpdfNext\Core\Config\FontConfig;

$doc->configureFonts(function (FontConfig $config): void {
    // Thêm thư mục chứa file .ttf / .otf
    $config->addDirectory('/path/to/my/fonts');

    // Thêm file font đơn lẻ với alias
    $config->addFont('/path/to/MyFont-Regular.ttf', alias: 'MyFont');
});

TIP

File font được nhúng dạng subset theo mặc định, giữ cho PDF output nhỏ gọn. Có thể bật nhúng đầy đủ cho từng font khi cần.

Thiết lập mã hóa

Mã hóa PDF được cấu hình qua value object EncryptionConfig:

php
use Yeeefang\TcpdfNext\Core\Config\EncryptionConfig;
use Yeeefang\TcpdfNext\Core\Enums\EncryptionLevel;
use Yeeefang\TcpdfNext\Core\Enums\Permission;

$doc->setEncryption(new EncryptionConfig(
    level: EncryptionLevel::AES256,
    userPassword: 'reader-pass',
    ownerPassword: 'admin-pass',
    permissions: [
        Permission::Print,
        Permission::Copy,
    ],
));
LevelMô tả
EncryptionLevel::RC4_40RC4 40-bit cũ (không khuyến nghị)
EncryptionLevel::RC4_128RC4 128-bit
EncryptionLevel::AES128AES 128-bit
EncryptionLevel::AES256AES 256-bit (khuyến nghị)

Thiết lập Tagged PDF

Tagged PDF (PDF trợ năng) cải thiện hỗ trợ trình đọc màn hình và được yêu cầu bởi PDF/UA. Bật tagging toàn cục:

php
$doc->enableTaggedPdf();

// Tùy chọn đặt ngôn ngữ document cho trợ năng
$doc->setLanguage('en');

Khi chế độ tagged PDF được bật, các tag cấu trúc (<P>, <H1><H6>, <Table>, v.v.) được tự động sinh bởi API văn bản và bảng.

Chế độ Deterministic

Mặc định, PDF chứa timestamp và định danh duy nhất khiến mỗi output khác nhau. Chế độ deterministic loại bỏ chúng, tạo output byte-giống-hệt cho cùng input — hữu ích cho snapshot testing và reproducible build:

php
$doc->enableDeterministicMode();

WARNING

Chế độ deterministic loại bỏ ngày tạo/sửa đổi và file identifier duy nhất. Không dùng cho tài liệu cần các trường metadata này.

Ví dụ cấu hình đầy đủ

Dưới đây là snippet hiển thị tất cả tùy chọn cấu hình chính:

php
use Yeeefang\TcpdfNext\Core\Document;
use Yeeefang\TcpdfNext\Core\Enums\PageSize;
use Yeeefang\TcpdfNext\Core\Enums\Orientation;
use Yeeefang\TcpdfNext\Core\Enums\Unit;
use Yeeefang\TcpdfNext\Core\Enums\EncryptionLevel;
use Yeeefang\TcpdfNext\Core\Enums\Permission;
use Yeeefang\TcpdfNext\Core\Config\EncryptionConfig;
use Yeeefang\TcpdfNext\Core\Config\FontConfig;
use Yeeefang\TcpdfNext\Core\ValueObjects\Margin;

$doc = Document::create(
    pageSize: PageSize::A4,
    orientation: Orientation::Portrait,
    unit: Unit::Millimeter,
);

// Margin
$doc->setMargins(new Margin(
    left: 15,
    top: 27,
    right: 15,
    bottom: 25,
));

// Font
$doc->configureFonts(function (FontConfig $config): void {
    $config->addDirectory('/path/to/fonts');
});

// Mã hóa
$doc->setEncryption(new EncryptionConfig(
    level: EncryptionLevel::AES256,
    userPassword: '',
    ownerPassword: 'secret',
    permissions: [Permission::Print],
));

// Trợ năng
$doc->enableTaggedPdf();
$doc->setLanguage('en');

// Output deterministic (cho test)
// $doc->enableDeterministicMode();

// Metadata
$doc->setTitle('Company Report');
$doc->setAuthor('TCPDF-Next');
$doc->setSubject('Monthly Summary');
$doc->setKeywords('report, finance, 2026');

$doc->addPage();
$doc->cell(text: 'Hello, configured world!');
$doc->save('/tmp/configured.pdf');

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