Rendering SVG to PNG in Rust with resvg

resvg is an SVG renderer written in Rust. It’s split into three crates that you use together:
- usvg parses SVG and simplifies it into a render tree (resolving CSS,
useelements, text-to-paths and so on). - resvg renders that tree.
- tiny-skia is the 2D raster backend that resvg draws into, and it can encode the result as PNG.
The resvg crate re-exports usvg and tiny_skia, so one dependency is enough. A common use is turning SVG icons, charts or diagrams into PNGs in a build script or a server, without shelling out to Inkscape or a headless browser.
The code here targets resvg 0.50.0. The API has changed several times since 0.19 (the version this post originally used): usvg::Tree::from_file, usvg::FitTo, resvg::Options and resvg::Image no longer exist, so older examples won’t compile.
Adding the dependency
Section titled “Adding the dependency”[dependencies]resvg = "0.50.0"All default features are on, including text and system-fonts (needed to render <text>) and raster-images (embedded PNG/JPEG/GIF/WebP).
Converting an SVG file to PNG
Section titled “Converting an SVG file to PNG”The flow is: read the bytes, parse them into a usvg::Tree, create a Pixmap the size of the image, render, save.
use resvg::{tiny_skia, usvg};use std::error::Error;
fn main() -> Result<(), Box<dyn Error>> { let input = "assets/example.svg"; let output = "example.png";
let mut opt = usvg::Options::default(); // Resolve relative paths in <image href="..."> against the SVG's folder opt.resources_dir = std::fs::canonicalize(input) .ok() .and_then(|p| p.parent().map(|p| p.to_path_buf())); // Without fonts, <text> elements are skipped opt.fontdb_mut().load_system_fonts();
let svg_data = std::fs::read(input)?; let tree = usvg::Tree::from_data(&svg_data, &opt)?;
let size = tree.size().to_int_size(); let mut pixmap = tiny_skia::Pixmap::new(size.width(), size.height()) .ok_or("SVG has zero width or height")?;
resvg::render(&tree, tiny_skia::Transform::default(), &mut pixmap.as_mut()); pixmap.save_png(output)?;
println!("Wrote {output}"); Ok(())}This is close to the minimal example in the resvg repository. A few details:
Tree::from_datatakes raw bytes and also accepts gzip-compressed.svgz(via the defaultsvgzfeature). There’s alsoTree::from_strif you already have a&str.Pixmap::newreturnsNonefor a zero-sized image, hence theok_or.load_system_fonts()scans the system font directories, which takes a noticeable amount of time. If you render many SVGs, build theOptionsonce and reuse it.
Rendering at a different size
Section titled “Rendering at a different size”resvg::render takes a tiny_skia::Transform, so scaling is a matter of making the pixmap bigger and passing a scale transform. This renders at 2x, for example for high-DPI icons:
use resvg::{tiny_skia, usvg};
fn render_scaled(tree: &usvg::Tree, scale: f32) -> Option<tiny_skia::Pixmap> { let size = tree.size(); let width = (size.width() * scale).ceil() as u32; let height = (size.height() * scale).ceil() as u32;
let mut pixmap = tiny_skia::Pixmap::new(width, height)?; resvg::render( tree, tiny_skia::Transform::from_scale(scale, scale), &mut pixmap.as_mut(), ); Some(pixmap)}To fit a target width instead, compute scale = target_width as f32 / size.width(). If you need the PNG bytes rather than a file (for an HTTP response, say), pixmap.encode_png() returns a Vec<u8>.
Limitations
Section titled “Limitations”resvg renders static SVG. It doesn’t run scripts or animations, and it has no interactivity. The pixmap starts transparent, so if you want a background color call pixmap.fill(...) before rendering. The repository’s README documents which parts of the SVG spec are supported.
Further reading
Section titled “Further reading”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)