最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using html2canvas to render a highcharts chart to pdf doesn't work on IE and Firefox - Stack Overflow

programmeradmin4浏览0评论

We are using html2canvas.js and html2canvas.svg.js (version 0.5.0 beta1) and highcharts.js to download a donut chart into pdf.

This works as expected in Chrome, however in IE and Firefox this isnt working. In IE the chart is rendered incorrectly, and in Firefox it is not rendered at all.

Below are screenshots of the download in Chrome, IE and Firefox

Chrome

IE (Edge)

Firefox

The code i am using to do the html2canvas is as follows:

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');
    }
  });

I have created a jsFiddle that demonstrates the issue here - /

Does anyone know what might be causing this issue, and how we can resolve it?

EDIT

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.

We are using html2canvas.js and html2canvas.svg.js (version 0.5.0 beta1) and highcharts.js to download a donut chart into pdf.

This works as expected in Chrome, however in IE and Firefox this isnt working. In IE the chart is rendered incorrectly, and in Firefox it is not rendered at all.

Below are screenshots of the download in Chrome, IE and Firefox

Chrome

IE (Edge)

Firefox

The code i am using to do the html2canvas is as follows:

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');
    }
  });

I have created a jsFiddle that demonstrates the issue here - http://jsfiddle/jko0rs5g/3/

Does anyone know what might be causing this issue, and how we can resolve it?

EDIT

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.

Share Improve this question edited Dec 7, 2015 at 14:28 Rob asked Dec 2, 2015 at 12:12 RobRob 1,5572 gold badges16 silver badges22 bronze badges 4
  • 3 Have you considered exporting module? Highcharts has exporting module that allows chart exporting to e.g. PDF format. JSFiddle: jsfiddle/27mp2ww8 – Kacper Madej Commented Dec 2, 2015 at 12:40
  • @Kacper Yes, under most situations we use the built in Highcharts exporting, which works well. This is for when there are a bination of Highcharts and normal html on a page which needs exporting in its entirety (the example just shows a single Highcharts chart for example purposes). – Rob Commented Dec 2, 2015 at 14:24
  • Not an answer, but an alternative ... cloudformatter./CSS2Pdf.SVGCharts.HighCharts – Kevin Brown Commented Dec 3, 2015 at 1:24
  • 1 I think there may be some issue with html2canvas lib. Check this answer. – Paweł Fus Commented Dec 3, 2015 at 14:51
Add a ment  | 

1 Answer 1

Reset to default 14

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 puted font size for DOM element in JS for how we calculated the actual font size)

Below is the updated code for the download button click

$('#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\\/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');
});

See an updated jsfiddle of it in action here - http://jsfiddle/zuvzcgvz/22/

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论