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

**URL:** https://discourse.julialang.org/t/how-to-convert-an-svg-file-to-png-with-fixed-pixel-size-resolution/33561
**Category:** General Usage
**Tags:** question
**Created:** [January 19, 2020, 11:07pm UTC](https://discourse.julialang.org/t/how-to-convert-an-svg-file-to-png-with-fixed-pixel-size-resolution/33561 "2020-01-19T23:07:56Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)
#### Post date: [January 19, 2020, 11:07pm UTC](https://discourse.julialang.org/t/how-to-convert-an-svg-file-to-png-with-fixed-pixel-size-resolution/33561/1 "2020-01-19T23:07:56Z")

</div>

I wonder how to fix the size of a png when converting svg to png with [`Rsvg.jl`](https://github.com/lobingera/Rsvg.jl)

Currently I am using the demo code

```julia
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);
Rsvg.handle_render_cairo(c,r);
Cairo.write_to_png(cs,filename_out);

```

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

Can anybody help?

---

<div class="post-metadata">

### Author: ![hhaensel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hhaensel/32/1207_2.png) [@hhaensel](https://discourse.julialang.org/u/hhaensel)
#### Post date: [January 20, 2020, 4:06pm UTC](https://discourse.julialang.org/t/how-to-convert-an-svg-file-to-png-with-fixed-pixel-size-resolution/33561/2 "2020-01-20T16:06:13Z")

</div>

I did find the solution myself 🙂

```julia
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)
Rsvg.handle_render_cairo(c,r)
Cairo.write_to_png(cs,filename_out)

```

Edit: I was inspired by the [rsvg manual](https://developer.gnome.org/rsvg/stable/RsvgHandle.html#rsvg_handle_set_size_callback)
