Render SVG Images into PDF, PNG, PostScript, or Bitmap Arrays
Renders vector-based ‘svg’ images into high-quality custom-size bitmap arrays using ‘librsvg’. The resulting bitmap can be written to e.g. ‘png’, ‘jpeg’ or ‘webp’ format. In addition, the package can convert images directly to various formats such as pdf or postscript.
Documentation
- The R package: https://docs.ropensci.org/rsvg
- Upstream librsvg: https://gitlab.gnome.org/GNOME/librsvg
Hello World
# create some svg
tmp <- tempfile()
svglite::svglite(tmp, width = 10, height = 7)
ggplot2::qplot(mpg, wt, data = mtcars, colour = factor(cyl))
dev.off()
# render it into a bitmap array
bitmap <- rsvg(tmp, height = 1440)
dim(bitmap) # h*w*c
png::writePNG(bitmap, "bitmap.png", dpi = 144)
jpeg::writeJPEG(bitmap, "bitmap.jpg", quality = 1)
webp::write_webp(bitmap, "bitmap.webp", quality = 100)
# render straight to output format
rsvg_pdf(tmp, "out.pdf")
rsvg_png(tmp, "out.png")
rsvg_svg(tmp, "out.svg")
rsvg_ps(tmp, "out.ps")
Installation
Binary packages for OS-X or Windows can be installed directly from CRAN:
install.packages("rsvg")
Installation from source on Linux or OSX requires librsvg2
. On Debian or Ubuntu install librsvg2-dev:
sudo apt-get install -y librsvg2-dev
On Fedora, CentOS or RHEL we need librsvg2-devel:
sudo yum install librsvg2-devel
On OS-X use rsvg from Homebrew:
brew install librsvg