Jspdf problems with addHTML printing

Asked

Viewed 520 times

0

I’m having trouble using the addHTML Jspdf, technically everything is working, but it does not print all the content that is in my div that I defined, it only prints what I can see on screen. Someone’s been through it?

let doc = new jsPDF('p','pt','a4')
        let source = document.getElementById('draft')

        let options = {
            format: 'PNG',
            pagesplit: false
        }

        doc.addHTML(source, 0, 0, options, function(){
          doc.output('dataurlnewwindow')
        })

1 answer

0


Fortunately I managed to solve my problem, follow the solution code.

$('body').css('overflow', 'visible')
            $('#app').css('overflow', 'visible')
            $('#pdfCarta').css('overflow', 'visible')
            $('#pdfCarta').css('position', 'initial')
            let a4          = [595.28, 990.89]
            $('#pdfCarta').width((595.28 * 1.33333) - 80).css('max-width', 'none');
            //$('#pdfCarta').css('transform', 'scale(2)');
            let useHeight = $('#pdfCarta').prop('scrollHeight');
            //alert(useHeight)

            let winHeight   = a4[1]
            let imagePieces = []
            let canvasImage = ''
            let formHeight  = $('#pdfCarta').height()
            let formWidth   = $('#pdfCarta').width()
            let source = document.getElementById('pdfCarta')

            imagePieces = [];
            imagePieces.length = 0;


            setTimeout(function(){
                html2canvas($('#pdfCarta')[0],{height: useHeight, useCORS: true, allowTaint: true, onrendered: canvas => {
                    canvasImage = new Image();
                    canvasImage.src = canvas.toDataURL("image/png", 0.92);
                    //window.open(canvas.toDataURL())
                    let doc = new jsPDF('p', 'pt', 'a4')

                   for (var i = 0; i <= source.clientHeight / 980; i++) {

                        var srcImg  = canvas;
                        var sX      = 0;
                        var sY      = 1120*i; 
                        var sWidth  = 778; 
                        var sHeight = 1120;
                        var dX      = 0;
                        var dY      = 0;
                        var dWidth  = 778;
                        var dHeight = 1120;

                        window.onePageCanvas = document.createElement("canvas");
                        onePageCanvas.setAttribute('width', 778);
                        onePageCanvas.setAttribute('height', 1120);
                        var ctx = onePageCanvas.getContext('2d');

                        ctx.drawImage(srcImg,sX,sY,sWidth,sHeight,dX,dY,dWidth,dHeight);

                        var canvasDataURL = onePageCanvas.toDataURL("image/png", 0.92);
                        var width         = onePageCanvas.width;
                        var height        = onePageCanvas.clientHeight;


                        if (i > 0) {
                            doc.addPage();
                        }
                        doc.setPage(i+1);
                        doc.addImage(canvasDataURL, 25, 0);


                    }
                    doc.save('draft.pdf');


                }})

            }, 1000)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.