docs

SVG images

An image element can show an SVG. Liuska draws it as vector PDF: paths stay paths, gradients stay gradients, and text is real text in the template's embedded fonts, so you can search and copy it and it stays sharp at any zoom. It is not turned into a bitmap. The PNG preview (--png, /api/preview) draws the same SVG with the built-in rasteriser.

{ "id": "chart", "type": "image", "src": "charts/sales.svg", "x": 40, "y": 120, "width": 258, "height": 146 }

src can be:

Source Example
a file relative to the template "src": "sales.svg" (.svgz works too)
a data URI "data:image/svg+xml;base64,PHN2Zy…" or "data:image/svg+xml;utf8,<svg …>"
a binding to a file name or URI "src": "{{report.chartFile}}"
a binding to inline markup "src": "{{report.chartSvg}}", where the data holds "<svg xmlns=…>…</svg>"

Liuska detects SVG from the content, not from the file extension. The SVG is parsed once and cached like any other image. In a PDF it is written once as a Form XObject, even when it appears on every page (in a header, for example).

Bringing in D3 charts (wkhtmltopdf migration)

Liuska runs no JavaScript. Keep the D3 code and run it on the backend with node and jsdom, then hand the SVG markup to Liuska in the data or as a file:

// npm install d3 jsdom
import { JSDOM } from "jsdom";
import * as d3 from "d3";

const { document } = new JSDOM("<!DOCTYPE html><body></body>").window;
const svg = d3.select(document.body).append("svg")
  .attr("xmlns", "http://www.w3.org/2000/svg")
  .attr("width", 258).attr("height", 146);
// … the existing chart code, with fixed sizes instead of clientWidth …
const markup = document.body.innerHTML;   // -> data.report.chartSvg

Things to watch:

Sizing and fit

The intrinsic size comes from width/height on the root <svg> (px, pt, mm, cm, in; one px = 0.75 pt) or, when those are missing or percentages, from the viewBox. Without either, the size is 300 × 150, as in browsers. An SVG without a viewBox gets one from its width and height, so it scales with the element box.

fit works as it does for bitmaps: contain (default) and cover keep the aspect ratio, and fill stretches (for SVG it overrides preserveAspectRatio to none). Within the box, the SVG's own viewBox and preserveAspectRatio apply, and content outside the viewport is clipped, as with overflow: hidden in a browser. If you leave out the element width/height, the intrinsic size is used.

Fonts

font-family lists are matched against the template's fonts, in order:

  1. A family declared in the template's fonts, a bundled alias (Go, Helvetica, Arial, …) or a family found in the configured font directories is used as is.
  2. monospace maps to the bundled Go Mono.
  3. Generic families (sans-serif, serif, system-ui, …) and unknown names fall back to the image element's own fontFamily (by default the template's default font).

font-weight 600 and above selects bold, and italic/oblique select italic, falling back as the template fonts do. Text uses kerning-free advance widths, the same as the rest of liuska.

Supported

Not supported (yet)

Liuska skips these and reports a render warning for each image element that uses them (in Result.Warnings, the CLI output and the API response).

Limits

An SVG may be up to 32 MB (after gzip decompression) and nested up to 256 levels deep (64 for rendering, 32 for use chains). XML entities are limited to the predefined and HTML ones: there is no DTD expansion.

Performance

The SVG is parsed once per image cache entry (about 0.25 ms for a D3 bar chart). The PDF form content is built once per SVG and font set and then reused as compressed bytes, so a warm render of a page with two D3 charts takes about 37 µs (BenchmarkSVGChartPage). Templates that don't use SVG are unaffected.