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
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:
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
$pdf->setPrintHeader(false); // Suppress header rendering
$pdf->setPrintFooter(false); // Suppress footer renderingMargins
$pdf->setHeaderMargin(float $margin); // Space above header
$pdf->setFooterMargin(float $margin); // Space below footerThese margins define the gap between the page edge and the header/footer content.
Multi-Column Layout
Defining Columns
$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
$pdf->setColumnsArray(array $columns); // Define column structure
$pdf->setColumn(int $col); // Switch to column (0-based)
$col = $pdf->getColumn(); // Get current column indexBooklet Mode
Booklet mode alternates inner and outer margins so pages can be folded and bound:
$pdf->setBooklet(bool $val, float $inner, float $outer);| Parameter | Description |
|---|---|
$val | true to enable, false to disable |
$inner | Inner (binding) margin in mm |
$outer | Outer (edge) margin in mm |
$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
| Need | Solution |
|---|---|
| Consistent branding on every page | setHeaderData() or setHeaderCallback() |
| Page numbers in the footer | setFooterCallback() with getPage() / getNumPages() |
| Newspaper-style columns | setColumnsArray() + setColumn() |
| Print-ready booklet output | setBooklet() |
| Remove headers on certain pages | setPrintHeader(false) before addPage() |