Skip to content

Novis\Pdf\Document::withFooter

$document->withFooter(
    callable(Page): Core\Html\Markup $fn,
    ?{ height?: string } $options = null,
): Document

Sets a function that returns the footer of each page of a document.

withFooter works like withHeader, in the bottom margin. render calls the function once for each page, with that page as a Page. The function returns an html template. height, such as { height: "2cm" }, sets the bottom margin.

A footer often needs numbers from the page. $page->isFirst() and $page->isLast() test where the page is. $page->marksUpTo("amount") returns every data-mark="amount:..." value up to this page, so the footer can show the total so far.

The footer never moves the body. A footer taller than its margin is cut off, and Rendered::overflows lists it with its page.

withFooter returns a new Document and does not change the one you called it on.

The examples below carry a total forward on an invoice, give the first page its own footer, and find a footer that does not fit.

Parameters

  • $fncallable(Page): Core\Html\Markup
  • $options?{ height?: string }default null

Return value

Returns Document.

Changelog

VersionChange
0.0.6Each Novis\Pdf\Document method that returns a new document now starts with with, for example withHeader() and withPassword(). created() is now withCreationTime().

Examples

An invoice with a carried forward total

<?nvs
// Shows a long invoice whose footer has the total so far on each page.
use Novis\Pdf\Document;
use Novis\Pdf\Page;
// `data-mark="amount:2.40"` gives each row the value `2.40` under the name `amount`.
string $rows = '';
for (int $n = 1; $n <= 60; $n++) {
$rows .= "<tr data-mark=\"amount:2.40\"><td>Coffee {$n}</td><td>2.40 EUR</td></tr>";
}
var $html = "<h1>Invoice 1042</h1><table><tbody>{$rows}</tbody></table>";
// `marksUpTo` returns the amounts on this page and on every page before it.
// The last page shows the total. Every other page shows the amount carried forward.
var $footer = fn(Page $page): Core\Html\Markup => {
decimal $total = 0.0;
foreach ($page->marksUpTo('amount') as string $amount) {
$total = $total + (($amount as ?decimal) ?? 0.0 as decimal);
}
string $label = $page->isLast() ? 'Total' : 'Carried forward';
echo 'page ', $page->number, ': ', $label, ' ', $total, " EUR\n";
return html`<p>{$label}: {$total} EUR</p>`;
};
Document::fromHtml($html)->withFooter($footer)->render();
Output
page 1: Carried forward 93.60 EUR
page 2: Total 144.00 EUR

The first page is different

<?nvs
// Shows a letter with the bank details on page 1 and a page number on the other pages.
use Novis\Pdf\Document;
use Novis\Pdf\Page;
use Novis\Pdf\Reader;
string $html = '<h1>Dear customer,</h1>'
. Core\Str::repeat('<p>Thank you for your order. Your parcel leaves the shop today.</p>', 70);
var $footer = fn(Page $page): Core\Html\Markup => $page->isFirst()
? html`<p>Shop, IBAN DE02 1203 0000 0000 2020 51</p>`
: html`<p>Page {$page->number} of {$page->count}</p>`;
// This prints the last line of each page, which is its footer.
var $reader = Reader::open(Document::fromHtml($html)->withFooter($footer)->render()->bytes);
for (uint $n = 1; $n <= $reader->pageCount; $n++) {
array<tainted string> $lines = Core\Str::split($reader->text($n), "\n");
echo 'page ', $n, ': ', $lines[Core\Arr::count($lines) - 1], "\n";
}
Output
page 1: Shop, IBAN DE02 1203 0000 0000 2020 51
page 2: Page 2 of 3
page 3: Page 3 of 3

A footer too tall for its margin

<?nvs
// Shows how to find a footer that does not fit in the bottom margin.
use Novis\Pdf\Document;
use Novis\Pdf\Page;
// The footer has four lines, and the margin is 1cm high.
var $footer = fn(Page $page): Core\Html\Markup =>
html`<p style="margin: 0">Shop<br>1 Example Street<br>Example City<br>example.com</p>`;
var $rendered = Document::fromHtml('<h1>Invoice 1042</h1>')->withFooter($footer, { height: '1cm' })->render();
// The render cuts the footer off at the margin and lists it in `overflows`.
// This prints the page and the area of each one.
foreach ($rendered->overflows as var $overflow) {
echo 'page ', $overflow->page, ': the ', $overflow->area, " is too tall\n";
}
// With a 3cm margin, the footer fits. This prints `0`.
echo Core\Arr::count(Document::fromHtml('<h1>Invoice 1042</h1>')->withFooter($footer, { height: '3cm' })->render()->overflows), "\n";
Output
page 1: the footer is too tall
0