1 回答
TA贡献1804条经验 获得超8个赞
看来您可能需要研究 Javascript 回调。回调onrendered不会立即发生,它们会在操作完成后执行。
您应该将最后 3 行移到最后一个嵌套onrendered回调中。通过此更改,将在 html2canvas 渲染发生后生成 PDF。
$('#print-chart-btn-donut').on('click', function() {
var canvas = document.querySelector('#' + chart_name_donut + '_Chart');
var canvas_img = canvas.toDataURL("image/png", 1.0); //JPEG will not match background color
var doc = new jsPDF('landscape', 'in', 'letter');
html2canvas(document.getElementById(chart_name_donut + "_Title"), {
onrendered: function(canvas) {
html2canvas(document.getElementById(chart_name_donut + "_LegendContainer"), {
onrendered: function(canvas2) {
var img = canvas.toDataURL("image/png");
doc.addImage(img, 'png', -4.25, 2, 20, 4, 'Doughnut1', 0, 0);
var img2 = canvas2.toDataURL("image/png");
doc.addImage(img2, 'png', -4.25, 1.75, 20, 4, 'Doughnut2', 0, 0);
// Generate the PDF after the final callback has executed
doc.autoPrint();
var blob = doc.output("bloburl");
window.open(blob, 'Doughnut Chart', "height=" + window.outerHeight + ", width=" + 0.36 * window.innerWidth + "\"");
}
})
}
});
doc.addImage(canvas_img, 'png', -4.25, 1.75, 20, 4, 'Doughnut', 0, 0);
});
添加回答
举报