jsPDF multi page PDF with HTML renderer

前端 未结 11 1615
感情败类
感情败类 2020-11-27 13:54

I am using jsPDF in my site to generate PDFs. But now I have multiple DIVs to print in a single PDF. which may take 2 to 3 pages.

For example:



        
相关标签:
11条回答
  • 2020-11-27 14:31

    You can use html2canvas plugin and jsPDF both. Process order: html to png & png to pdf

    Example code:

    jQuery('#part1').html2canvas({
        onrendered: function( canvas ) {
            var img1 = canvas.toDataURL('image/png');
        }
    });
    jQuery('#part2').html2canvas({
        onrendered: function( canvas ) {
            var img2 = canvas.toDataURL('image/png');
        }
    });
    jQuery('#part3').html2canvas({
        onrendered: function( canvas ) {
            var img3 = canvas.toDataURL('image/png');
        }
    });
    var doc = new jsPDF('p', 'mm');
    doc.addImage( img1, 'PNG', 0, 0, 210, 297); // A4 sizes
    doc.addImage( img2, 'PNG', 0, 90, 210, 297); // img1 and img2 on first page
    
    doc.addPage();
    doc.addImage( img3, 'PNG', 0, 0, 210, 297); // img3 on second page
    doc.save("file.pdf");
    
    0 讨论(0)
  • 2020-11-27 14:31
    var a = 0;
    var d;
    var increment;
    
    for(n in array){
        d = a++;        
    
        if(n % 6 === 0 && n != 0){
            doc.addPage();
            a = 1;
            d = 0;
        }
    
        increment = d == 0 ? 10 : 50;
        size = (d * increment) <= 0 ? 10 : d * increment;
    
        doc.text(array[n], 10, size);
    }
    
    0 讨论(0)
  • 2020-11-27 14:36

    This is my first post which support only a single page http://www.techumber.com/html-to-pdf-conversion-using-javascript/

    Now, the second one will support the multiple pages. http://www.techumber.com/how-to-convert-html-to-pdf-using-javascript-multipage/

    0 讨论(0)
  • 2020-11-27 14:42

    here's an example using html2canvas & jspdf, although how you generate the canvas doesn't matter--we're just going to use the height of that as the breakpoint on a for loop, in which a new page is created and content added to it.

    after the for loop, the pdf is saved.

    function makePDF() {
    
           var quotes = document.getElementById('container-fluid');
           html2canvas(quotes)
          .then((canvas) => {
                //! MAKE YOUR PDF
                var pdf = new jsPDF('p', 'pt', 'letter');
    
                for (var i = 0; i <= quotes.clientHeight/980; i++) {
                    //! This is all just html2canvas stuff
                    var srcImg  = canvas;
                    var sX      = 0;
                    var sY      = 980*i; // start 980 pixels down for every new page
                    var sWidth  = 900;
                    var sHeight = 980;
                    var dX      = 0;
                    var dY      = 0;
                    var dWidth  = 900;
                    var dHeight = 980;
    
                    window.onePageCanvas = document.createElement("canvas");
                    onePageCanvas.setAttribute('width', 900);
                    onePageCanvas.setAttribute('height', 980);
                    var ctx = onePageCanvas.getContext('2d');
                    // details on this usage of this function: 
                    // https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Using_images#Slicing
                    ctx.drawImage(srcImg,sX,sY,sWidth,sHeight,dX,dY,dWidth,dHeight);
    
                    // document.body.appendChild(canvas);
                    var canvasDataURL = onePageCanvas.toDataURL("image/png", 1.0);
    
                    var width         = onePageCanvas.width;
                    var height        = onePageCanvas.clientHeight;
    
                    //! If we're on anything other than the first page,
                    // add another page
                    if (i > 0) {
                        pdf.addPage(612, 791); //8.5" x 11" in pts (in*72)
                    }
                    //! now we declare that we're working on that page
                    pdf.setPage(i+1);
                    //! now we add content to that page!
                    pdf.addImage(canvasDataURL, 'PNG', 20, 40, (width*.62), (height*.62));
    
                }
                //! after the for loop is finished running, we save the pdf.
                pdf.save('Test.pdf');
            }
          });
        }
    
    0 讨论(0)
  • 2020-11-27 14:42

    I found the solution on this page: https://github.com/MrRio/jsPDF/issues/434 From the user: wangzhixuan

    I copy the solution here: // suppose your picture is already in a canvas

          var imgData = canvas.toDataURL('image/png');
    
          /*
          Here are the numbers (paper width and height) that I found to work. 
          It still creates a little overlap part between the pages, but good enough for me.
          if you can find an official number from jsPDF, use them.
          */
          var imgWidth = 210; 
          var pageHeight = 295;  
          var imgHeight = canvas.height * imgWidth / canvas.width;
          var heightLeft = imgHeight;
    
          var doc = new jsPDF('p', 'mm');
          var position = 0;
    
          doc.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
          heightLeft -= pageHeight;
    
          while (heightLeft >= 0) {
            position = heightLeft - imgHeight;
            doc.addPage();
            doc.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight);
            heightLeft -= pageHeight;
          }
          doc.save( 'file.pdf');
    
    0 讨论(0)
提交回复
热议问题