You’re right about the documentation! We should fix that.
I did a quick test, calling myDiagram.makeSvg({ scale: 1, callback: function(svg) { console.log(svg); } })
. It worked as I expected, returning null immediately and then calling the callback with the SVG DOM.
I also did a quick test:
<!DOCTYPE html>
<html>
<head>
<title>Minimal GoJS Sample</title>
<!-- Copyright 1998-2020 by Northwoods Software Corporation. -->
<meta charset="UTF-8">
<script src="https://unpkg.com/gojs@2.1.21/release/go.js"></script>
<script id="code">
function init() {
var $ = go.GraphObject.make;
myDiagram =
$(go.Diagram, "myDiagramDiv");
myDiagram.nodeTemplate =
$(go.Node, "Auto",
$(go.Shape,
{ fill: "white" },
new go.Binding("fill", "color")),
$(go.Picture,
{ margin: 8, width: 50, height: 50 },
new go.Binding("source", "key", function(t) { return "https://unpkg.com/gojs@2.1.21/samples/images/hs" + t + ".jpg"; }))
);
myDiagram.model = new go.GraphLinksModel(
[
{ key: 1, text: "Alpha", color: "lightblue" },
{ key: 2, text: "Beta", color: "orange" },
{ key: 3, text: "Gamma", color: "lightgreen" },
{ key: 4, text: "Delta", color: "pink" }
],
[
{ from: 1, to: 2 },
{ from: 1, to: 3 },
{ from: 2, to: 2 },
{ from: 3, to: 4 },
{ from: 4, to: 1 }
]);
}
// partly taken from samples/svgDataUrl.html
function toDataURL(url, callback) {
var xhr = new XMLHttpRequest();
xhr.onload = () => {
var reader = new FileReader();
reader.onloadend = () => callback(reader.result);
reader.readAsDataURL(xhr.response);
};
xhr.open('GET', url);
xhr.responseType = 'blob';
xhr.send();
}
function setupDownload(svg) {
const filename = 'new.svg';
const aTag = document.createElement('a');
const svgstr = new XMLSerializer().serializeToString(svg);
const blob = new Blob([svgstr], { type: 'image/svg+xml' });
document.body.appendChild(aTag);
const url = window.URL.createObjectURL(blob);
aTag.setAttribute('href', url);
aTag.setAttribute('download', filename);
aTag.click();
window.URL.revokeObjectURL(url);
document.body.removeChild(aTag);
}
// Make SVG, but modify the SVG <image> Element's href to refer to a Base64 URI instead of the go.Picture source URL.
function downloadSvgWithData() {
myDiagram.makeSvg({
scale: 1,
background: 'white',
elementFinished: (graphobject, svgelement) => {
if (!(graphobject instanceof go.Picture)) return;
toDataURL(svgelement.href.baseVal, (dataUrl) => {
svgelement.setAttribute('href', dataUrl);
});
},
callback: (svg) => { setTimeout(() => { setupDownload(svg); }, 1000); },
});
}
</script>
</head>
<body onload="init()">
<div id="myDiagramDiv" style="border: solid 1px black; width:100%; height:600px"></div>
<button onclick="downloadSvgWithData()">Download</button>
</body>
</html>
The images were correctly embedded as data URIs in the downloaded SVG.
The use of a 1 second wait isn’t reliable, though, so you will need to decide on your own timeout or else do a better job making sure as many SVG files are downloaded as you can before you give up and download anyway.