頁面管理¶
NextPDF 的頁面管理 API 設計為流暢且直觀。每份文件至少需要一頁,頁面可以在任何時間點新增,並支援混合頁面尺寸——例如在同一份文件中混合 A4 直向與 A3 橫向。
新增頁面¶
use NextPDF\Core\Document;
use NextPDF\ValueObjects\PageSize;
use NextPDF\ValueObjects\Orientation;
// 使用文件預設設定新增頁面
$document->addPage();
// 指定特定尺寸(覆蓋文件預設值)
$document->addPage(pageSize: PageSize::A3);
// 指定尺寸與方向
$document->addPage(
pageSize: PageSize::A4,
orientation: Orientation::Landscape,
);
ISO 216 標準紙張尺寸¶
NextPDF 內建完整的 ISO 216 A 系列與 B 系列,以及常用的北美尺寸:
use NextPDF\ValueObjects\PageSize;
// A 系列(ISO 216)
PageSize::A0 // 841 × 1189 mm
PageSize::A4 // 210 × 297 mm(最常用)
PageSize::A5 // 148 × 210 mm
// B 系列(ISO 216)
PageSize::B4 // 250 × 353 mm
PageSize::B5 // 176 × 250 mm
// 北美尺寸
PageSize::Letter // 215.9 × 279.4 mm
PageSize::Legal // 215.9 × 355.6 mm
PageSize::Tabloid // 279.4 × 431.8 mm
自訂頁面尺寸¶
use NextPDF\ValueObjects\PageSize;
use NextPDF\ValueObjects\Dimension;
use NextPDF\ValueObjects\Unit;
// 使用毫米定義自訂尺寸
$customSize = PageSize::custom(
width: Dimension::of(value: 100.0, unit: Unit::Millimeter),
height: Dimension::of(value: 150.0, unit: Unit::Millimeter),
);
$document->addPage(pageSize: $customSize);
頁面方向¶
use NextPDF\ValueObjects\Orientation;
// 橫向:自動交換寬度與高度
$document->addPage(
pageSize: PageSize::A4,
orientation: Orientation::Landscape,
);
// 查詢當前頁面尺寸(考慮方向後的實際值)
$actualSize = $document->context()->currentPageSize;
// $actualSize->width = 297.0, $actualSize->height = 210.0(A4 橫向)
頁面屬性¶
// 取得頁面資訊
$pageCount = $document->getPageCount();
$currentPage = $document->context()->currentPageNumber; // 1-based
跳頁與插頁¶
// 在特定位置插入頁面(插入後,後續頁面順延)
$document->insertPageAt(position: 3, pageSize: PageSize::A4);
// 刪除頁面
$document->removePage(pageNumber: 2);
參見¶
- Value Objects — PageSize、Dimension、Orientation 型別定義
- 文件生命週期 — Document 物件與 RenderingContext
- PDF 合併 — 多 PDF 合併時的頁面處理
- 書籤 — 頁面導航書籤