1 回答

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超8個(gè)贊
看來(lái)您可能需要研究 Javascript 回調(diào)。回調(diào)onrendered不會(huì)立即發(fā)生,它們會(huì)在操作完成后執(zhí)行。
您應(yīng)該將最后 3 行移到最后一個(gè)嵌套o(hù)nrendered回調(diào)中。通過(guò)此更改,將在 html2canvas 渲染發(fā)生后生成 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);
});
添加回答
舉報(bào)