JSPDF本地保存的文件在Acrobat中显示文档[110]错误

人气:62 发布:2023-01-03 标签: javascript pdf-generation jspdf

问题描述

我有以下JavaScript代码https://jsfiddle.net/d72sgwrc/5/,它假定要保存我的屏幕图像,将其转换为Canvas,并将其保存为PDF。一旦文件下载到我的本地计算机上,我就可以在浏览器中查看PDF文件,但如果我想在Acrobat PDF查看器上打开该文件,我会收到以下错误:"处理页面时出错。准备此文档时出现问题(110)。我的HTML页面只是一个包含大量生成编号的表格。

JS

function exportPDF() {
var pdf = new jsPDF('l', 'px'),
    source = $('body')[0];
var canvasToImage = function(canvas) {
    var img = new Image();
    var dataURL = canvas.toDataURL('image/png');
    img.src = dataURL;
    return img;
};
var canvasShiftImage = function(oldCanvas, shiftAmt) {
    shiftAmt = parseInt(shiftAmt) || 0;
    if (!shiftAmt) {
        return oldCanvas;
    }

    var newCanvas = document.createElement('canvas');
    newCanvas.height = oldCanvas.height - shiftAmt;
    newCanvas.width = oldCanvas.width;
    var ctx = newCanvas.getContext('2d');

    var img = canvasToImage(oldCanvas);
    ctx.drawImage(img, 0, shiftAmt, img.width, img.height, 0, 0, img.width, img.height);

    return newCanvas;
};


var canvasToImageSuccess = function(canvas) {
    var pdf = new jsPDF('l', 'px'),
        pdfInternals = pdf.internal,
        pdfPageSize = pdfInternals.pageSize,
        pdfScaleFactor = pdfInternals.scaleFactor,
        pdfPageWidth = pdfPageSize.width,
        pdfPageHeight = pdfPageSize.height,
        totalPdfHeight = 0,
        htmlPageHeight = canvas.height,
        htmlScaleFactor = canvas.width / (pdfPageWidth * pdfScaleFactor),
        safetyNet = 0;

    while (totalPdfHeight < htmlPageHeight && safetyNet < 15) {
        var newCanvas = canvasShiftImage(canvas, totalPdfHeight);
        pdf.addImage(newCanvas, 'png', 0, 0, pdfPageWidth, 0, null, 'NONE');

        totalPdfHeight += (pdfPageHeight * pdfScaleFactor * htmlScaleFactor);

        if (totalPdfHeight < htmlPageHeight) {
            pdf.addPage();
        }
        safetyNet++;
    }

    pdf.save('test.PDF');
};
html2canvas(source, {
    onrendered: function(canvas) {
        canvasToImageSuccess(canvas);
    }
});
}

    exportPDF();

由于我无法将其粘贴到此处,因此Html在JSFdle中,出现错误https://jsfiddle.net/d72sgwrc/5/

我正在使用以下JS库 https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.debug.js

https://fastcdn.org/FileSaver.js/1.1.20151003/FileSaver.min.js

https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.min.js

https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js

推荐答案

jspdf调试的版本非常重要。

<script src="https://code.jquery.com/jquery-3.1.1.js"></script>
    <script src="https://code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.debug.js"></script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
        <script src="https://cdn.rawgit.com/MrRio/jsPDF/master/libs/png_support/png.js"></script>
        <script src="https://cdn.rawgit.comMrRio/jsPDF/master/plugins/addimage.js"></script>
        <script src="https://cdn.rawgit.com/MrRio/jsPDF/master/libs/png_support/zlib.js"></script>

18