How to save an svg generated by raphael
Asked Answered
L

5

18

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

Lenwood answered 12/4, 2012 at 9:32 Comment(2)
I presume you mean save at the client end? There must be a better way than this, but worst-case scenario, you could grab the XML with JavaScript/jQuery, send it to the server via AJAX, and then serve it to the user via your usual web framework with appropriate headers set.Demob
Ah, another way: you can encode an SVG file as a base64 string, then add it to a data: link. The user can click it to display, and use 'Save' in their browser.Demob
A
8

As stef commented, the BlobBuilder API is deprecated. Instead, use the Blob API.

Building on Andreas' answer, here is how I quickly got it to work:

svgString = r.toSVG();
a = document.createElement('a');
a.download = 'mySvg.svg';
a.type = 'image/svg+xml';
blob = new Blob([svgString], {"type": "image/svg+xml"});
a.href = (window.URL || webkitURL).createObjectURL(blob);
a.click();
Anesthesiologist answered 6/9, 2013 at 21:40 Comment(0)
L
14

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();
Lenwood answered 18/4, 2012 at 20:51 Comment(2)
Sadly looks like BlobBuilder is obsolete. Blob appears to be the replacement but has no append method.Communication
Neema's answer has the current solution.Kinnon
A
8

As stef commented, the BlobBuilder API is deprecated. Instead, use the Blob API.

Building on Andreas' answer, here is how I quickly got it to work:

svgString = r.toSVG();
a = document.createElement('a');
a.download = 'mySvg.svg';
a.type = 'image/svg+xml';
blob = new Blob([svgString], {"type": "image/svg+xml"});
a.href = (window.URL || webkitURL).createObjectURL(blob);
a.click();
Anesthesiologist answered 6/9, 2013 at 21:40 Comment(0)
W
1

If you don't want to use the Rapahel.Export, you can directly get the svg from the dom, like that:

var svgString = document.getElementById('holder').innerHTML;
a = document.createElement('a');
a.download = 'mySvg.svg';
a.type = 'image/svg+xml';
blob = new Blob([svgString], {"type": "image/svg+xml"});
a.href = (window.URL || webkitURL).createObjectURL(blob);
a.click();

"holder" is the id of the div where is loaded Raphael : r = Raphael("holder"); Note that I think it won't work on old browser that doesn't handle svg

Wiliness answered 12/8, 2015 at 11:16 Comment(0)
D
0

For the part where you save the blob, I'd recommend https://github.com/eligrey/FileSaver.js/

or https://www.npmjs.com/package/angular-file-saver if you're using AngularJs

Diageotropism answered 1/12, 2015 at 18:0 Comment(0)
I
0

At least for simple cases we can use the div that the Raphael plot is written to (usually 'canvas') and download the innerhtml from that. First, we need a javascript function which will copy the content of the div and create a download link (this function was from another page, not my work):

<script>
function downloadInnerHtml(filename, divId, mimeType) {
var elHtml = document.getElementById(divId).innerHTML;
console.log(elHtml); //useful for troubleshooting
var link = document.createElement('a');
mimeType = mimeType || 'text/plain';
link.setAttribute('download', filename);
link.setAttribute('href', 'data:' + mimeType + ';charset=utf-8,' + encodeURIComponent(elHtml));
link.click(); 
}
</script>

Second, we need a button on the page to run the function

<button onclick="downloadInnerHtml('raphaelplot.svg', 'canvas','text/html')">save plot as svg</button>
Isoleucine answered 1/6, 2021 at 9:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.