Problem saving as png a SVG generated by Raphael JS in a canvas

 ̄綄美尐妖づ 提交于 2019-12-05 03:31:20

canvg accepts the SVG data a file path or a single-line string

but in your code, your are passing the html content of the div #ec as

canvg('canvas', svg, {renderCallback: saveDaPicture, ignoreMouse: true, ignoreAnimation: true});

Both jQuery's $.html() and DOM's innerHTML methods return the HTML content of an element as is, so most probably in multiple lines.

canvg interprets this multi-line html content as a file path,

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="600" height="512"> 

and tries to fetch the data from the malformed url.

So the SVG to Canvas conversion process failed and that's the reason you are not getting the image as expect.

Here is a updated version that should work,

function saveDaPicture(){
    var img = document.getElementById('canvas').toDataURL("image/png");
    $('body').append('<img src="'+img+'"/>');
}

$('#save').click(function(){
    var svg = $('#ec').html().replace(/>\s+/g, ">").replace(/\s+</g, "<");
                             // strips off all spaces between tags
    //alert(svg);
    canvg('canvas', svg, {renderCallback: saveDaPicture, ignoreMouse: true, ignoreAnimation: true});
});

svgfix.js will solve this errors. take a look at this blog post Export Raphael Graphic to Image

I worked with SVG - Edit and generated SVG images, what we did was install ImageMagic on the server (you probably have it installed already).

Once installed you only need to do execute a command like "convert foo.svg foo.png" in the terminal. If you are using php then you can do:

shell_exec("convert image.svg image.png");

In php and it's done.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!