Skip to content

Dokumen Multi-Halaman

Buat PDF yang mencakup beberapa halaman dengan break otomatis, header/footer kustom, dan penomoran halaman.

Contoh Lengkap

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

// -- Halaman 1: Halaman Judul -----------------------------------------------
$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);

// -- Halaman Konten: Bab dengan 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);

    // Ulang teks body untuk memicu page break otomatis
    for ($i = 0; $i < 10; $i++) {
        $pdf->multiCell(0, 7, $body, align: Alignment::Justified);
    }
}

// -- Header & Footer (pasca-render) ------------------------------------------
$total = $pdf->getNumPages();

for ($p = 2; $p <= $total; $p++) {         // lewati halaman judul
    $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;

Konsep Utama

Page Break Otomatis

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

Ketika kursor mencapai margin mm dari tepi bawah, halaman baru disisipkan secara otomatis dan konten berlanjut di margin atas.

Penyisipan Halaman Manual

php
use TcpdfNext\Enums\Orientation;

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

Penomoran Halaman

TCPDF-Next tidak menyisipkan nomor halaman secara otomatis. Gunakan pendekatan two-pass -- tulis semua konten terlebih dahulu, lalu cap nomor di setiap halaman:

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

Total jumlah halaman tidak diketahui sampai semua konten telah ditulis, itulah mengapa pola two-pass diperlukan.

Grup Halaman

Gunakan startPageGroup() untuk penomoran berbasis bagian (misalnya, mulai ulang dari 1 untuk setiap bab):

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

Margin

php
use TcpdfNext\Core\Margin;

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

Atau atur secara individual:

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

Output

Contoh ini menghasilkan PDF multi-halaman dengan halaman judul yang di-center, tiga bab yang mengalir ke halaman tambahan via auto page break, dan footer "Page X of Y" di setiap halaman kecuali halaman judul.

Didistribusikan di bawah lisensi LGPL-3.0-or-later.