我有一个调用html2canvas的函数。如果画布只有1个pdf“页面”大,则它没有问题。如果它大于此值,则我执行以下while语句将画布分成较小的画布,然后将其一次添加到PDF中。

它适合一页我在PDF中生成大约5个“页面”的页面使用,但是我有另一个页面,它可能比不会生成的页面大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' );
        }
    } );
}

最佳答案

事实证明,我的选择器超出了画布的最大大小,因此被渲染为空白。经过一番挖掘后,最大尺寸(镀铬)为268,435,456像素。我尝试创建的div略超过270,000,000像素。

我的解决方案是在前半部分添加firstHalf类,在后半部分添加secondHalf,然后显示替代项。
然后,我在传入jsPDF doc时调用了exportHTMLToPDF两次,以便它将使用相同的。

以更系统的方式,可以用数字代替隐藏多个“部分”。
.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 );
            }
        }
    } );
}

10-05 20:29
查看更多