13 releases (breaking)

0.10.0 Mar 7, 2024
0.9.1 Nov 29, 2023
0.9.0 Oct 25, 2023
0.5.0 Jun 12, 2023
0.1.0 Dec 6, 2021

#11 in Multimedia

Download history 386/week @ 2024-01-03 474/week @ 2024-01-10 666/week @ 2024-01-17 767/week @ 2024-01-24 715/week @ 2024-01-31 1079/week @ 2024-02-07 769/week @ 2024-02-14 1312/week @ 2024-02-21 944/week @ 2024-02-28 2085/week @ 2024-03-06 2794/week @ 2024-03-13 1436/week @ 2024-03-20 1657/week @ 2024-03-27 1241/week @ 2024-04-03 1089/week @ 2024-04-10 661/week @ 2024-04-17

4,839 downloads per month
Used in 12 crates (6 directly)

MIT/Apache

91KB
1.5K SLoC

svg2pdf

Build status Current crates.io release Documentation

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.

Apart from groups with filters on them, the conversion will translate the SVG content to PDF without rasterizing it, so no quality is lost.

Example

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

let path = "tests/svg/custom/integration/matplotlib/time_series.svg";
let svg = std::fs::read_to_string(path)?;

// This can only fail if the SVG is malformed. This one is not.
let pdf = svg2pdf::convert_str(&svg, svg2pdf::Options::default())?;

// ... and now you have a Vec<u8> which you could write to a file or
// transmit over the network!
std::fs::write("target/time_series.pdf", pdf)?;

CLI

This crate also contains a command line interface. Install it by running the command below:

cargo install svg2pdf-cli

You can then convert SVGs to PDFs by running commands like these:

svg2pdf your.svg

Supported features

In general, a large part of the SVG specification is supported, including features like:

  • Path drawing with fills and strokes
  • Gradients
  • Patterns
  • Clip paths
  • Masks
  • Filters
  • Transformation matrices
  • Respecting the keepAspectRatio attribute
  • Raster images and nested SVGs

Unsupported features

Among the unsupported features are currently:

  • The spreadMethod attribute of gradients
  • Text will be converted into shapes before converting to PDF. It is planned to add support for text preservation in a future update.
  • Raster images are not color managed but use PDF's DeviceRGB color space
  • A number of features that were added in SVG2 (see here)

Contributing

We are looking forward to receiving your bugs and feature requests in the Issues tab. We would also be very happy to accept PRs for bug fixes, features, or refactorings! We'd be happy to assist you with your PR's, so feel free to post Work in Progress PRs if marked as such. Please be kind to the maintainers and other contributors. If you feel that there are any problems, please feel free to reach out to us privately.

Thanks to each and every prospective contributor for the effort you (plan to) invest in this project and for adopting it!

License

svg2pdf is licensed under a MIT / Apache 2.0 dual license.

Users and consumers of the library may choose which of those licenses they want to apply whereas contributors have to accept that their code is in compliance and distributed under the terms of both of these licenses.

Dependencies

~18MB
~198K SLoC