Novis\Pdf\Document::withHeader
$document->withHeader(
callable(Page): Core\Html\Markup $fn,
?{ height?: string } $options = null,
): DocumentSets a function that returns the header of each page of a document.
render calls the function once for each page, with that page as a Page. The Page has the page number, the number of pages, and values that the HTML sets for that page, such as the current chapter. The function returns an html template, and render draws it in the top margin of that page.
The header never moves the body of the document. height, such as { height: "3cm" }, sets the top margin before the body is placed on the pages. Without it, the margin is the one that CSS @page gives. A header taller than its margin is cut off at the margin. Rendered::overflows lists it with its page number.
withHeader returns a new Document and does not change the one you called it on.
Good to know: a header that needs no logic can be plain CSS. @page { @top-right { content: counter(page) } } writes the page number. When both are there, the function's header is drawn.
The examples below number the pages of a report, show the chapter of each page, and make room for a taller header.
Parameters
$fncallable(Page): Core\Html\Markup$options?{ height?: string }default null
Return value
Returns Document.
Changelog
| Version | Change |
|---|---|
0.0.6 | Each Novis\Pdf\Document method that returns a new document now starts with with, for example withHeader() and withPassword(). created() is now withCreationTime(). |
Examples
Page numbers in a report
<?nvs// Shows a report with "Page 1 of 3" in the header of each page.use Novis\Pdf\Document;use Novis\Pdf\Page;
string $html = '<h1>Orders in March</h1>' . Core\Str::repeat('<p>Order 1042 was sent on time, and the customer paid by card.</p>', 120);
// `render` calls the function once for each page, with that page as a `Page`.// `$page->number` is the page and `$page->count` is the number of pages.var $header = fn(Page $page): Core\Html\Markup => { echo 'header for page ', $page->number, "\n"; return html`<p style="text-align: right">Page {$page->number} of {$page->count}</p>`;};
var $rendered = Document::fromHtml($html)->withHeader($header)->render();echo $rendered->pageCount, " pages\n";header for page 1header for page 2header for page 3header for page 4header for page 55 pagesThe chapter in the header
<?nvs// Shows a manual with the name of the current chapter in the header.use Novis\Pdf\Document;use Novis\Pdf\Page;use Novis\Pdf\Reader;
// `string-set` saves the text of each `h2` under the name `chapter`.string $html = '<style>h2 { break-before: page; string-set: chapter content() }</style>' . '<h1>Shop manual</h1><h2>Orders</h2><p>Each order has a number.</p>' . '<h2>Returns</h2><p>A customer may return an item within 30 days.</p>';
// `$page->string("chapter")` returns the chapter on this page. The first page has none, so it is `null`.var $header = fn(Page $page): Core\Html\Markup => { string $chapter = $page->string('chapter') ?? 'Shop manual'; return html`<p>{$chapter}</p>`;};
// This prints the first line of each page, which is its header.var $reader = Reader::open(Document::fromHtml($html)->withHeader($header)->render()->bytes);for (uint $n = 1; $n <= $reader->pageCount; $n++) { echo 'page ', $n, ': ', Core\Str::before($reader->text($n), "\n") ?? '', "\n";}page 1: Shop manualpage 2: Orderspage 3: ReturnsA taller header
<?nvs// Shows how to make room for a header with a logo and an address.use Novis\Pdf\Document;use Novis\Pdf\Page;
string $html = Core\Str::repeat('<p>Order 1042 was sent on time, and the customer paid by card.</p>', 160);var $header = fn(Page $page): Core\Html\Markup => html`<h2>Shop</h2><p>1 Example Street, Example City</p>`;
// Without `height`, the header has the top margin that `@page` gives.var $plain = Document::fromHtml($html)->withHeader($header)->render();echo $plain->pageCount, ' pages, ', Core\Arr::count($plain->overflows), " headers too tall\n";
// `height` sets the top margin. The body has less room on each page, so there are more pages.var $tall = Document::fromHtml($html)->withHeader($header, { height: '4cm' })->render();echo $tall->pageCount, ' pages, ', Core\Arr::count($tall->overflows), " headers too tall\n";6 pages, 6 headers too tall7 pages, 0 headers too tall