当我单击时下载带有动态ID的pdf时,使用jspdf的pdf中出现了相同的highchart

时间:2018-11-05 18:50:05

标签: javascript jquery html highcharts jspdf

我正在使用jspdf捕获Hichchart的内容,该内容通过动态ID onclick动态生成。在这里,当我单击Submit1按钮时,将生成带有动态ID的不同图表,同样,当我单击下载按钮时,它将下载pdf中动态创建的图表内容。我可以下载,但同一图表将再次下载,而不是页面上的其他图表。我已更新代码以在此处进行演示 http://plnkr.co/edit/Z3hJcC8pgzZxXPLWpfBw?p=preview,任何人都可以帮助我,我只是将html和javascript代码放在这里。

html

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
 <script src="jspdf.debug.js"></script>
 <script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/series-label.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js"></script>
  <script src="https://github.com/devongovett/pdfkit/releases/download/v0.6.2/pdfkit.js"></script>
  <script src="rgbcolor.js"></script>
  <script src="canvg.js"></script>
  <script src="script.js"></script>

  <input type="button" id="download"  value="download" /> click to console
  <div><button id="button1" class="button1">submit1</button></div>

script.js

$(document).ready(function() {
    var index = 0;
    var id = [];
    $('#button1').on('click', function() {
        $('body').append($("<div id='chart" + index + "'></div>"));

        Highcharts.chart('chart' + index, {
           title: {
        text: 'Chart-'+index+''
    },
            series: [{
                data: [1, 2, 3]
            }]
        });
        var temp = "chart" + index + "";
        console.log(temp);
        id.push(temp);

        index++;
    });

    $('#download').on('click', function() {
console.log(id);
  var doc = new jsPDF('portrait', 'pt', 'a4', true);
  var elementHandler = {
    '#ignorePDF': function(element, renderer) {
      return true;
    }
  };

  var source = document.getElementById("top-content");
  doc.fromHTML(source, 15, 15, {
    'width': 560,
    'elementHandlers': elementHandler
  });

  var DOMURL = window.URL || window.webkitURL || window;
   var elements = id;

 // console.log(xx);
  for (let i in elements) {
      console.log(elements[i]);
      var svg = document.querySelector('svg');
      var canvas = document.createElement('canvas');
      var canvasIE = document.createElement('canvas');
      var context = canvas.getContext('2d');

      var data1 = (new XMLSerializer()).serializeToString(svg);
      canvg(canvas, data1);
      var svgBlob = new Blob([data1], {
        type: 'image/svg+xml;charset=utf-8'
      });

      var url = DOMURL.createObjectURL(svgBlob);

      var img = new Image();
      img.onload = function() {
        context.canvas.width = $("#"+elements[i]).find('svg').width();
        context.canvas.height = $("#"+elements[i]).find('svg').height();

        context.drawImage(img, 0, 0);
        // freeing up the memory as image is drawn to canvas
        DOMURL.revokeObjectURL(url);

        var dataUrl;
        if (isIEBrowser()) { // Check of IE browser 
          var svg = $(elements[i]).highcharts().container.innerHTML;
          canvg(canvasIE, svg);
          dataUrl = canvasIE.toDataURL('image/JPEG');
        } else {
          dataUrl = canvas.toDataURL('image/jpeg');
        }

        doc.addImage(dataUrl, 'JPEG', 20, 150, 560, 350);

          var bottomContent = document.getElementById("bottom-content");
  doc.fromHTML(bottomContent, 15, 700, {
    'width': 560,
    'elementHandlers': elementHandler
  });
         doc.fromHTML(source, 15, 15, {
    'width': 560,
    'elementHandlers': elementHandler
  });
  doc.addPage();
      };

      img.src = url;

    }


  setTimeout(function() {
    doc.save('TestChart.pdf');
  }, 2000);

});
});
function isIEBrowser() {
  var ieBrowser;
  var ua = window.navigator.userAgent;
  var msie = ua.indexOf("MSIE ");

  if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // Internet Explorer
  {
    ieBrowser = true;
  } else //Other browser
  {
    console.log('Other Browser');
    ieBrowser = false;
  }

  return ieBrowser;
};

1 个答案:

答案 0 :(得分:1)

我在您的代码中发现了两个问题。

首先,您使用document.querySelector('svg');仅返回第一个svg元素-您应使用document.querySelectorAll('svg');,然后获取正确的元素svg[i]

第二,您似乎在代码异步方面遇到了问题-帮助将所有声明var更改为let

实时演示:http://plnkr.co/edit/epHnjnPhWFUdtic97jNA?p=preview