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>
from pathlib import Path

import vl_convert as vlc

svg = Path("chart.svg").read_text(encoding="utf-8")

png = vlc.svg_to_png(svg, scale=2)
pdf = vlc.svg_to_pdf(svg)
Path("chart.png").write_bytes(png)
Path("chart.pdf").write_bytes(pdf)

Both results are bytes. svg_to_jpeg() produces JPEG.

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.