Skip to content

Multi-Page Documents

Generate PDFs that span multiple pages with automatic breaks, custom headers/footers, and page numbering.

Full Example

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);

// -- Page 1: Title Page --------------------------------------------------
$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);

// -- Body Pages: Chapters with auto page break ---------------------------
$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);

    // Repeat body text to trigger automatic page breaks
    for ($i = 0; $i < 10; $i++) {
        $pdf->multiCell(0, 7, $body, align: Alignment::Justified);
    }
}

// -- Headers & Footers (post-render) -------------------------------------
$total = $pdf->getNumPages();

for ($p = 2; $p <= $total; $p++) {         // skip title page
    $pdf->setPage($p)
        // Header
        ->setFont('helvetica', style: 'I', size: 8)
        ->setTextColor(150, 150, 150)
        ->setXY(15, 8)
        ->cell(0, 5, 'Acme Corporation -- Annual Report 2026')
        // Footer
        ->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;

Key Concepts

Automatic Page Breaks

php
->setAutoPageBreak(enabled: true, margin: 25)

When the cursor reaches margin mm from the bottom edge, a new page is inserted automatically and content continues at the top margin.

Manual Page Insertion

php
use TcpdfNext\Enums\Orientation;

->addPage()                                   // A4 portrait (default)
->addPage(orientation: Orientation::Landscape) // force landscape

Page Numbering

TCPDF-Next does not insert page numbers automatically. Use a two-pass approach -- write all content first, then stamp numbers on each page:

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

The total page count is unknown until all content has been written, which is why the two-pass pattern is necessary.

Page Groups

Use startPageGroup() for section-based numbering (e.g., restart at 1 for each chapter):

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

Margins

php
use TcpdfNext\Core\Margin;

->setMargins(new Margin(left: 15, top: 20, right: 15))

Or set them individually:

php
->setLeftMargin(15)
->setTopMargin(20)
->setRightMargin(15)

Output

This example generates a multi-page PDF with a centered title page, three chapters that flow across additional pages via auto page break, and a "Page X of Y" footer on every page except the title page.

Released under the LGPL-3.0-or-later License.