SVG to PNG conversion using Ruby + cairo + rsvg on Mac OS X Snow Leopard + MacPorts
A way to convert a SVG file to PNG file using Ruby + cairo + rsvg on Mac OS X Snow Leopard + MacPorts is confirmed.
I thought there may be a way to use the components of web browsers to do such a thing, but I could not find such a way. So, I trid to do it using cairo + rsvg, according to http://jp.rubyist.net/magazine/?0019-cairo .
Install
Because there are big dependencies to native libraries (glib or such), port, not gem, is used to install the libraries;
$ sudo port install rb-glib2 $ sudo port install rb-rcairo $ sudo port install rb-rsvg
It took considerable time, because MacPort downloaded and compiled everything needed. And, it is important to make the MacPort up to date (sudo gem selfupdate etc.)
A test script
#svg_test.rb require 'rubygems' require 'open-uri' require 'rsvg2' SRC = 'tiger.svg' DST = 'tiger.png' unless File.exist?(SRC) open('http://croczilla.com/bits_and_pieces/svg/samples/tiger/tiger.svg') {|i| File.open(SRC, 'w') {|w| w.print i.read } } end svg = RSVG::Handle.new_from_file(SRC) surface = Cairo::ImageSurface.new(Cairo::FORMAT_ARGB32, 800, 800) context = Cairo::Context.new(surface) context.render_rsvg_handle(svg) surface.write_to_png(DST)