Skip to content

页面 (HasPages)

新增页面

php
use Yeeefang\TcpdfNext\Core\Document;
use Yeeefang\TcpdfNext\ValueObjects\PageSize;
use Yeeefang\TcpdfNext\Contracts\Orientation;

$pdf = Document::create()
    ->addPage()                                          // A4 纵向(默认)
    ->addPage(PageSize::Letter())                        // US Letter
    ->addPage(PageSize::A3(), Orientation::Landscape)    // A3 横向
    ->addPage(PageSize::Legal());                        // US Legal

页面尺寸

PageSizefinal readonly 值对象,提供工厂方法:

ISO 216 A 系列

A0(), A1(), A2(), A3(), A4(), A5(), A6()

ISO 216 B 系列

B0(), B1(), B2(), B3(), B4(), B5()

北美

Letter(), Legal(), Tabloid()

自定义尺寸

php
// 宽度与高度以点为单位(1 pt = 1/72 英寸)
$custom = new PageSize(400.0, 600.0, 'custom');

// 或从名称字符串创建
$a4 = PageSize::fromName('A4');

方向切换

php
$landscape = PageSize::A4()->landscape();
$portrait = PageSize::A4()->portrait();

边距

php
use Yeeefang\TcpdfNext\ValueObjects\Margin;

$pdf->setMargins(Margin::symmetric(15.0, 10.0))  // 上下 15mm、左右 10mm
    ->setLeftMargin(20.0)
    ->setTopMargin(25.0)
    ->setRightMargin(20.0);

工厂方法:

  • Margin::uniform(10.0) — 四边 10mm
  • Margin::symmetric(15.0, 10.0) — 上下 15mm、左右 10mm
  • Margin::zero() — 无边距
  • new Margin(top, right, bottom, left) — 明确指定

自动分页

php
$pdf->setAutoPageBreak(true, 20);  // 距底部 20mm 时分页
$pdf->setAutoPageBreak(false);      // 停用

启用时,超出页面的内容会自动触发 addPage()

位置

php
$pdf->setX(50.0);          // 设置水平位置
$pdf->setY(100.0);         // 设置垂直位置
$pdf->setXY(50.0, 100.0);  // 同时设置

$x = $pdf->getX();
$y = $pdf->getY();
$width = $pdf->getPageWidth();
$height = $pdf->getPageHeight();
$margins = $pdf->getMargins();  // 返回 Margin 对象

页面操作

php
$pdf->movePage(3, 1);   // 将第 3 页移至位置 1
$pdf->copyPage(2);      // 复制第 2 页
$pdf->deletePage(4);    // 删除第 4 页

页面分组

将页面分组以独立编号(例如每章节独立页码):

php
$pdf->startPageGroup();
$groupPageNo = $pdf->getGroupPageNo();

页面区域

定义内容应避开的矩形区域:

php
$pdf->addPageRegion(10, 10, 50, 50);  // x, y, 宽度, 高度

$regions = $pdf->getPageRegions();
$pdf->removePageRegion(0);
$pdf->setPageRegions($regions);

页面信息

php
$currentPage = $pdf->getPage();       // 当前页面索引
$totalPages = $pdf->getNumPages();    // 总页数

以 LGPL-3.0-or-later 许可证发布。