Skip to content

Instantly share code, notes, and snippets.

@aosalias
Created April 14, 2016 17:32
Show Gist options
  • Save aosalias/1f0dd03b2f7d639fee58dd090b9a519b to your computer and use it in GitHub Desktop.
Save aosalias/1f0dd03b2f7d639fee58dd090b9a519b to your computer and use it in GitHub Desktop.
Create a download link for an svg client side (Like one drawn from D3.js) (Excuse the jQuery, this could easily be done in raw js)
$('.container svg').attr({
'xmlns': 'http://www.w3.org/2000/svg',
'xmlns:xlink': "http://www.w3.org/1999/xlink"
});
var base64SvgText = btoa(
encodeURIComponent(
$('.container').html()
).replace(/%([0-9A-F]{2})/g,
function (match, p1) {
return String.fromCharCode('0x' + p1);
}
)
);
$('a.download-svg').attr({
'download': 'cool-graph.svg',
'href': 'data:image/svg+xml;charset=utf-8;base64,' + base64SvgText
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment