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
| Method | Tí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
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');2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Bookmark / Outline
$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
$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.
Link nội bộ
Tạo tham chiếu chéo có thể click giữa các trang:
$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);2
3
4
5
6
Link bên ngoài
Truyền chuỗi URL làm tham số $link trên cell(), write() hoặc image():
$pdf->cell(0, 10, 'Visit our website', link: 'https://example.com', newLine: true)
->write(10, 'Click here', link: 'https://docs.example.com');2
Named Destination
$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
$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.
$pdf->annotation(50, 80, 10, 10, 'Review this section before release.', [
'subtype' => 'Text',
'icon' => 'Comment',
'color' => [255, 255, 0],
]);2
3
4
5