-
-
Save caged/4649511 to your computer and use it in GitHub Desktop.
var svg = document.getElementById('graph'), | |
xml = new XMLSerializer().serializeToString(svg), | |
data = "data:image/svg+xml;base64," + btoa(xml), | |
img = new Image() | |
img.setAttribute('src', data) | |
document.body.appendChild(img) |
Just a heads-up: this doesn't work if the SVG features symbols outside of the Latin1 range as the btoa
function is unable to convert these:
Uncaught DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range.
This Stack Overflow answer provides a workaround though: http://stackoverflow.com/a/26603875/1317805
I'm trying to doit with a Highmaps svg but it throws InvalidCharacterError: String contains an invalid character
This does NOT convert SVG images to PNG. In fact, it does not transform the data at all, all it does is take an SVG element in the document tree, serialize it as text, create a data URI with the text embedded, and create an image that uses said URI, in effect creating an image with the entire image source embedded in the URI. PNG does not figure into this at all.
Nice, worked perfectly to make an embedded SVG downloadable!
http://svgopen.org/2010/papers/62-From_SVG_to_Canvas_and_Back/ ?