Skip to content

Navigation (HasNavigation)

Trait HasNavigation và các module nền (BookmarkManager, TocManager, AnnotationManager, FileAttachment) cung cấp tính năng navigation PDF: bookmark phân cấp, mục lục tự tạo, link nội bộ và bên ngoài, named destination, chú thích và file đính kèm nhúng. Mọi method trả về static, nên mỗi lệnh gọi có thể chain.

Tham chiếu nhanh

MethodTính năng
bookmark()Thêm bookmark / outline entry phân cấp
addTOC()Tự tạo mục lục với dot leader
addHTMLTOC()Mục lục kiểu HTML
addLink()Tạo đích link nội bộ (trả về link ID)
setLink()Đặt vị trí đích link nội bộ
setDestination()Tạo named destination anchor
annotation()Thêm chú thích văn bản
addFileAttachment()Nhúng file đính kèm trong PDF

Ví dụ cơ bản

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->addPage()
    ->setFont('Helvetica', '', 12)

    // Bookmark
    ->bookmark('Chapter 1', 0)
    ->cell(0, 10, 'Chapter 1: Introduction', newLine: true)
    ->bookmark('Section 1.1', 1)
    ->cell(0, 10, '1.1 Getting Started', newLine: true)

    // Link nội bộ
    ->addPage()
    ->bookmark('Chapter 2', 0)
    ->cell(0, 10, 'Chapter 2: Advanced Topics', newLine: true)

    // File đính kèm
    ->addFileAttachment('/path/to/data.xlsx', 'data.xlsx', 'Supporting data')

    // Tự tạo TOC cuối cùng (chèn tại trang 1)
    ->addTOC(1, ' . ', 'Table of Contents');

Bookmark / Outline

php
$pdf->bookmark(string $txt, int $level = 0, float $y = -1, int $page = -1, string $style = '', array $color = []);

Bookmark xuất hiện trong panel outline của trình đọc PDF. Lồng chúng bằng cách tăng $level.

Mục lục

php
$pdf->addTOC(int $page, string $numberSuffix = '', string $bookmarkText = '');

Gọi addTOC() sau khi tất cả bookmark đã được thêm. TOC được xây từ cây bookmark và chèn tại vị trí trang chỉ định. Dùng addHTMLTOC() để kiểm soát đầy đủ style entry qua HTML và CSS.

Tạo tham chiếu chéo có thể click giữa các trang:

php
$linkId = $pdf->addLink();

$pdf->write(10, 'Jump to Chapter 2', link: $linkId)
    ->addPage()
    ->setLink($linkId, y: 0)
    ->cell(0, 10, 'Chapter 2 starts here', newLine: true);

Truyền chuỗi URL làm tham số $link trên cell(), write() hoặc image():

php
$pdf->cell(0, 10, 'Visit our website', link: 'https://example.com', newLine: true)
    ->write(10, 'Click here', link: 'https://docs.example.com');

Named Destination

php
$pdf->setDestination(string $name, float $y = -1, int $page = -1);

Named destination cho phép document bên ngoài hoặc URL link đến vị trí cụ thể qua fragment #name.

Chú thích

php
$pdf->annotation(float $x, float $y, float $w, float $h, string $text, array $opt = []);

Chú thích xuất hiện dạng icon sticky-note trong trình xem PDF.

php
$pdf->annotation(50, 80, 10, 10, 'Review this section before release.', [
    'subtype' => 'Text',
    'icon'    => 'Comment',
    'color'   => [255, 255, 0],
]);

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