Novis\Pdf\Page::string
Returns a value that CSS string-set saves on a page, such as the title of the current chapter.
In CSS, h2 { string-set: chapter content() } saves the text of each h2 under the name chapter. string("chapter") returns the value for this page. If no h2 is on the page, the value is the last one from an earlier page. If no value is set yet, the result is null.
The name must have the same case as in the CSS. For the name chapter, string("Chapter") returns null.
The examples below put the chapter title in the header, show the null before the first chapter, and put the customer name on each page of a batch of invoices.
Parameters
$namestring
Return value
Returns ?string.
Examples
The chapter title in the header
<?nvs// Shows the title of the current chapter in the header of each page.use Novis\Pdf\Document;use Novis\Pdf\Page;
// Each `h2` starts a new page and saves its text under the name `chapter`.var $html = '<style>h2 { break-before: page; string-set: chapter content() }</style>' . '<h2>Getting started</h2><p>Install the app.</p>' . '<h2>Orders</h2><p>Find an order.</p>' . '<h2>Returns</h2><p>Send an item back.</p>';
var $header = fn(Page $page): Core\Html\Markup => { string $title = $page->string('chapter') ?? ''; echo 'page ', $page->number, ': ', $title, "\n"; return html`<p>Shop handbook, {$title}</p>`;};
Document::fromHtml($html)->withHeader($header)->render();page 1: Getting startedpage 2: Orderspage 3: ReturnsBefore the first chapter
<?nvs// Shows the value of `string` before the first chapter and on a page with no chapter title.use Novis\Pdf\Document;use Novis\Pdf\Page;
// Page 1 is a title page. Page 3 has no `h2`, because the long text of page 2 goes on.var $html = '<style>h1 { break-after: page } h2 { string-set: chapter content() }</style>' . '<h1>Shop handbook</h1><h2>Orders</h2>' . Core\Str::repeat('<p>Each order has a number. Search for the number to find the order.</p>', 40);
// Before the first `h2`, the result is `null`.var $header = fn(Page $page): Core\Html\Markup => { ?string $title = $page->string('chapter'); echo 'page ', $page->number, ': ', $title ?? 'null', "\n"; return html`<p>{$title ?? 'Shop handbook'}</p>`;};
Document::fromHtml($html)->withHeader($header)->render();page 1: nullpage 2: Orderspage 3: OrdersThe customer on each invoice
<?nvs// Shows the name of the customer in the header, in one PDF file with an invoice for each customer.use Novis\Pdf\Document;use Novis\Pdf\Page;
array<string> $customers = ['Ada Lovelace', 'Alan Turing', 'Grace Hopper'];
// Each invoice starts on a new page. Its `h1` saves the customer name under `customer`.string $html = '<style>h1 { break-before: page; string-set: customer content() }</style>';foreach ($customers as string $name) { $html .= "<h1>{$name}</h1><p>Invoice for one order.</p>";}
var $header = fn(Page $page): Core\Html\Markup => { string $customer = $page->string('customer') ?? ''; echo 'page ', $page->number, ': invoice for ', $customer, "\n"; return html`<p>Shop, invoice for {$customer}</p>`;};
Document::fromHtml($html)->withHeader($header)->render();page 1: invoice for Ada Lovelacepage 2: invoice for Alan Turingpage 3: invoice for Grace Hopper