マルチページドキュメント
自動改ページ、カスタムヘッダー/フッター、ページ番号付きの複数ページにわたるPDFを生成します。
完全なサンプル
php
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use TcpdfNext\Document;
use TcpdfNext\Enums\Alignment;
$pdf = Document::create()
->setTitle('Annual Report 2026')
->setAutoPageBreak(enabled: true, margin: 25);
// -- ページ1:タイトルページ --------------------------------------------------
$pdf->addPage()
->setY(100)
->setFont('helvetica', style: 'B', size: 28)
->cell(0, 15, 'Annual Report 2026', align: Alignment::Center, newLine: true)
->setFont('helvetica', size: 14)
->setTextColor(108, 117, 125)
->cell(0, 10, 'Acme Corporation', align: Alignment::Center, newLine: true)
->setTextColor(33, 37, 41);
// -- 本文ページ:自動改ページ付きチャプター ---------------------------
$chapters = [
'Executive Summary' => 'Revenue grew 18% year-over-year to $5.8 billion, driven by strong demand across all product lines and geographic expansion into Southeast Asia.',
'Market Analysis' => 'The global widget market reached $42 billion with a 7.2% CAGR. Our market share increased from 12.1% to 13.8%, placing us second worldwide.',
'Financial Statements' => 'Operating income rose to $870 million. Free cash flow exceeded $400 million, enabling continued investment in R&D and strategic acquisitions.',
];
foreach ($chapters as $title => $body) {
$pdf->addPage()
->setFont('helvetica', style: 'B', size: 20)
->cell(0, 12, $title, newLine: true)
->setFont('helvetica', size: 11);
// 本文テキストを繰り返して自動改ページをトリガー
for ($i = 0; $i < 10; $i++) {
$pdf->multiCell(0, 7, $body, align: Alignment::Justified);
}
}
// -- ヘッダーとフッター(レンダリング後) -------------------------------------
$total = $pdf->getNumPages();
for ($p = 2; $p <= $total; $p++) { // タイトルページをスキップ
$pdf->setPage($p)
// ヘッダー
->setFont('helvetica', style: 'I', size: 8)
->setTextColor(150, 150, 150)
->setXY(15, 8)
->cell(0, 5, 'Acme Corporation -- Annual Report 2026')
// フッター
->setXY(0, 285)
->cell(210, 5, "Page {$p} of {$total}", align: Alignment::Center);
}
$pdf->setTextColor(33, 37, 41)
->save(__DIR__ . '/multi-page.pdf');
echo "PDF created -- {$total} pages." . PHP_EOL;重要なコンセプト
自動改ページ
php
->setAutoPageBreak(enabled: true, margin: 25)カーソルが下端からmargin mmの位置に達すると、新しいページが自動的に挿入され、コンテンツは上マージンから続行されます。
手動ページ挿入
php
use TcpdfNext\Enums\Orientation;
->addPage() // A4縦(デフォルト)
->addPage(orientation: Orientation::Landscape) // 横向きを強制ページ番号
TCPDF-Nextはページ番号を自動挿入しません。2パスアプローチを使用してください -- まずすべてのコンテンツを書き込み、次に各ページに番号をスタンプします:
php
$total = $pdf->getNumPages();
for ($p = 1; $p <= $total; $p++) {
$pdf->setPage($p)
->setXY(0, 285)
->cell(210, 5, "Page {$p} of {$total}", align: Alignment::Center);
}TIP
総ページ数はすべてのコンテンツが書き込まれるまで不明なため、2パスパターンが必要です。
ページグループ
セクションベースのページ番号(例:各チャプターで1から再開始)にはstartPageGroup()を使用します:
php
$pdf->startPageGroup();
$currentGroupPage = $pdf->getGroupPageNo();マージン
php
use TcpdfNext\Core\Margin;
->setMargins(new Margin(left: 15, top: 20, right: 15))または個別に設定:
php
->setLeftMargin(15)
->setTopMargin(20)
->setRightMargin(15)出力
このサンプルは、中央揃えのタイトルページ、自動改ページで追加ページにフローする3つのチャプター、タイトルページを除くすべてのページに「Page X of Y」フッターを含むマルチページPDFを生成します。