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:
- Page CSS doesn't travel. In the old HTML page, rules such as
.axis text { font-size: 7px }styled the chart. A standalone SVG only sees its own styles, so put those rules into a<style>element inside the<svg>(svg.append("style").text(css)). Liuska applies them: it supports type,.classand#idselectors, compound selectors, the descendant and>combinators, selector lists, specificity and!important. Without them, D3's own defaults apply:currentColor(black),10px sans-serifon axes and 16px on other text. - Sizes.
clientWidthis 0 in jsdom, so pass the chart box size in explicitly. One SVG pixel becomes one point when the element box has the same numbers. In any case the SVG is scaled into the element box according tofit. examples/svg-charts/contains the benchmark workload's two D3 v5 charts and four D3 v7 charts (donut, gradient area with clip path, symbols, stacked bars), rendered this way, plus a few icons.
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:
- 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. monospacemaps to the bundled Go Mono.- Generic families (
sans-serif,serif,system-ui, …) and unknown names fall back to the image element's ownfontFamily(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
- Elements:
svg(also nested, as a viewport),g,a,switch(first child),defs,symbol,use(href/xlink:href, also of symbols withviewBox),path,rect(rx/ry),circle,ellipse,line,polyline,polygon,text,tspan,style,linearGradient,radialGradient,stop,clipPath. - Path data: every command (
M L H V C S Q T A Z, absolute and relative), compact number syntax (1.5.5,1e-3, arc flags without separators). - Transforms:
matrix,translate,scale,rotate(with a centre),skewX,skewY. - Painting:
fill/strokewith any CSS colour (hex with alpha,rgb()/rgba(),hsl()/hsla(), the named colours,currentColorwithcolor),fill-opacity,stroke-opacity,opacity,fill-rule,stroke-width,stroke-linecap,stroke-linejoin,stroke-miterlimit,stroke-dasharray,stroke-dashoffset,display,visibility. Values can be set as presentation attributes, instyle=""or in<style>rules (including thefontshorthand). - Gradients: linear and radial (
fx/fy/frfocal point),gradientUnits(both),gradientTransform,hrefinheritance, any number of stops. In PDF,stop-opacitybecomes a soft mask. - Clipping:
clip-path="url(#id)"on any element or group,clipPathUnits(both),clip-rule, nested clips. - Text:
x,y,dx,dy(the first value of each list),text-anchorper text chunk,dominant-baseline/alignment-baseline(middle,central,hanging,text-after-edge),letter-spacing,font-sizein px, pt, em or %, nestedtspans, whitespace collapsing.
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).
filter,mask,patternfills (the fallback colour inurl(#p) redis used),marker, embedded<image>,foreignObject,textPath, animation.spreadMethodreflect/repeat(drawn aspad), gradient fills on text (drawn with the middle stop colour) and gradient strokes (middle stop colour).- Group
opacityis multiplied into each child instead of being composited as one layer, so overlapping children inside a semi-transparent group show through each other. The element-levelopacityof the Liuskaimageelement does composite as one layer, because the SVG form is a transparency group. - Per-glyph position lists (
x="10 20 30"),rotateon text,writing-mode,textLength, CSS@mediarules, pseudo-classes, attribute and sibling selectors. - External references:
hrefs that point outside the document are never fetched, and nothing is loaded over the network or from disk. - The PNG preview fills even-odd holes with nonzero winding, draws all stroke joins and caps round, and ignores clip paths on text. The PDF output is exact.
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.