Novis\Pdf\Document::withSlots
Sets a function that fills each <slot name="..."> in a table, once for each page.
A table over several pages repeats its thead and tfoot rows on each page. A cell in those rows can contain a <slot>. render calls your function once for each Page. The function returns an array that maps each slot name to an html template.
$page->marksUpTo("amount") returns the data-mark="amount:..." values up to this page. $page->marks("amount") returns those on this page only.
The HTML of a slot does not use the document's styles, so set its font size with a <style>. A slot never changes the height of its row. HTML that is too tall is cut off and listed in Rendered::overflows.
A table without a width is as wide as its content. Give it one, such as width: 100%, so the slot's column has room.
withSlots returns a new Document and does not change the one you called it on.
The examples below show a running total on each page, add up the hours of each page, and find slot HTML that does not fit.
Parameters
$fncallable(Page): array<Core\Html\Markup>
Return value
Returns Document.
Changelog
| Version | Change |
|---|---|
0.0.6 | Each Novis\Pdf\Document method that returns a new document now starts with with, for example withHeader() and withPassword(). created() is now withCreationTime(). |
Examples
A carried forward total in the table
<?nvs// Shows an invoice table over several pages, with the total so far at the bottom of each page.use Novis\Pdf\Document;use Novis\Pdf\Page;
string $rows = '';for (int $n = 1; $n <= 50; $n++) { $rows .= "<tr data-mark=\"amount:1.50\"><td>Tea {$n}</td><td>1.50</td></tr>";}
// The `tfoot` rows repeat at the bottom of each page that the table is on.// `data-repeat="continued"` shows a row where the table goes on. `data-repeat="last"` shows it on the last page.var $html = '<style>@page { size: 105mm 148mm; margin: 10mm } body { font-size: 10pt }</style>' . "<table style=\"width: 100%\"><thead><tr><th>Item</th><th>EUR</th></tr></thead><tbody>{$rows}</tbody><tfoot>" . "<tr data-repeat=\"continued\"><td>Carried forward</td><td><slot name=\"carried\"></slot></td></tr>" . "<tr data-repeat=\"last\"><td>Total</td><td><slot name=\"total\"></slot></td></tr></tfoot></table>";
// The function returns the HTML for each slot name. Each page shows only one of the two rows.// A fill does not use the styles of the document, so it sets the font size of the table.var $slots = fn(Page $page): array<Core\Html\Markup> => { decimal $total = 0.0; foreach ($page->marksUpTo('amount') as string $amount) { $total = $total + (($amount as ?decimal) ?? 0.0 as decimal); } echo 'page ', $page->number, ' of ', $page->count, ': ', $total, "\n"; return [ 'carried' => html`<style>body { font-size: 10pt }</style>{$total}`, 'total' => html`<style>body { font-size: 10pt }</style><b>{$total}</b>`, ];};
// Every fill fits in its cell, so this prints `0`.var $rendered = Document::fromHtml($html)->withSlots($slots)->render();echo 'fills that do not fit: ', Core\Arr::count($rendered->overflows), "\n";page 1 of 3: 33.00page 2 of 3: 67.50page 3 of 3: 75.00fills that do not fit: 0A subtotal for each page
<?nvs// Shows a list of hours worked, with the hours of each page at the bottom of that page.use Novis\Pdf\Document;use Novis\Pdf\Page;use Novis\Pdf\Reader;
string $rows = '';for (int $day = 1; $day <= 60; $day++) { string $hours = $day % 2 == 0 ? '8' : '6'; $rows .= "<tr data-mark=\"hours:{$hours}\"><td>Day {$day}</td><td>{$hours}</td></tr>";}var $html = '<style>@page { size: 105mm 148mm; margin: 10mm } body { font-size: 10pt }</style>' . "<table style=\"width: 100%\"><thead><tr><th>Day</th><th>Hours</th></tr></thead><tbody>{$rows}</tbody>" . "<tfoot><tr><td>This page</td><td><slot name=\"page\"></slot></td></tr></tfoot></table>";
// `marks` returns the values on this page only.var $slots = fn(Page $page): array<Core\Html\Markup> => { int $sum = 0; foreach ($page->marks('hours') as string $hours) { $sum = $sum + (($hours as ?int) ?? 0); } return ['page' => html`{$sum}`];};
// The last line of each page is the footer row: `This page` and the text of the slot.// This prints that line for each page.var $reader = Reader::open(Document::fromHtml($html)->withSlots($slots)->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";}page 1: This page 154page 2: This page 160page 3: This page 106A fill too tall for its cell
<?nvs// Shows how to find the HTML that does not fit in the cell of its slot.use Novis\Pdf\Document;use Novis\Pdf\Page;
var $html = "<table><tbody><tr><td>Delivery</td><td><slot name=\"note\"></slot></td></tr></tbody></table>";
// The slot keeps the height of its row. Three paragraphs are taller than one row.var $slots = fn(Page $page): array<Core\Html\Markup> => ['note' => html`<p>Ring the bell.</p><p>Leave the parcel at the door.</p><p>Call 0123 456.</p>`];var $rendered = Document::fromHtml($html)->withSlots($slots)->render();
// The render cuts the HTML off at the cell and lists it in `overflows`, with the slot name as its area.foreach ($rendered->overflows as var $overflow) { echo 'page ', $overflow->page, ': the slot `', $overflow->area, "` is too tall\n";}page 1: the slot `note` is too tall