Skip to content

Novis\Pdf\Document::render

$document->render(): Rendered

Makes the PDF file of a document, and returns it with its page count.

render returns a Rendered. bytes is the PDF file, which you can send, save or attach to an email. pageCount is the number of pages.

The render also lists what it could not draw. dropped lists the CSS it does not support. It also lists each name in font-family that has no font, such as a typo. A font must come from @font-face or be a built-in font. missingGlyphs lists the text that no font has. overflows lists each header, footer, slot or @page margin box that did not fit. A render does not throw an error for these, so check the lists in a test.

A render reads no file, no network and no clock. The same document always gives the same bytes. A render has the memory and time limits of its request. Tables, flex containers and grid containers may be nested 32 levels deep. A deeper document throws a LogicError.

The examples below make a shipping label with a barcode, list what a render could not draw, and put a contract and its terms into one file.

Return value

Returns Rendered.

Changelog

VersionChange
0.0.6Rendered->dropped lists each name in font-family that has no font, such as a typo. The property is font-family, and the value is the name. So a test that checks that dropped is empty also finds a misspelt font.

Examples

A shipping label with a barcode

<?nvs
// Shows a shipping label with a barcode, made as a PDF file for a label printer.
use Novis\Image\Barcode;
use Novis\Pdf\Document;
use Novis\Pdf\Reader;
// `Barcode::svg` returns an SVG image as a string, so it can go straight into the HTML.
string $barcode = Barcode::svg('SHIP-2026-0042', Barcode::Symbology::Code128, { height: 60 });
// `@page` sets the size of the label: 100mm by 150mm.
string $html = '<style>@page { size: 100mm 150mm; margin: 5mm }</style>'
. '<h2>Ada Lovelace</h2><p>1 Example Street<br>12345 Example City</p>'
. "<div>{$barcode}</div><p>Parcel SHIP-2026-0042</p>";
// `render` returns the file as `bytes`. A program sends them to the printer or saves them.
var $label = Document::fromHtml($html)->render();
echo $label->pageCount, ' page, ', Core\Arr::count($label->dropped), " CSS rules dropped\n";
echo Reader::open($label->bytes)->text(1), "\n";
Output
1 page, 0 CSS rules dropped
Ada Lovelace
1 Example Street
12345 Example City
Parcel SHIP-2026-0042

What the render could not draw

<?nvs
// Shows how to check what a render could not draw: CSS it does not support, and text without a font.
use Novis\Pdf\Document;
// `box-shadow` is not supported. `Arail` is a typo, so no font has that name.
// No font has the character `π“€€`.
string $html = '<style>h1 { box-shadow: 2px 2px gray } p { font-family: Arail, sans-serif }</style>'
. '<h1>Order 1042</h1><p>Gift note: π“€€</p>';
var $rendered = Document::fromHtml($html)->render();
// `dropped` lists each CSS declaration that the render did not use, with its selector and line.
// It also lists `Arail`, because no font has that name. The text uses `sans-serif` instead.
foreach ($rendered->dropped as var $css) {
echo 'dropped ', $css->property, ': ', $css->value, ' in ', $css->selector, ' at line ', $css->line, "\n";
}
// `missingGlyphs` lists the text that no font has. The render draws an empty box for it.
foreach ($rendered->missingGlyphs as var $missing) {
echo 'missing `', $missing->text, '` on page ', $missing->page, "\n";
}
Output
dropped box-shadow: 2px 2px gray in h1 at line 1
dropped font-family: Arail in p at line 1
missing `π“€€` on page 1

A contract with its terms appended

<?nvs
// Shows a contract and its terms, rendered from two documents and put into one PDF file.
use Novis\Pdf\Document;
use Novis\Pdf\Pages;
use Novis\Pdf\Reader;
// The terms are the same for every contract, so the program renders them once.
bytes $terms = Document::fromHtml('<h1>Terms</h1><p>The rent is due on the first day of each month.</p>')
->render()->bytes;
// Each contract is a new render with the name of the customer.
string $customer = 'Blog';
bytes $contract = Document::fromHtml(html`<h1>Contract</h1><p>Shop rents a stand to {$customer} for one year.</p>`)
->render()->bytes;
// `Pages::merge` puts every page of both files into one file, in order.
tainted bytes $file = Pages::merge([Pages::of($contract), Pages::of($terms)]);
// This prints the first line of each page.
var $reader = Reader::open($file);
for (uint $n = 1; $n <= $reader->pageCount; $n++) {
echo 'page ', $n, ': ', Core\Str::before($reader->text($n), "\n") ?? '', "\n";
}
Output
page 1: Contract
page 2: Terms