Novis\Pdf\Document::fromHtml
Novis\Pdf\Document::fromHtml(Core\Html\Markup|string $html): DocumentNovis\Pdf\Document::fromHtml( Core\Html\Markup|string $html, ): Document
Starts a PDF document from HTML.
The HTML can be a string or the result of an html template. Use a template when the page has text from a user, such as a customer name. The template escapes that text, so it shows as text and never as HTML. A document has only the HTML it was given. An image, a stylesheet or a font that the HTML names by a file name must be added with withAsset.
fromHtml returns a new Document. Each method after it, such as withHeader or withPassword, returns a new Document too. Call render at the end to make the PDF file.
Good to know: the PDF follows the CSS for printed pages. @page sets the paper size and the margins, and break-before: page starts a new page.
The examples below make a receipt from a string, build a page with an html template, and make a long report with a table of contents.
Parameters
$htmlCore\Html\Markup|string
Return value
Returns Document.
Examples
A receipt from a string
<?nvs// Shows how to make a PDF receipt from a string of HTML.use Novis\Pdf\Document;use Novis\Pdf\Reader;
string $html = '<h1>Receipt 1042</h1><p>Shop, 1 Example Street</p>' . '<table><tr><td>Tea</td><td>2.50 EUR</td></tr><tr><td>Cake</td><td>3.20 EUR</td></tr></table>';
// `fromHtml` starts the document, and `render` makes the PDF file.var $rendered = Document::fromHtml($html)->render();echo $rendered->pageCount, " page\n";
// `Reader::open` reads the file back. This prints the text of page 1.echo Reader::open($rendered->bytes)->text(1), "\n";1 pageReceipt 1042Shop, 1 Example StreetTea 2.50 EURCake 3.20 EURCustomer text in an html template
<?nvs// Shows how to build the HTML with an `html` template, so text from a customer is safe.use Novis\Pdf\Document;use Novis\Pdf\Reader;
string $customer = 'Ada <script>alert(1)</script>';
// The `html` template escapes `$customer`. The tag is text in the PDF, not HTML.var $page = html`<h1>Order for {$customer}</h1><p>Thank you for your order.</p>`;var $pdf = Document::fromHtml($page)->render()->bytes;
// This prints the heading with the `<script>` tag as plain characters.echo Core\Str::before(Reader::open($pdf)->text(1), "\n") ?? '', "\n";Order for Ada <script>alert(1)</script>A report with a table of contents
<?nvs// Shows a long report whose table of contents gives the page of each chapter.use Novis\Pdf\Document;use Novis\Pdf\Reader;
string $contents = '';string $chapters = '';for (int $n = 1; $n <= 3; $n++) { $contents .= "<li><a href=\"#chapter{$n}\">Chapter {$n}</a></li>"; $chapters .= "<h2 id=\"chapter{$n}\">Chapter {$n}</h2>" . Core\Str::repeat('<p>Sales rose in every region, and the shop opened two new stores.</p>', 30);}
// `target-counter` writes the page number of the element that a link points to.// `leader('.')` fills the space before the number with dots.string $style = '<style>h2 { break-before: page } ' . "a::after { content: leader('.') target-counter(attr(href), page) }</style>";var $rendered = Document::fromHtml($style . "<h1>Annual report</h1><ul>{$contents}</ul>{$chapters}")->render();echo $rendered->pageCount, " pages\n";
// This prints each chapter in the table of contents, with its page number.foreach (Core\Str::split(Reader::open($rendered->bytes)->text(1), "\n") as tainted string $line) { if (Core\Str::startsWith($line, 'Chapter')) { echo $line, "\n"; }}7 pagesChapter 1 2Chapter 2 4Chapter 3 6