Skip to content

Novis\Pdf\Page::isFirst

$page->isFirst(): bool

Checks whether a page is the first page of the document.

isFirst returns true when the page number is 1, and false on every other page. Use it in a header or footer function to give the first page a different header. A letter often has the full company address on the first page and only a short line on the pages after it.

Good to know: the CSS rule @page :first sets the margins and margin boxes of the first page. isFirst is for a header or footer that a function writes.

The examples below leave out the header on the first page, put a letterhead on the first page only, and print a page number on every page except a title page.

Return value

Returns bool.

Examples

No header on the first page

<?nvs
// Shows how to leave out the header on the first page of a document.
use Novis\Pdf\Document;
use Novis\Pdf\Page;
use Novis\Pdf\Reader;
var $html = '<style>p { break-after: page }</style><p>First page</p><p>Second page</p>';
// The header is empty on the first page.
var $header = fn(Page $page): Core\Html\Markup => $page->isFirst() ? html`` : html`<p>Shop letter</p>`;
// `Reader` reads the text of each page back from the PDF file.
// The PDF file stores the header after the body, so the header text comes last.
var $pdf = Document::fromHtml($html)->withHeader($header)->render()->bytes;
var $reader = Reader::open($pdf);
echo 'page 1: ', Core\Str::replace($reader->text(1), "\n", ' / '), "\n";
echo 'page 2: ', Core\Str::replace($reader->text(2), "\n", ' / '), "\n";
Output
page 1: First page
page 2: Second page / Shop letter

A letterhead on the first page

<?nvs
// Shows a letter with the full address on the first page and a short header after it.
use Novis\Pdf\Document;
use Novis\Pdf\Page;
var $letter = '<p>Dear customer,</p>' . Core\Str::repeat('<p>Thank you for your order. It is on its way.</p>', 60);
// The first page gets the full address. Every other page gets one short line.
var $header = fn(Page $page): Core\Html\Markup => {
if ($page->isFirst()) {
echo 'page ', $page->number, ": full address\n";
return html`<p><b>Shop</b><br>1 Example Street<br>shop@example.com</p>`;
}
echo 'page ', $page->number, ": short header\n";
return html`<p>Shop, page {$page->number}</p>`;
};
// `height` makes the top margin tall enough for the address.
Document::fromHtml($letter)->withHeader($header, { height: '3cm' })->render();
Output
page 1: full address
page 2: short header
page 3: short header

No number on the title page

<?nvs
// Shows a page number in the footer of every page except the title page.
use Novis\Pdf\Document;
use Novis\Pdf\Page;
var $html = '<style>h2 { break-before: page }</style>'
. '<h1>User guide</h1><h2>Install</h2><p>Download the file.</p><h2>Start</h2><p>Open the app.</p>';
// The title page does not count. So the second page of the file is page 1 of 2.
var $footer = fn(Page $page): Core\Html\Markup => {
if ($page->isFirst()) {
return html``;
}
string $text = 'Page ' . ($page->number - 1) as string . ' of ' . ($page->count - 1) as string;
echo $text, "\n";
return html`<p>{$text}</p>`;
};
Document::fromHtml($html)->withFooter($footer)->render();
Output
Page 1 of 2
Page 2 of 2