Converting from SVG#

Use the SVG functions when another tool has already produced the vector image. VlConvert converts it to PNG, JPEG, or PDF without running Vega.

The SVG must declare its size through width and height attributes or a viewBox. Fonts and images it references must be available to the rendering process. See Fonts and Google Fonts and Security and Network Access when the SVG is not self-contained.

Save this SVG document:

chart.svg
<svg xmlns="http://www.w3.org/2000/svg" width="320" height="120" viewBox="0 0 320 120">
  <rect width="320" height="120" rx="8" fill="#f3f6fa"/>
  <circle cx="60" cy="60" r="32" fill="#4c78a8"/>
  <path d="M118 84 L158 28 L198 84 Z" fill="#f58518"/>
  <rect x="230" y="28" width="64" height="64" rx="6" fill="#54a24b"/>
</svg>

Send JSON with the markup in an svg string to /svg/png, /svg/jpeg, or /svg/pdf. Save this complete request as request.json:

request.json
{
  "svg": "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"320\" height=\"120\" viewBox=\"0 0 320 120\">\n  <rect width=\"320\" height=\"120\" rx=\"8\" fill=\"#f3f6fa\"/>\n  <circle cx=\"60\" cy=\"60\" r=\"32\" fill=\"#4c78a8\"/>\n  <path d=\"M118 84 L158 28 L198 84 Z\" fill=\"#f58518\"/>\n  <rect x=\"230\" y=\"28\" width=\"64\" height=\"64\" rx=\"6\" fill=\"#54a24b\"/>\n</svg>\n",
  "scale": 2
}
$ curl http://127.0.0.1:3000/svg/png \
>   -H 'Content-Type: application/json' \
>   --data-binary @request.json \
>   --output chart.png

The PNG conversion preserves the shapes and colors in the SVG document:

A blue circle, orange triangle, and green square on a pale background

See Image Size and Quality Recommendations for the scale, ppi, and quality options.