Skip to content

Layout (HasLayout)

The HasLayout trait and Layout module provide page-level structure: headers, footers, multi-column layouts, and booklet mode. The module is composed of PageManager, ColumnManager, BookletManager, and HeaderFooterManager.

All methods return static, so every call can be chained.

Headers and Footers

Built-in Header

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->setHeaderData('logo.png', 30, 'Company Name', 'Generated report — Confidential')
    ->setHeaderMargin(10)
    ->setFooterMargin(10)
    ->addPage();

setHeaderData() accepts a logo path, logo width, title string, and description string.

Custom Callbacks

For full control, register callbacks that receive the Document instance:

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create();

$pdf->setHeaderCallback(function (Document $doc) {
    $doc->setFont('Helvetica', 'B', 12)
        ->cell(0, 10, 'My Company — Confidential', align: 'C', newLine: true)
        ->line(10, 18, 200, 18);
});

$pdf->setFooterCallback(function (Document $doc) {
    $doc->setY(-15)
        ->setFont('Helvetica', '', 8)
        ->cell(0, 10, 'Page ' . $doc->getPage() . '/' . $doc->getNumPages(), align: 'C');
});

$pdf->addPage()
    ->setFont('Helvetica', '', 12)
    ->cell(0, 10, 'Content with custom header and footer');

Enable / Disable

php
$pdf->setPrintHeader(false);  // Suppress header rendering
$pdf->setPrintFooter(false);  // Suppress footer rendering

Margins

php
$pdf->setHeaderMargin(float $margin);  // Space above header
$pdf->setFooterMargin(float $margin);  // Space below footer

These margins define the gap between the page edge and the header/footer content.

Multi-Column Layout

Defining Columns

php
$pdf = Document::create()
    ->addPage()
    ->setColumnsArray([
        ['w' => 90, 's' => 5],  // Column 1: 90mm wide, 5mm space
        ['w' => 90, 's' => 0],  // Column 2: 90mm wide
    ])
    ->setColumn(0)
    ->setFont('Helvetica', '', 10)
    ->multiCell(0, 5, 'Left column content...')
    ->setColumn(1)
    ->multiCell(0, 5, 'Right column content...');

Each entry defines a column with w (width) and s (spacing). Use setColumn() to switch between columns.

Column Methods

php
$pdf->setColumnsArray(array $columns);  // Define column structure
$pdf->setColumn(int $col);              // Switch to column (0-based)
$col = $pdf->getColumn();               // Get current column index

Booklet Mode

Booklet mode alternates inner and outer margins so pages can be folded and bound:

php
$pdf->setBooklet(bool $val, float $inner, float $outer);
ParameterDescription
$valtrue to enable, false to disable
$innerInner (binding) margin in mm
$outerOuter (edge) margin in mm
php
$pdf = Document::create()
    ->setBooklet(true, 20, 10)
    ->addPage()   // Odd page: inner margin on left
    ->cell(0, 10, 'Page 1 — wider left margin for binding')
    ->addPage()   // Even page: inner margin on right
    ->cell(0, 10, 'Page 2 — wider right margin for binding');

The margin swap happens automatically on each addPage() call.

Choosing the Right Layout Tool

NeedSolution
Consistent branding on every pagesetHeaderData() or setHeaderCallback()
Page numbers in the footersetFooterCallback() with getPage() / getNumPages()
Newspaper-style columnssetColumnsArray() + setColumn()
Print-ready booklet outputsetBooklet()
Remove headers on certain pagessetPrintHeader(false) before addPage()

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