Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert SVG's to PNGs. This works OK if the SVG's styles are inline. The SVG element must contain an xmlns attribute. Webkit also requires you specify a font size on `text` elements.
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)
@amn
Copy link

amn commented Jul 31, 2019

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.

@MarkRobertJohnson
Copy link

MarkRobertJohnson commented Apr 14, 2020

Nice, worked perfectly to make an embedded SVG downloadable!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment