Skip to content

다중 페이지 문서

자동 페이지 나누기, 커스텀 머리글/바닥글, 페이지 번호 매기기로 여러 페이지에 걸친 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)

출력

이 예제는 가운데 정렬된 표지, 자동 페이지 나누기로 추가 페이지에 흐르는 세 개의 챕터, 표지를 제외한 모든 페이지에 "Page X of Y" 바닥글이 있는 다중 페이지 PDF를 생성합니다.

LGPL-3.0-or-later 라이선스로 배포됩니다.