Crate svg2pdf

source ·
Expand description

Convert SVG files to PDFs.

This crate allows to convert static (i.e. non-interactive) SVG files to either standalone PDF files or Form XObjects that can be embedded in another PDF file and used just like images.

The conversion will translate the SVG content to PDF without rasterizing them (the only exception being objects with filters on them, but in this case only this single group will be rasterized, while the remaining contents of the SVG will still be turned into a vector graphic), so no quality is lost.

§Example

This example reads an SVG file and writes the corresponding PDF back to the disk.

use svg2pdf::usvg::fontdb;
use svg2pdf::{ConversionOptions, PageOptions};
use std::sync::Arc;

let input = "tests/svg/custom/integration/matplotlib/stairs.svg";
let output = "target/stairs.pdf";

let svg = std::fs::read_to_string(input)?;
let mut options = svg2pdf::usvg::Options::default();
options.fontdb_mut().load_system_fonts();
let tree = svg2pdf::usvg::Tree::from_str(&svg, &options)?;

let pdf = svg2pdf::to_pdf(&tree, ConversionOptions::default(), PageOptions::default());
std::fs::write(output, pdf)?;

§Supported features

In general, a very large part of the SVG specification is supported, including but not limited to:

  • Paths with simple and complex fills
  • Gradients
  • Patterns
  • Clip paths
  • Masks
  • Transformations
  • Viewbox
  • Text
  • Raster images and nested SVGs

§Unsupported features

Among the unsupported features are currently:

  • The spreadMethod attribute of gradients
  • Raster images are not color managed but use PDF’s DeviceRGB color space
  • A number of features that were added in SVG2, See here for a more comprehensive list.

Re-exports§

Structs§

Functions§