Quoin
Guide
Laravel
Math
Reference
Examples
Gallery
Changelog
GitHub
Guide
Laravel
Math
Reference
Examples
Gallery
Changelog
GitHub
  • Start

    • Introduction
    • Installation
    • Your first document
    • Versions and compatibility
  • Building documents

    • Layout
    • Text
    • Tables
    • Pages and pagination
    • Images, SVG, links and QR codes
    • Decorators
  • Look and output

    • Styles and themes
    • Fonts
    • Rich text
    • Equations
    • HTML export
    • Fonts in HTML
    • Word export
    • Passwords, signatures and merging

HTML export

Any document can be exported as HTML, with the same theme, fonts and named styles: for a browser preview, an email body, or a document shown inside another page.

Laravel
$invoice = Quoin::view('pdf.invoice', compact('invoice'));

return $invoice->pdf()->download('invoice.pdf');   // PDF
return $invoice->html();                           // a full HTML page
PHP
use Quoin\{Document, Page, Text};
use Quoin\Export\HtmlOptions;

$document = Document::make(Page::a4()->body(Text::make('Hello')));

$page     = $document->toHtml(new HtmlOptions(fullPage: true))->bytes();
$fragment = $document->toHtml()->bytes();   // one .quoin element, no <html>

In Laravel, ->html()->page() and ->html()->fragment() both return a PendingOutput, with the same toString(), save(), download() and inline() methods as a PDF. Returned from a route, ->html() gives the full page; echoed in a view, it gives the fragment.

What's different in HTML

Browsers don't have pages, so a few things are approximated: fraction column widths, the page itself, and headers and footers, which appear once per page section. Each approximation is reported as a warning.

  • Even-page headers and footers aren't shown. HTML has one header and one footer for the whole run.
  • A first-page footer is shown only when the page has no normal footer. If it has both, the normal footer is shown.
  • Backgrounds and overlays cover the whole page section, which is at least one page tall and grows to fit its content. They're drawn once, not on every printed page, and overlay content is placed against that taller box, so it lands about where it does in the PDF. Both are reported as approximated.

A few things are dropped, and strict mode fails an HTML export on them: PDF page templates and embedded PDF pages. Code highlighting that can't be done is dropped in every format, as Code blocks explains. Without strict mode these are reported as warnings.

A table of contents becomes a <nav class="quoin-toc"> with nested ordered lists of links to the headings. Headings that are entries get an id (a slug of their text, with -2 added on a clash). It has no page numbers or leaders, and each table of contents is reported once as approximated.

To pick the HTML element a view exports as, call htmlTag() (div, section, aside, blockquote, header, footer or figure). PDF ignores it.

The HTML uses meaningful tags where it can: sections become <section>, callouts <aside>, blockquotes <blockquote>, lists <ol> or <ul>, and tables <table>. A row or cell's minimum height is written as its height, which a browser treats as a minimum for table rows and cells (it ignores min-height there).

Columns

A column flow exports as an element with CSS columns (column-count, column-gap, column-rule), and a ColumnBreak as a column break. The browser fills the columns itself, so they won't match the PDF line for line. A child that starts a new column can show an extra gap above it, since CSS can't know where a column starts. Unequal column widths and unequal gaps are approximated as equal columns and equal gaps, reported as approximated.

Fonts

A PDF carries its fonts inside the file; a browser has to load them. By default the HTML only names the fonts and the browser uses whatever it has. To embed your fonts, link to files you host, or load them from Google Fonts, see Fonts in HTML.

Equations

Equations are written as MathML Core: <math display="block"> for a block equation and <math display="inline"> inside a line. The browser lays them out, so spacing can differ a little from the PDF. What font the browser uses depends on the font mode. With the default, none, the stylesheet still names "Quoin Math" first but defines no face for it, so the browser falls back to its own math font. embed embeds STIX Two Math (or your math font) in the page, link links to it, and sources embeds it as a data: URI, since no outside source serves it. A block equation is placed with margins.

Inline SVGs

By default an SVG is written as an <img> with a data: URI. An SVG shown that way can't use the page's fonts, so its text is set in the browser's own, and the export reports SVG text in the browser's own fonts (set inlineSvgs to use the page's fonts).

Set inlineSvgs to write each SVG into the page as <svg> markup instead:

Laravel
// config/quoin.php
'html' => [
    'inline_svgs' => true,
],
PHP
use Quoin\Export\HtmlOptions;

$options = new HtmlOptions(inlineSvgs: true);
// or: (new HtmlOptions())->withInlineSvgs(true)

With it on:

  • SVG text uses the font the PDF uses, whether the family comes from font-family (an attribute, a style attribute or a <style> rule, in any letter case) or from the font shorthand, whose family part is rewritten the same way. A family you haven't registered is replaced by the document's family, as in the PDF. A family used only inside an SVG gets an @font-face rule in the font modes that write them.
  • Every id is prefixed, with its references rewritten, and a <style> element is scoped to its own SVG, so several SVGs in one export can't clash with each other or with the page. The prefix is built from a short hash of the SVG's markup and its position in the export, so SVGs with different markup keep different prefixes even when you put fragments from separate exports on one page. Two fragments that each start with the same markup in the same position share a prefix; vary the markup or export them together.
  • The SVG shrinks to its container, like an image does, and keeps its aspect ratio. Setting only a width or only a height on the node scales the other side from the viewBox. @keyframes are kept with their names prefixed; @supports and @import rules are removed.
  • An SVG with no xmlns (such as d3's outerHTML) is written as SVG, and a root content attribute (draw.io's copy of the whole diagram) is dropped.
  • An SVG with alt text gets role="img" and aria-label. A decorative one gets aria-hidden="true".
  • Editor data is removed: comments, <metadata>, elements that aren't SVG (such as Inkscape's sodipodi:namedview and inkscape:* elements) with everything inside them, and attributes from other namespaces (such as inkscape:label). Drawing elements are kept as they are.
  • An SVG with a <foreignObject> stays an <img>, since the markup inside it isn't SVG. The export reports SVG kept as an image (<foreignObject> can't be written inline).

Caution

Inline SVG runs in the page. Turn inlineSvgs on only when you trust where every SVG comes from. Scripts and event handlers are removed, but not links (including javascript: links) or animations that change links, and an inlined SVG can make the browser fetch external resources (an <image> href, feImage, url(...) in a style). Leave it off for SVGs that users upload.

Fitting a narrow container

The HTML keeps the page's width and margins. A fragment in a container narrower than the page, such as a side panel or a phone screen, reflows its content into what's left after the margins, so text and tables get squeezed. To show it as a scaled-down print preview instead, zoom each page to the container's width:

<div class="preview">{!! $invoice->html() !!}</div>

<script>
  document.querySelectorAll('.preview').forEach((box) => {
    const fit = () => box.querySelectorAll('.pc-page').forEach((page) => {
      page.style.zoom = '';
      const pageWidth = parseFloat(getComputedStyle(page).maxWidth); // the page width, in px
      page.style.zoom = Math.min(1, box.clientWidth / pageWidth);
    });
    fit();
    new ResizeObserver(fit).observe(box);
  });
</script>

Each .pc-page has the page's width as its max-width, so the zoom is the container's width over that, and never more than 1. The page keeps the same line breaks and table layout at any width, and because zoom scales layout as well as drawing, the container's height shrinks with it.

Image URLs

Images are embedded as data: URIs by default. To load them from your own URLs instead, pass an imageUrl closure in PHP, or set html.image_url to a resolver class in Laravel. Return null to embed an image. SVGs are always embedded, as data: URIs or inline markup, so imageUrl doesn't apply to them.

Edit this page on GitHub
Last Updated: 11/17/26, 2:47 PM
Prev
Equations
Next
Fonts in HTML