How to convert an svg file to png with fixed pixel size/resolution

I wonder how to fix the size of a png when converting svg to png with Rsvg.jl

Currently I am using the demo code

using Rsvg
using Cairo

filename_in = "a4.svg"
filename_out = "a4.png"

r = Rsvg.handle_new_from_file(filename_in);
d = Rsvg.handle_get_dimensions(r);
cs = Cairo.CairoImageSurface(d.width,d.height,Cairo.FORMAT_ARGB32);
c = Cairo.CairoContext(cs);

I played around with set_default_dpi(dpi::Float64) and handle_set_dpi(handle::RsvgHandle, dpi::Float64) but without success.

Can anybody help?

1 Like

I did find the solution myself :slight_smile:

new_height = 48
r = Rsvg.handle_new_from_file(filename_in)
d = Rsvg.handle_get_dimensions(r)
scalingfactor = new_height / d.height
cs = Cairo.CairoImageSurface(round(Int,d.width * scalingfactor), new_height, Cairo.FORMAT_ARGB32)
c = Cairo.CairoContext(cs)
Cairo.scale(c, scalingfactor, scalingfactor)

Edit: I was inspired by the rsvg manual

1 Like