Skip to content

Novis\Pdf\Page::constructor

Makes 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 CSS page property of the first block on this page. It is null when the page has no name.
  • $stringsarray<Named>the value of each CSS string-set name on this page
  • $marksarray<array<Named>>the data-mark values 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";
Output
<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";
Output
header of page 1 of 3
header of page 2 of 3
header of page 3 of 3
3 pages

A 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";
Output
page 1: no footer
page 2: footer
page 3: footer
3 pages