2016-12-02 96 views
2

我有以下JavaScript代码https://jsfiddle.net/d72sgwrc/5/它假设保存我的屏幕图像将其转换为Canvas并将其保存为PDF。一旦文件被下载到本地机器上,我就能够在浏览器中查看PDF文件,但是如果我想在Acrobat PDF查看器上打开文件,则会出现以下错误:“出现错误处理页面。出现问题准备好这个文件(110)。我的HTML页面仅仅是一个一群生成的数字的表格。JSPDF本地保存的文件给Acrobat文档[110]错误

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

+0

懒洋洋地将你的代码复制到小提琴并粘贴链接并不酷。你遗漏了最后一个'}',你没有包含jQuery或其他你正在使用的库,并且你的代码没有运行。我更新了你的小提琴,但由于你太懒惰而没有做好,所以我懒得帮你。祝你好运。 –

+0

@Iwrestledabearonce。不,我为此感到抱歉。我试图让代码在这里,但它给我错误。所以我把它复制到jsfiddle中。我试着重新编辑这个 –

+0

@Iwrestledabearonce。我再次道歉,我修复了它 –

回答

1

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

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