Skip to content

Novis\Pdf\Document::withAsset

$document->withAsset(string $name, bytes $data): Document$document->withAsset(
    string $name,
    bytes $data,
): Document

Adds a file that the HTML of a document names, such as an image, a stylesheet, a font or a PDF file.

A render reads no file from the disk and nothing from the network. It reads only its assets and data: URIs. withAsset("logo.svg", $bytes) gives the bytes for every place the HTML, the CSS or an SVG image names logo.svg. The names must match exactly. Any other name, a file: address and an http: address make render throw a LogicError that names it.

A program that needs an image from another server downloads it first, and then adds it as an asset.

withAsset returns a new Document and does not change the one you called it on.

Good to know: a PDF file can be an asset too. <img src="terms.pdf#page=2"> draws page 2 of it, and @page { background-image: url(letterhead.pdf) } draws it under every page.

The examples below add a logo and a stylesheet, show the error for a file that was not added, and print a contract on a letterhead.

Parameters

  • $namestring
  • $databytes

Return value

Returns Document.

Changelog

VersionChange
0.0.6Each Novis\Pdf\Document method that returns a new document now starts with with, for example withHeader() and withPassword(). created() is now withCreationTime().

Examples

A logo and a stylesheet

<?nvs
// Shows how to add the logo and the stylesheet that an invoice names.
use Novis\Pdf\Document;
use Novis\Pdf\Reader;
string $logo = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"120\" height=\"40\">"
. "<rect width=\"120\" height=\"40\" fill=\"#1d4ed8\"/></svg>";
string $style = 'body { font-family: serif } h1 { color: #1d4ed8 }';
string $html = "<link rel=\"stylesheet\" href=\"invoice.css\">"
. "<img src=\"logo.svg\" alt=\"Shop\"><h1>Invoice 1042</h1><p>Total: 5.70 EUR</p>";
// Each name in the HTML must match the name of an asset exactly.
var $rendered = Document::fromHtml($html)
->withAsset('logo.svg', $logo as bytes)
->withAsset('invoice.css', $style as bytes)
->render();
// This prints the page count and the text of the invoice.
echo $rendered->pageCount, " page\n";
echo Reader::open($rendered->bytes)->text(1), "\n";
Output
1 page
Invoice 1042
Total: 5.70 EUR

A file that was not added

<?nvs
// Shows what happens when the HTML names a file that is not an asset.
use Novis\Pdf\Document;
string $html = "<img src=\"logo.png\" alt=\"Shop\"><h1>Invoice 1042</h1>";
// The render reads no file from the disk and nothing from the network.
// `logo.png` is not an asset, so `render` throws a `LogicError` that names it.
try {
Document::fromHtml($html)->render();
} catch (LogicError $e) {
echo $e->message, "\n";
}
// An address on another server throws the same error.
try {
Document::fromHtml("<img src=\"https://example.com/logo.png\" alt=\"Shop\">")->render();
} catch (LogicError $e) {
echo $e->message, "\n";
}
Output
`logo.png` is not in the asset map: a render reads only its assets and `data:` URIs
`https://example.com/logo.png` is not in the asset map: a render reads only its assets and `data:` URIs

A letterhead under every page

<?nvs
// Shows a contract printed on a letterhead, which is a PDF file added as an asset.
use Novis\Pdf\Document;
use Novis\Pdf\Reader;
// The letterhead. A real program reads this file from its storage.
bytes $letterhead = Document::fromHtml("<p style=\"text-align: right\">Shop, 1 Example Street</p>")
->render()->bytes;
// `background-image` in `@page` draws page 1 of the letterhead under every page.
string $html = '<style>@page { background-image: url(letterhead.pdf) } h2 { break-before: page }</style>'
. '<h1>Contract</h1><p>Shop rents a stand to Blog for one year.</p>'
. '<h2>Terms</h2><p>The rent is due on the first day of each month.</p>';
var $contract = Document::fromHtml($html)->withAsset('letterhead.pdf', $letterhead)->render();
// This prints the text of each page. The address of the letterhead is on both pages.
var $reader = Reader::open($contract->bytes);
for (uint $page = 1; $page <= $reader->pageCount; $page++) {
echo 'page ', $page, ': ', Core\Str::replace($reader->text($page), "\n", ' / '), "\n";
}
Output
page 1: Shop, 1 Example Street / Contract / Shop rents a stand to Blog for one year.
page 2: Shop, 1 Example Street / Terms / The rent is due on the first day of each month.