为什么 jsPDF 在大文档上只输出空白页?

Why is jsPDF outputing only blank pages on larger documents?

我有一个调用 html2canvas 的函数。如果 canvas 只有 1 个 pdf 'page' 大,则没有问题。如果它大于我所做的 while 语句将 canvas 分解成更小的 canvases 然后我一次向 PDF 添加 1。

它非常适用于我拥有的一页,它在 PDF 中生成了大约 5 'pages',但我有一个不同的页面,该页面可能大 3 倍,但不会生成,这令人非常沮丧。

正在输出 PDF,但如果有任何数据,它完全是空白的,页数正确。 为什么它是空白的,我怎样才能输出正确的信息?我已经在墙上撞了一段时间了,任何帮助将不胜感激。

function exportHTMLToPDF( selector, pdfData ){
    html2canvas( $( selector ), {
        onrendered: function ( canvas ) {
            //Size variables
            var posFromTop = 0; //The position from the top
            var canvasLeft; 
            // width, height, and ratio (w x h) of a canvas 'page'
            var pageWidth, pageHeight, pageRatio;
            //Upright oritentation (portrait)
            if ( pdfData.orientation == 'p' ) {
                pageRatio = 0.77272727272;
            } else { //Landscape orientation
                pageRatio = 1.29411764706;
            }
            pageWidth = canvas.width;
            pageHeight = pageWidth / pageRatio;
            canvasLeft = canvas.height * pageWidth / pageWidth;
            //Set up the pdf
            var doc = jsPDF( pdfData.orientation, 'mm', [pageWidth, pageHeight] );
            //Get canvas context
            var ctx = canvas.getContext( '2d' );
            //Set that this is the first page so doc.addPage() isn't called
            var firstPage = true;
            //Get the next 'page'
            var imgData;
            //Create a second canvas to put the imgData in
            var c2 = document.createElement( 'canvas' );
            c2.setAttribute( 'width', pageWidth );
            c2.setAttribute( 'height', pageHeight );
            //Get a second canvas context to put the image on
            var ctx2 = c2.getContext( '2d' );
            //How many pages the pdf document will be
            var pages = canvasLeft / pageHeight;
            var imageURI;
            //While there are still more pages
            while ( pages >= 0 ) {
                //Convert the next part of the canvas to image data
                imgData = ctx.getImageData( 0, posFromTop, pageWidth, pageHeight );
                //Change the transparancy into white space
                for ( var i = 0; i < imgData.data.length; i += 4 ) {
                    if ( imgData.data[i + 3] == 0 ) {//If transparent
                        imgData.data[i] = 255;
                        imgData.data[i + 1] = 255;
                        imgData.data[i + 2] = 255;
                        imgData.data[i + 3] = 255;
                    }
                }
                //Put the image on the canvas
                ctx2.putImageData( imgData, 0, 0 );
                //If the document needs another page, add one
                firstPage ? firstPage = false : doc.addPage();

                //Turn the canvas with the newest image data into DataURL and put that 
                //on the pdf, with compression (the 'FAST' part)
                doc.addImage( c2.toDataURL( 'image/png' ), 'PNG', 0, 0, pageWidth, pageHeight, null, 'FAST' );
                pages--;
                posFromTop += pageHeight;
            }
            //Saves the pdf
            doc.save( pdfData.fileName + '.pdf' );
        }
    } );
}

事实证明,我的选择器超出了 canvas 的最大尺寸,因此呈现为空白。经过一些挖掘后,最大尺寸(在 chrome 中)为 268,435,456 像素区域。我尝试创建的 div 略高于 270,000,000 像素。

我的解决方案是在上半部分添加 class firstHalf,在下半部分添加 secondHalf,然后交替显示。
然后,我在传入 jsPDF doc 时两次调用 exportHTMLToPDF,以便它使用同一个。

以更系统的方式隐藏多个 "parts" 可以用数字代替。 .part1.part2 等,然后用传入的变量遍历它们。

function exportHTMLToPDF( selector, pdfData, doc ){
    var currentPart = pdfData.curPart;
    var lastPart = pdfData.lastPart;
    html2canvas( $(selector), {
        onrendered: function (canvas){
            //**Get page size data**//
            if( doc == null ){
                doc = jsPDF( 'p', 'mm', [pageWidth, pageHeight] );
            }
            //**Add the current page to the doc**//
            //If this is the last page, save
            if( currentPart == lastPart ){
                doc.save( pdfData.fileName + '.pdf' );
            } else {
                //Else call this function again, for the next page
                $('.part' + currentPart ).hide();
                $('.part' + ( currentPart + 1 ).show();
                pdfData.curPart = currentPart + 1;
                exportHTMLToPDF( selector, pdfData, doc );
            }
        }
    } );
}