Novis\Pdf\Page::constructor
$page->constructor(
uint $number,
uint $count,
?string $name,
array<Named> $strings,
array<array<Named>> $marks,
): voidMakes one page of a PDF document, with its number, the page count and the values a header or footer needs.
Document::render makes a Page for each page and passes it to your header, footer or slots function. number is the page number, counted from 1. count is the number of pages in the document. name is the named page that CSS page gives it, or null.
You can also write new Page yourself. A test can then call a header function with any page it wants, without a render. The last two values are the string-set values of this page and the marks of every page. Each is a list of names and values.
The examples below test a footer function with a page made by hand, print the number and count of each page in a render, and show a named cover page.
Parameters
$numberuintthe number of this page, starting at 1$countuintthe number of pages in the document$name?stringthe page name from the CSSpageproperty of the first block on this page. It isnullwhen the page has no name.$stringsarray<Named>the value of each CSSstring-setname on this page$marksarray<array<Named>>thedata-markvalues of every page of the document, one list for each page
Return value
Returns void.
Examples
Test a footer without a render
<?nvs// Shows how to test a footer function with a page you make yourself.use Novis\Pdf\Page;
// The footer prints the page number and the page count.var $footer = fn(Page $page): Core\Html\Markup => html`<p>Page {$page->number} of {$page->count}</p>`;
// This page is page 2 of 5. It has no named page, no `string-set` values and no marks.var $page = new Page(2, 5, null, [], []);
// This prints `<p>Page 2 of 5</p>`.echo Core\Html::toSource($footer($page), 'the example prints the markup'), "\n";<p>Page 2 of 5</p>Number and count in a render
<?nvs// Shows the page number and the page count that a render gives a header function.use Novis\Pdf\Document;use Novis\Pdf\Page;
// Each `h2` starts a new page, so the document has three pages.var $html = '<style>h2 { break-before: page }</style>' . '<h1>Shop catalogue</h1><h2>Books</h2><p>A book</p><h2>Toys</h2><p>A ball</p>';
// `render` calls the header function once for each page.var $header = fn(Page $page): Core\Html\Markup => { echo 'header of page ', $page->number, ' of ', $page->count, "\n"; return html`<p>Shop catalogue, page {$page->number} of {$page->count}</p>`;};
var $rendered = Document::fromHtml($html)->withHeader($header)->render();echo $rendered->pageCount, " pages\n";header of page 1 of 3header of page 2 of 3header of page 3 of 33 pagesA named cover page
<?nvs// Shows how a footer finds the cover page of a report through the name of the page.use Novis\Pdf\Document;use Novis\Pdf\Page;
// CSS `page: cover` puts the first block on a page named `cover`.var $html = '<style>@page cover { margin: 40mm } .cover { page: cover } h2 { break-before: page }</style>' . "<div class=\"cover\"><h1>Annual report</h1></div>" . '<h2>Sales</h2><p>Sales grew.</p><h2>Costs</h2><p>Costs fell.</p>';
// `name` is `cover` on the cover page and `null` on the other pages.var $footer = fn(Page $page): Core\Html\Markup => { if ($page->name == 'cover') { echo 'page ', $page->number, ": no footer\n"; return html``; } echo 'page ', $page->number, ": footer\n"; return html`<p>Annual report, page {$page->number}</p>`;};
echo Document::fromHtml($html)->withFooter($footer)->render()->pageCount, " pages\n";page 1: no footerpage 2: footerpage 3: footer3 pages