ナビゲーション(HasNavigation)
HasNavigation トレイトとその基盤モジュール(BookmarkManager、TocManager、AnnotationManager、FileAttachment)は、PDFナビゲーション機能を提供します:階層型ブックマーク、自動生成目次、内部/外部リンク、名前付きデスティネーション、アノテーション、埋め込みファイル添付。すべてのメソッドは static を返すため、すべての呼び出しをチェーンできます。
クイックリファレンス
| メソッド | 機能 |
|---|---|
bookmark() | 階層型ブックマーク/アウトラインエントリを追加 |
addTOC() | ドットリーダー付き目次を自動生成 |
addHTMLTOC() | HTMLスタイルの目次 |
addLink() | 内部リンクデスティネーションを作成(リンクIDを返す) |
setLink() | 内部リンクのターゲット位置を設定 |
setDestination() | 名前付きデスティネーションアンカーを作成 |
annotation() | テキストアノテーションを追加 |
addFileAttachment() | PDFにファイル添付を埋め込む |
基本例
php
use Yeeefang\TcpdfNext\Core\Document;
$pdf = Document::create()
->addPage()
->setFont('Helvetica', '', 12)
// ブックマーク
->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)
// 内部リンク
->addPage()
->bookmark('Chapter 2', 0)
->cell(0, 10, 'Chapter 2: Advanced Topics', newLine: true)
// ファイル添付
->addFileAttachment('/path/to/data.xlsx', 'data.xlsx', 'Supporting data')
// 目次を自動生成(ページ1に挿入)
->addTOC(1, ' . ', 'Table of Contents');1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
ブックマーク / アウトライン
php
$pdf->bookmark(string $txt, int $level = 0, float $y = -1, int $page = -1, string $style = '', array $color = []);1
ブックマークはPDFリーダーのアウトラインパネルに表示されます。$level を増やすことでネストできます。
目次
php
$pdf->addTOC(int $page, string $numberSuffix = '', string $bookmarkText = '');1
すべてのブックマークが追加された後に addTOC() を呼び出します。目次はブックマークツリーから構築され、指定されたページ位置に挿入されます。HTMLとCSSによるエントリスタイルの完全な制御には addHTMLTOC() を使用します。
内部リンク
ページ間のクリック可能なクロスリファレンスを作成します:
php
use Yeeefang\TcpdfNext\Core\Document;
$pdf = Document::create()
->addPage()
->setFont('Helvetica', '', 12);
$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);1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
外部リンク
cell()、write()、または image() の $link パラメータにURL文字列を渡します:
php
$pdf->cell(0, 10, 'Visit our website', link: 'https://example.com', newLine: true)
->write(10, 'Click here', link: 'https://docs.example.com');1
2
2
名前付きデスティネーション
php
$pdf->setDestination(string $name, float $y = -1, int $page = -1);1
名前付きデスティネーションにより、外部ドキュメントやURLが #name フラグメントを通じて特定の場所にリンクできます。
アノテーション
php
$pdf->annotation(float $x, float $y, float $w, float $h, string $text, array $opt = []);1
アノテーションはPDFビューアで付箋アイコンとして表示されます。
php
$pdf->annotation(50, 80, 10, 10, 'Review this section before release.', [
'subtype' => 'Text',
'icon' => 'Comment',
'color' => [255, 255, 0],
]);1
2
3
4
5
2
3
4
5
ファイル添付
php
$pdf->addFileAttachment(string $file, string $name, string $desc);1
埋め込みファイルはPDFリーダーの添付パネルに表示されます。
php
$pdf->addFileAttachment('/reports/q4-data.xlsx', 'q4-data.xlsx', 'Q4 financial data')
->addFileAttachment('/reports/methodology.pdf', 'methodology.pdf', 'Research methodology');1
2
2