Novis\Pdf\Page::marksUpTo
Returns the values of the marks with one name on this page and on every page before it.
A mark is an attribute on any HTML element, such as data-mark="amount:12.50". marksUpTo("amount") returns the values of every amount mark from page 1 to this page, in the order of the document. Add them up to get a running total, which a long invoice shows at the bottom of each page.
The values are strings. Convert each one with as ?decimal before you add it, so the total is exact.
The examples below carry a running total in the footer of an invoice, show the total of the earlier pages in the header, and count the items so far.
Parameters
$namestring
Return value
Returns array<string>.
Examples
A running total in the footer
<?nvs// Shows an invoice footer with the running total of every page so far.use Novis\Pdf\Document;use Novis\Pdf\Page;
// Each row has a mark with the name `amount` and the price of the row as its value.string $rows = '';for (int $n = 1; $n <= 70; $n++) { $rows .= "<tr data-mark=\"amount:1.50\"><td>Tea {$n}</td><td>1.50 EUR</td></tr>";}var $html = "<h1>Invoice 1042</h1><table><tbody>{$rows}</tbody></table>";
// `as ?decimal` converts each value to an exact number.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();page 1: Carried forward 58.50 EURpage 2: Total 105.00 EURBrought forward in the header
<?nvs// Shows the total of the earlier pages at the top of each page of an invoice.use Novis\Pdf\Document;use Novis\Pdf\Page;
string $rows = '';for (int $n = 1; $n <= 70; $n++) { $rows .= "<tr data-mark=\"amount:2.00\"><td>Cake {$n}</td><td>2.00 EUR</td></tr>";}var $html = "<h1>Invoice 1042</h1><table><tbody>{$rows}</tbody></table>";
// This function adds up a list of amounts.var $sum = fn(array<string> $amounts): decimal => { decimal $total = 0.0; foreach ($amounts as string $amount) { $total = $total + (($amount as ?decimal) ?? 0.0 as decimal); } return $total;};
// The earlier pages are every page so far, without the rows of this page.var $header = fn(Page $page): Core\Html\Markup => { if ($page->isFirst()) { return html``; } decimal $before = $sum($page->marksUpTo('amount')) - $sum($page->marks('amount')); echo 'page ', $page->number, ': brought forward ', $before, " EUR\n"; return html`<p>Brought forward: {$before} EUR</p>`;};
Document::fromHtml($html)->withHeader($header)->render();page 2: brought forward 78.00 EURThe items so far
<?nvs// Shows a packing list that says which items each page lists, such as "items 1 to 39 of 50".use Novis\Pdf\Document;use Novis\Pdf\Page;
string $rows = '';for (int $n = 1; $n <= 50; $n++) { $rows .= "<tr data-mark=\"item:{$n}\"><td>Item {$n}</td><td>Box A</td></tr>";}var $html = "<h1>Packing list</h1><table><tbody>{$rows}</tbody></table>";
// The last value up to this page is the number of the last item on this page.var $footer = fn(Page $page): Core\Html\Markup => { array<string> $soFar = $page->marksUpTo('item'); uint $last = Core\Arr::count($soFar); uint $first = $last - Core\Arr::count($page->marks('item')) + 1; echo 'page ', $page->number, ': items ', $first, ' to ', $last, " of 50\n"; return html`<p>Items {$first} to {$last} of 50</p>`;};
Document::fromHtml($html)->withFooter($footer)->render();page 1: items 1 to 39 of 50page 2: items 40 to 50 of 50