Reputation: 6449
I'm sending a SVG string from browser (Javascript) to a server that is running Rails (Ruby). I want to convert this string to a PNG with transparency or, at least, a SVG file so I can convert it later
Any ideas? I installed RMagick but I'm still not sure how to create the file from the string.
Any other solution to achieve this?
The idea is to create simple "logos" dynamically
Upvotes: 9
Views: 9134
Reputation: 24439
With RMagick, just read the string into an image instance with Image.from_blob + defining the SVG format
require "RMagick"
svg_string='
<svg xmlns="http://www.w3.org/2000/svg" width="120" height="120" viewPort="0 0 120 120" version="1.1">
<rect width="150" height="150" fill="rgb(0, 255, 0)" stroke-width="1" stroke="rgb(0, 0, 0)" />
<line x1="20" y1="100" x2="100" y2="20" stroke="black" stroke-width="2"/>
</svg>'
img = Magick::Image.from_blob(svg_string) {
self.format = 'SVG'
self.background_color = 'transparent'
}
img.write "example_out.png"
Edit
If the string is just a SVG path, there's Magick::Draw.path
to "rebuild" vector graphics. Doc & examples here.
Upvotes: 12
Reputation: 1227
We did this with some SVG charts we generated with c3/d3. We ended up using Capybara to visit a web page that rendered the SVG, and then saving a snapshot of the page.
Upvotes: 0