我尝试使用jsPDF导出谷歌图表和HTML表格 . 由于谷歌图表无法导出,我创建了一个图像,并使用pdf.addImage()将其添加到pdf . 然后使用'pdf.fromHTML()'方法将表附加到相同的pdf文件 . 提供更高的上边距值以在图像下方显示它 .

function exportChart() {
    var pdf = new jsPDF('p', 'pt', 'letter');

    //Creating the image from chart and adding to the pdf
    var img = document.getElementById("graphImg");
    var canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;
    var ctx = canvas.getContext("2d");
    ctx.drawImage(img, 0, 0);
    var dataURL = canvas.toDataURL("image/png");
    pdf.addImage(dataURL, "PNG", 100,20); 
    canvas = null;

    //Adding the html table to the same pdf file 
    source = $('#graphDiv')[0];
    specialElementHandlers = {
        '#bypassme': function (element, renderer) {
            return true;
        }
    };
    margins = {
        top: 300, // Providing high top margin value to make the table below to the image
        bottom: 60,
        left: 80,
        width: 400
    };
    pdf.fromHTML(
    source, 
    margins.left, 
    margins.top, { 
        'width': margins.width, 
        'elementHandlers': specialElementHandlers
    },

    function (dispose) {
        pdf.save('Export.pdf');
    }, margins);
}

当表格较小时,一切正常并且pdf导出完美,但是当表格大小时,某些行会附加到新的pdf页面,但是上边距为300.这会在第二页中产生大量空白 . 请帮忙解决这个问题 .