<?php
declare(strict_types=1);
/**
* E095_page_labels.php
*
* Page labels shown by PDF viewers in place of the physical page numbers: a body
* numbered from 3, a roman numbered front matter, a prefix-only cover label and a
* prefixed appendix. Each page states the label a viewer shows for it.
*
* @since 2026-11-28
* @category Library
* @package Pdf
* @author Nicola Asuni <info@tecnick.com>
* @copyright 2002-2026 Nicola Asuni - Tecnick.com LTD
* @license https://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE)
* @link https://github.com/tecnickcom/tc-lib-pdf
*
* This file is part of tc-lib-pdf software library.
*/
// NOTE: local file reads (images, fonts, attachments) are restricted to an allowlist of
// trusted paths that covers this package tree, so run the examples in place. To read assets
// from other locations, list them in the 'allowedPaths' entry of the fileOptions constructor
// parameter (see E047_remote_resources_security.php).
// NOTE: run make fonts in the project root to generate the dependencies and example fonts.
// autoloader when using Composer
require __DIR__ . '/../vendor/autoload.php';
// define fonts directory
\define('K_PATH_FONTS', \realpath(__DIR__ . '/../vendor/tecnickcom/tc-lib-pdf-font/target/fonts'));
$pdf = new \Com\Tecnick\Pdf\Tcpdf();
$pdf->setCreator('tc-lib-pdf');
$pdf->setAuthor('Nicola Asuni');
$pdf->setSubject('tc-lib-pdf example: 095');
$pdf->setTitle('Page Labels');
$pdf->setKeywords('TCPDF tc-lib-pdf example page labels numbering roman prefix viewer');
$pdf->setPDFFilename('095_page_labels.pdf');
/**
* Adds a page with the given page data and writes its title and expected label.
*
* @param array<string, mixed> $data Page data passed to addPage().
*/
$addPage = function (string $title, string $expected, array $data = []) use ($pdf): void {
$pdf->addPage($data);
$font = $pdf->font->insert($pdf->pon, 'helvetica', 'B', 16);
$pdf->page->addContent($font['out']);
$pdf->page->addContent($pdf->getTextCell($title, 15, 20, 180));
$font = $pdf->font->insert($pdf->pon, 'helvetica', '', 11);
$pdf->page->addContent($font['out']);
$pdf->page->addContent($pdf->getTextCell('Label shown by the viewer: ' . $expected, 15, 32, 180));
};
// ----------
// The first document part: a cover, a front matter and a body numbered from 3.
// ----------
// A label with no numbering style shows only its prefix.
$addPage('Cover', 'Cover', ['label' => ['style' => '', 'prefix' => 'Cover']]);
// Lower case roman numbers from i.
$addPage('Front matter', 'i', ['label' => ['style' => 'r']]);
$addPage('Front matter', 'ii');
// Decimal numbers from 3, as in a printed book where the cover counts as pages 1 and 2.
$addPage('Body', '3', ['label' => ['start' => 3]]);
$addPage('Body', '4');
$addPage('Body', '5');
// ----------
// The appendix: a label set after the page is added, with a prefix.
// ----------
$addPage('Appendix', 'A-1');
$pdf->page->setLabel(['prefix' => 'A-']);
$addPage('Appendix', 'A-2');
// =============================================================
$rawpdf = $pdf->getOutPDFString();
$pdf->renderPDF(rawpdf: $rawpdf);