使用html2canvas将highcharts图表呈现为pdf在IE和Firefox上不起作用

我们正在使用html2canvas.js和html2canvas.svg.js(版本0.5.0 beta1)和highcharts.js将甜甜圈图下载为pdf。

这在Chrome中可以正常工作,但是在IE和Firefox中无法正常工作。在IE中,图表显示不正确,而在Firefox中则根本不显示。

以下是在Chrome,IE和Firefox中下载的屏幕截图

铬

IE(边缘)IE浏览器

火狐浏览器 在此处输入图片说明

我用来做html2canvas的代码如下:

html2canvas($("#container"), {
    onrendered: function (canvas) {
      var imgData = canvas.toDataURL(
        'image/png');
      var doc = new jsPDF('p', 'mm');
      doc.addImage(imgData, 'PNG', 10, 10);
      doc.save('sample-file.pdf');
    }
  });

我创建了一个jsFiddle来在这里演示问题-http: //jsfiddle.net/jko0rs5g/3/

有谁知道可能导致此问题的原因,以及我们如何解决该问题?

编辑

Just to clarify why we are not using the built in Highcharts exporting, this for when we are adding additional html to the Highcarts, such as additional information above or below the chart, or a score inside the donut for example. I have updated the jsfiddle to reflect this.

Rob

Thanks to Pawel Fus for the nod in the right direction, we got this working using canvg.js, which temporarily replaces the svg with a canvas before calling html2canvas.

The final issue came when some of the html within the svg uses em's to size the font (which unfortunately a lot of our templates do). We got around this by updating the font size for anything using em's to the underlying pixel size before rendering the svg into a canvas (see Get computed font size for DOM element in JS for how we calculated the actual font size)

以下是下载按钮点击的更新代码

$('#download').click(function() {
  var svgElements = $("#container").find('svg');

  //replace all svgs with a temp canvas
  svgElements.each(function() {
    var canvas, xml;

    // canvg doesn't cope very well with em font sizes so find the calculated size in pixels and replace it in the element.
    $.each($(this).find('[style*=em]'), function(index, el) {
      $(this).css('font-size', getStyle(el, 'font-size'));
    });

    canvas = document.createElement("canvas");
    canvas.className = "screenShotTempCanvas";
    //convert SVG into a XML string
    xml = (new XMLSerializer()).serializeToString(this);

    // Removing the name space as IE throws an error
    xml = xml.replace(/xmlns=\"http:\/\/www\.w3\.org\/2000\/svg\"/, '');

    //draw the SVG onto a canvas
    canvg(canvas, xml);
    $(canvas).insertAfter(this);
    //hide the SVG element
    $(this).attr('class', 'tempHide');
    $(this).hide();
  });


  html2canvas($("#container"), {
    onrendered: function(canvas) {
      var imgData = canvas.toDataURL(
        'image/png');
      var doc = new jsPDF('p', 'mm');
      doc.addImage(imgData, 'PNG', 10, 10);
      doc.save('sample-file.pdf');
    }
  });

  $("#container").find('.screenShotTempCanvas').remove();
  $("#container").find('.tempHide').show().removeClass('tempHide');
});

在此处查看其操作的更新的jsfiddle- http://jsfiddle.net/zuvzcgvz/22/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

html2canvas代码不起作用IE 11

Html2Canvas不起作用... pdf显示为空

CSS动画在Firefox和IE上不起作用

highstock 2.0.4在IE和Firefox上不起作用

html2canvas在angularjs中不起作用

html2canvas根本不起作用

为什么不使用html2canvas和canvas2image将图表保存为PNG格式?

隐藏的溢出在Firefox上不起作用,但在IE和chrome中起作用

PagedList在IE上不起作用,但在Chrome和Firefox上起作用

使用jsPdf和Html2Canvas将HTML页面转换为Angular中的PDF

使用html2canvas和jspdf将html页面截图转换为pdf

如何使用html2canvas JS将当前屏幕下载为PDF和图像?

jQuery UI droppable在IE和Firefox上不起作用(但在Chrome上有效)

<table>中的Flex CSS属性在IE和Firefox上不起作用

徽标绝对定位在Firefox和IE上不起作用

:hover属性在Firefox / IE上不起作用

粘性页脚显示表在Firefox / IE上不起作用

html2canvas无法在FireFox和IE 11中用于Google材质表

结合使用HTML2Canvas和Google图表来创建图像

使用HTML2CANVAS将莫里斯图表保存为图像

使用IE 9和虚拟主机的会话在localhost上不起作用

html 图形上传在 Firefox 上不起作用

html2canvas和jsPDF:将生成的pdf作为电子邮件附件发送

jsPDF和html2canvas无法将图像写入pdf

结合使用html2canvas和jsPDF来保存页面的PDF

如何使用html2canvas和jspdf以适当且简单的方式导出为pdf

jQuery在IE 10上不起作用

背景颜色在IE上不起作用

文件下载在Firefox上不起作用