JSPDF本地保存的文件使Acrobat产生文档[110]错误

时间:2016-12-02 19:17:53

标签: 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在JSFiddle中,因为我无法将其粘贴到此处,我收到错误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

3 个答案:

答案 0 :(得分:3)

我遇到了同样的问题。升级到jsPDF 1.3.4解决了它。

答案 1 :(得分:1)

jspdf debug的版本非常重要。

<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>

答案 2 :(得分:1)

我遇到了同样的问题,我添加了函数的所有参数。

// 设置PDF

  1. let doc = new jsPDF('p', 'mm', [297, 210]);
  2. doc.addImage(dataURL, "JPEG", marginPDF, 15, widthImgPDF, heigthImgPDF, "fotoX", "SLOW");
  3. doc.save("消防检查 - " + questID + ".pdf");