Skip to content

Novis\Pdf\Document

An HTML document to render as a PDF file, with the images, stylesheets, fonts and PDF files it uses. Each method returns a new Document and leaves this one as it was.

Novis\Pdf\Document is an HTML document that render turns into a PDF file. Each method returns a new Document and does not change the old one. The render reads only the files you add with withAsset and data: URIs, so a name that is not an asset throws a LogicError. render returns a Rendered with the file, its page count, and the CSS it could not draw.

<?nvs
use Novis\Pdf\Document;
use Novis\Pdf\Page;
// The logo is a file that the HTML names. `withAsset` adds it to the document.
bytes $logo = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"40\" height=\"20\"><rect width=\"40\" height=\"20\"/></svg>" as bytes;
string $html = "<html lang=\"en\"><head><title>Invoice 1042</title></head><body>"
. "<img src=\"logo.svg\" alt=\"Shop\"><h1>Invoice 1042</h1><p>Tea: 2.50 EUR</p></body></html>";
// The footer is a function. The render calls it once for each page.
var $rendered = Document::fromHtml($html)
->withAsset('logo.svg', $logo)
->withFooter(fn(Page $page): Core\Html\Markup => html`<p>Page {$page->number} of {$page->count}</p>`)
->render();
echo Core\Bytes::startsWith($rendered->bytes, '%PDF-' as bytes) ? 'a PDF file' : 'not a PDF file', "\n";
echo $rendered->pageCount, " page\n";
Output
a PDF file
1 page

Members

10 members.

MemberSignature
fromHtmlfromHtml(Core\Html\Markup|string $html): Document
withAssetwithAsset(string $name, bytes $data): Document
withStandardwithStandard(Standard $standard): Document
withInvoiceXmlwithInvoiceXml(string $xml): Document
withPasswordwithPassword(string $password): Document
withCreationTimewithCreationTime(Core\Time\Instant $at): Document
withHeaderwithHeader(callable(Page): Core\Html\Markup $fn, ?{ height?: string } $options = null): Document
withFooterwithFooter(callable(Page): Core\Html\Markup $fn, ?{ height?: string } $options = null): Document
withSlotswithSlots(callable(Page): array<Core\Html\Markup> $fn): Document
renderrender(): Rendered

Enums

  • Standard { PdfA2b, PdfA3b, PdfUa1 }The set of rules that a PDF file follows. PDF/A is for files kept in an archive for many years. PDF/UA is for files that a screen reader (a program that reads text aloud) can read. PdfA2b and PdfA3b are PDF/A, and PdfUa1 is PDF/UA. Only PdfA3b can contain an electronic invoice. An electronic invoice is an invoice as an XML file that a program can read. Document::withInvoiceXml attaches this XML file to the document.

Example apps

These complete apps use this class. You can download and run each one.

  • PDF documents: A small web app that makes three PDF files. They are an invoice, an encrypted report and event tickets. See each file, then run the app and its tests.