1

If I do not add foreignObject tag in html, the code works fine and the content saves as image file but when I add the foreignObject, the browser show error in console. Is there a way to convert SVG to canvas and save it to image file if the SVG contains foreignObject. The code I tried is below

<button id="button">svg to png</button>


<svg id = "svgId" width="13cm" height="13cm" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="200" height="200">
  <rect x="0" y="0" width="5cm" height="5cm" style="fill:red;"/>
    <g>
        <rect width = "8.6cm" height = "10cm" style="stroke:black; stroke-width:2; fill:none;"/>
        <text x="1cm" y = "6.5cm" style="font-size:28px;">Heading Here</text>
        <line x1="0cm" y1="8cm" x2="8.6cm" y2="8cm" style="stroke:black; stroke-width:1;"/>
        <foreignObject width="8cm" height="4.5cm" x = "0.5cm" y="8.5cm"  requiredFeatures="http://www.w3.org/TR/SVG11/feature#Extensibility">
              <p xmlns="http://www.w3.org/1999/xhtml" id = "news1">Text goes here asd sadl sad sadsad sad sa dsad asd sad saasd sad asd sad sad sasd sadsa dsa sadsad sad asdjsalkdjsal salkdj jsalkdjsal akld lksa adsad asa asa sa adasdas das dsad sad sad asd sad sad sakldjsalkjdlsakdj sakldj salkdjsakldjsaldkja sdlksaj dlkaj sa</p>
            </foreignObject>
    </g>
</svg>

<canvas id="canvas" width="400" height = "400"></canvas>

<script type="text/javascript">
var btn = document.getElementById('button');
var svg = document.getElementById('svgId');
var canvas = document.getElementById('canvas');

function triggerDownload (imgURI) {
  var evt = new MouseEvent('click', {
    view: window,
    bubbles: false,
    cancelable: true
  });

  var a = document.createElement('a');
  a.setAttribute('download', 'image.png');
  a.setAttribute('href', imgURI);
  a.setAttribute('target', '_blank');

  a.dispatchEvent(evt);
}

btn.addEventListener('click', function () {
  var canvas = document.getElementById('canvas');
  var ctx = canvas.getContext('2d');
  var data = (new XMLSerializer()).serializeToString(svg);
  var DOMURL = window.URL || window.webkitURL || window;

  var img = new Image();
  var svgBlob = new Blob([data], {type: 'image/svg+xml;charset=utf-8'});
  var url = DOMURL.createObjectURL(svgBlob);

  img.onload = function () {
    ctx.drawImage(img, 0, 0,'12cm','12cm');
    DOMURL.revokeObjectURL(url);

    var imgURI = canvas
        .toDataURL('image/png')
        .replace('image/png', 'image/octet-stream');

    triggerDownload(imgURI);
  };

  img.src = url;
});

</script>
AliSavi
  • 53
  • 5
  • i'm using chrome and its not working in it – AliSavi May 16 '17 at 20:41
  • Possible duplicate of [Problems with getting dataURI from image](http://stackoverflow.com/questions/40897039/problems-with-getting-datauri-from-image) – Kaiido May 17 '17 at 00:33
  • @RobertLongson, it is an known [chrome's security measure](https://bugs.chromium.org/p/chromium/issues/detail?id=294129) that they are leveraging, which actually had an [implementation bug](https://bugs.chromium.org/p/chromium/issues/detail?id=675608&can=2&start=0&num=100&q=&colspec=ID%20Pri%20M%20Stars%20ReleaseBlock%20Component%20Status%20Owner%20Summary%20OS%20Modified&groupby=&sort=) allowing for a workaround. But only FF and chrome do support exporting canvas with fO, and with a lot of caveats. – Kaiido May 17 '17 at 00:37

0 Answers0