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>
use vl_convert_rs::{PngOpts, VlConverter};
let svg = std::fs::read_to_string("chart.svg")?;
let converter = VlConverter::new();
let output = converter
.svg_to_png(
&svg,
PngOpts {
scale: Some(2.0),
..Default::default()
},
)
.await?;
std::fs::write("chart.png", output.data)?;
The PNG bytes are in output.data. Use svg_to_jpeg() with JpegOpts or svg_to_pdf() with PdfOpts for other formats. These methods apply the converter’s font and image-access settings. They require a Tokio runtime but do not start JavaScript workers.
The PNG conversion preserves the shapes and colors in the SVG document:
See Image Size and Quality Recommendations for the scale, ppi, and quality options.