Skip to content

Novis\Pdf\Page::isLast

$page->isLast(): bool

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

isLast returns true when the page number is the same as the page count. Use it in a footer function to write something only at the end, such as a signature line. You can also use it to write "Continued on the next page" on every other page. A document with one page is both the first page and the last page.

The examples below write a note on every page except the last, put a signature line on the last page, and show a document of one page.

Return value

Returns bool.

Examples

Continued on the next page

<?nvs
// Shows a footer that says the invoice goes on, on every page except the last.
use Novis\Pdf\Document;
use Novis\Pdf\Page;
string $rows = '';
for (int $n = 1; $n <= 60; $n++) {
$rows .= "<tr><td>Tea {$n}</td><td>1.50 EUR</td></tr>";
}
var $html = "<h1>Invoice 1042</h1><table><tbody>{$rows}</tbody></table>";
var $footer = fn(Page $page): Core\Html\Markup => {
string $text = $page->isLast() ? 'End of invoice' : 'Continued on the next page';
echo 'page ', $page->number, ': ', $text, "\n";
return html`<p>{$text}</p>`;
};
Document::fromHtml($html)->withFooter($footer)->render();
Output
page 1: Continued on the next page
page 2: End of invoice

A signature line on the last page

<?nvs
// Shows a contract with a signature line in the footer of the last page only.
use Novis\Pdf\Document;
use Novis\Pdf\Page;
use Novis\Pdf\Reader;
var $html = '<style>h2 { break-before: page }</style>'
. '<h1>Contract</h1><p>Shop rents a room.</p><h2>Terms</h2><p>The rent is due each month.</p>';
var $footer = fn(Page $page): Core\Html\Markup => $page->isLast() ? html`<p>Signature: ________</p>` : html``;
// This reads the text of each page back from the PDF file.
// The PDF file stores the footer after the body, so the footer text comes last.
var $reader = Reader::open(Document::fromHtml($html)->withFooter($footer)->render()->bytes);
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: Contract / Shop rents a room.
page 2: Terms / The rent is due each month. / Signature: ________

One page is first and last

<?nvs
// Shows that the only page of a short document is both the first and the last page.
use Novis\Pdf\Document;
use Novis\Pdf\Page;
var $header = fn(Page $page): Core\Html\Markup => {
echo 'page ', $page->number, ' of ', $page->count, "\n";
echo 'first: ', $page->isFirst() ? 'yes' : 'no', "\n";
echo 'last: ', $page->isLast() ? 'yes' : 'no', "\n";
return html`<p>Shop receipt</p>`;
};
Document::fromHtml('<p>One coffee, 3.20 EUR</p>')->withHeader($header)->render();
Output
page 1 of 1
first: yes
last: yes