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.
<?nvsuse 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";a PDF file1 pageMembers
10 members.
| Member | Signature |
|---|---|
fromHtml | fromHtml(Core\Html\Markup|string $html): Document |
withAsset | withAsset(string $name, bytes $data): Document |
withStandard | withStandard(Standard $standard): Document |
withInvoiceXml | withInvoiceXml(string $xml): Document |
withPassword | withPassword(string $password): Document |
withCreationTime | withCreationTime(Core\Time\Instant $at): Document |
withHeader | withHeader(callable(Page): Core\Html\Markup $fn, ?{ height?: string } $options = null): Document |
withFooter | withFooter(callable(Page): Core\Html\Markup $fn, ?{ height?: string } $options = null): Document |
withSlots | withSlots(callable(Page): array<Core\Html\Markup> $fn): Document |
render | render(): 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.PdfA2bandPdfA3bare PDF/A, andPdfUa1is PDF/UA. OnlyPdfA3bcan contain an electronic invoice. An electronic invoice is an invoice as an XML file that a program can read.Document::withInvoiceXmlattaches 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.
