Novis\Pdf\Rendered::constructor
$rendered->constructor(
bytes $bytes,
uint $pageCount,
array<Page> $pages,
array<Dropped> $dropped,
array<MissingGlyph> $missingGlyphs,
array<Overflow> $overflows,
): voidThe result of Document::render: the PDF file, its pages, and the lists of what the render could not draw.
bytes is the PDF file. pageCount is the number of pages. pages has one Page for each page, with the marks and string-set values of that page. dropped lists each CSS declaration the render does not support, with its selector, line and column. missingGlyphs lists text that no font can draw, with its page. overflows lists header, footer or slot content that is too tall for its area. A render that worked well has three empty lists.
Good to know: a test can check that dropped is empty. The test then fails when somebody adds CSS that the render ignores. You can also write new Rendered yourself, for example in a test. With pages, you can find the page of each mark after the render. To print a page number in the same document, such as in a table of contents, use the CSS function target-counter() instead. It needs only one render.
The examples below read the file and the page of each order, show the three lists, and make a result by hand for a test.
Parameters
$bytesbytesthe PDF file$pageCountuintthe number of pages in the PDF file$pagesarray<Page>onePagefor each page of the PDF file, with itsstring-setvalues and its marks$droppedarray<Dropped>the CSS the render does not support, with the line and column of each declaration$missingGlyphsarray<MissingGlyph>the text that no font has, with the fonts tried and the page$overflowsarray<Overflow>the HTML that is too tall for its cell, with its page and area
Return value
Returns void.
Changelog
| Version | Change |
|---|---|
0.0.6 | Rendered->pages has one Page for each page of a rendered PDF file. You can read the page of each data-mark after one render. new Rendered takes the pages as its third value. |
Examples
The file and its pages
<?nvs// Shows the PDF file, the page count and the pages that a render returns.use Novis\Pdf\Document;use Novis\Pdf\Page;
var $html = '<style>p { break-after: page }</style>' . '<p data-mark="order:1042">Order 1042</p><p data-mark="order:1043">Order 1043</p>';var $rendered = Document::fromHtml($html)->render();
// `bytes` is the PDF file. Every PDF file starts with `%PDF-`.echo Core\Bytes::slice($rendered->bytes, 0, 5) as string, "\n";echo $rendered->pageCount, " pages\n";
// `pages` has one `Page` for each page. This prints the page of each order.foreach ($rendered->pages as Page $page) { foreach ($page->marks('order') as string $order) { echo "order {$order} is on page {$page->number}\n"; }}
// The three lists are empty, because the render drew everything.echo Core\Arr::count($rendered->dropped), ' dropped, ';echo Core\Arr::count($rendered->missingGlyphs), ' missing glyphs, ';echo Core\Arr::count($rendered->overflows), " overflows\n";%PDF-2 pagesorder 1042 is on page 1order 1043 is on page 20 dropped, 0 missing glyphs, 0 overflowsWhat the render could not draw
<?nvs// Shows the CSS a render ignores, text no font can draw, and a footer too tall for its margin.use Novis\Pdf\Document;use Novis\Pdf\Page;
// `text-shadow` is not supported. No default font has the robot emoji.var $html = "<style>h1 { text-shadow: 1px 1px gray }</style><h1>Shop</h1><p>Our helper \u{1F916}</p>";
// The footer text fills many lines, but the bottom margin is only 1 cm high.string $terms = Core\Str::repeat('Shop sells to customers in the whole country. ', 20);var $footer = fn(Page $page): Core\Html\Markup => html`<p>{$terms}</p>`;
var $rendered = Document::fromHtml($html)->withFooter($footer, { height: '1cm' })->render();foreach ($rendered->dropped as var $css) { echo 'dropped: ', $css->property, ' in ', $css->selector, ', line ', $css->line, "\n";}foreach ($rendered->missingGlyphs as var $glyph) { echo 'missing glyph on page ', $glyph->page, "\n";}foreach ($rendered->overflows as var $overflow) { echo 'overflow: the ', $overflow->area, ' of page ', $overflow->page, "\n";}dropped: text-shadow in h1, line 1missing glyph on page 1overflow: the footer of page 1A result for a test
<?nvs// Shows how to make a render result by hand, to test code that uses one.use Novis\Pdf\Rendered;
class Archive{ // This method writes the log line for a render that a shop saves. public static function logLine(Rendered $result): string { uint $problems = Core\Arr::count($result->dropped) + Core\Arr::count($result->missingGlyphs); return "saved {$result->pageCount} pages, {$problems} problems"; }}
// The values are the file, the page count, the pages and the three lists.// The list of pages is empty here. The list of dropped CSS has one declaration.var $result = new Rendered( '%PDF-1.7' as bytes, 3, [], [{ property: 'text-shadow', value: '1px 1px gray', selector: 'h1', line: 1, column: 12 }], [], [],);
// This prints `saved 3 pages, 1 problems`.echo Archive::logLine($result), "\n";saved 3 pages, 1 problems