Skip to content

Novis\Pdf\Page::marks

$page->marks(string $name): array<string>$page->marks(
    string $name,
): array<string>

Returns the values of the marks with one name on this page.

A mark is an attribute on any HTML element: data-mark="amount:12.50". The part before the colon is the name, and the part after it is the value. One element can have several marks, separated by ;, such as data-mark="amount:12.50;tax:2.00". marks("amount") returns the values of every amount mark on this page, in the order of the document.

Good to know: the values are strings. To add up amounts, convert each one with as ?decimal. To get the values of this page and every page before it, use marksUpTo.

The examples below count the rows on each page, read two marks from one element, and add up the amounts of each page.

Parameters

  • $namestring

Return value

Returns array<string>.

Examples

Rows on each page

<?nvs
// Shows how a footer counts the order rows on its page.
use Novis\Pdf\Document;
use Novis\Pdf\Page;
// Each row has a mark with the name `order` and the order number as its value.
string $rows = '';
for (int $n = 1001; $n <= 1060; $n++) {
$rows .= "<tr data-mark=\"order:{$n}\"><td>Order {$n}</td><td>Shipped</td></tr>";
}
var $html = "<h1>Orders</h1><table><tbody>{$rows}</tbody></table>";
var $footer = fn(Page $page): Core\Html\Markup => {
array<string> $orders = $page->marks('order');
uint $count = Core\Arr::count($orders);
echo 'page ', $page->number, ': ', $count, ' orders, the first is ', $orders[0], "\n";
return html`<p>{$count} orders on this page</p>`;
};
Document::fromHtml($html)->withFooter($footer)->render();
Output
page 1: 39 orders, the first is 1001
page 2: 21 orders, the first is 1040

Two marks on one element

<?nvs
// Shows one element with two marks, and how a footer reads each name.
use Novis\Pdf\Document;
use Novis\Pdf\Page;
// `;` separates the marks. Each row has an `amount` mark and a `tax` mark.
var $html = '<h1>Invoice 1042</h1>'
. "<p data-mark=\"amount:12.50;tax:2.38\">Tea</p>"
. "<p data-mark=\"amount:4.00;tax:0.76\">Cups</p>";
// This prints the values of each name on the page, as strings.
var $footer = fn(Page $page): Core\Html\Markup => {
echo 'amount: ', Core\Str::join($page->marks('amount'), ', '), "\n";
echo 'tax: ', Core\Str::join($page->marks('tax'), ', '), "\n";
echo 'discount: ', Core\Arr::count($page->marks('discount')), " values\n";
return html`<p>Invoice 1042</p>`;
};
Document::fromHtml($html)->withFooter($footer)->render();
Output
amount: 12.50, 4.00
tax: 2.38, 0.76
discount: 0 values

A subtotal for each page

<?nvs
// Shows an invoice footer with the subtotal of the rows on its page.
use Novis\Pdf\Document;
use Novis\Pdf\Page;
string $rows = '';
for (int $n = 1; $n <= 40; $n++) {
$rows .= "<tr data-mark=\"amount:2.25\"><td>Coffee {$n}</td><td>2.25 EUR</td></tr>";
}
var $html = "<h1>Invoice 1042</h1><table><tbody>{$rows}</tbody></table>";
// The values are strings, so `as ?decimal` converts each one to an exact number.
var $footer = fn(Page $page): Core\Html\Markup => {
decimal $subtotal = 0.0;
foreach ($page->marks('amount') as string $amount) {
$subtotal = $subtotal + (($amount as ?decimal) ?? 0.0 as decimal);
}
echo 'page ', $page->number, ': subtotal ', $subtotal, " EUR\n";
return html`<p>Subtotal of this page: {$subtotal} EUR</p>`;
};
Document::fromHtml($html)->withFooter($footer)->render();
Output
page 1: subtotal 85.50 EUR
page 2: subtotal 4.50 EUR