How to save an svg generated by raphael

Andreas Köberle picture Andreas Köberle · Apr 12, 2012 · Viewed 9.3k times · Source

Is there a way to save the SVG generated by raphael as an svg file. Note it only need to work in chrome.

Answer

Andreas Köberle picture Andreas Köberle · Apr 18, 2012

I came up with a solution using Raphael.Export, which gives me an valid svg/xml String (something that I couldn't get from the simply from the innerHTML of the DOM object that holds the svg) and Blobbuilder to create a url for a link which will I fire a click event at the end to save the file.

svgString = @paper.toSVG();
a = document.createElement('a');
a.download = 'mySvg.svg';
a.type = 'image/svg+xml';
bb = new(window.BlobBuilder || WebKitBlobBuilder);
bb.append(svgString);
blob = bb.getBlob('image/svg+xml');
a.href = (window.URL || webkitURL).createObjectURL(blob);
a.click();