I am working on a PHP script in which I will need to convert SVG to png and to save CPU usage and server load it's better to do the conversion in the client site(Browser) and this is possible with only Javascript. Html code :
<div>
<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" version="1.1" width="800" height="420" id="svg"><image height="420" width="800" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="https://upload.wikimedia.org/wikipedia/commons/7/76/Tabby_cat-hello.jpg" transform="matrix(1 0 0 1 0 0)" class="draggable" type="staticimg" id="1465449556528" ></image> </svg>
<a id='imgId'>Save</a>
</div>
<canvas></canvas>
Javascript code:
var svg = document.getElementsByTagName('svg')[0];
var svg_xml = (new XMLSerializer()).serializeToString(svg),
blob = new Blob([svg_xml], {type:'image/svg+xml;charset=utf-8'}),
url = window.URL.createObjectURL(blob);
var img = new Image();
img.width = 730;
img.height = 300;
img.onload = function(){
var canvas = document.createElement('canvas');
canvas.width = 730;
canvas.height = 300;
var ctx = canvas.getContext('2d');
ctx.drawImage(img, 0, 0, 730, 300);
window.URL.revokeObjectURL(url);
var canvasdata = canvas.toDataURL('image/png');
var a = document.getElementById('imgId');
a.download = "export_" + Date.now() + ".png";
a.href=canvasdata;
}
img.src = url
the svg works just find but i cant convert to png. JSFIDDLE : http://jsfiddle.net/vyLtxgh4/6/