jsPDF multi page PDF with HTML renderer

前端 未结 11 1614
感情败类
感情败类 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:18
    $( document ).ready(function() {    
    $('#cmd').click(function() {
          var options = {
                  pagesplit: true //include this in your code
          };
          var pdf = new jsPDF('p', 'pt', 'a4');
          pdf.addHTML($("#pdfContent"), 15, 15, options, function() {
            pdf.save('Menu.pdf');
          });
        });
    });
    
    0 讨论(0)
  • 2020-11-27 14:20
             html2canvas(element[0], {
                        onrendered: function (canvas) {
                            pages = Math.ceil(element[0].clientHeight / 1450);
                            for (i = 0; i <= pages; i += 1) {
                                if (i > 0) {
                                    pdf.addPage();
                                }
                                srcImg = canvas;
                                sX = 0;
                                sY = 1450 * i;
                                sWidth = 1100;
                                sHeight = 1450;
                                dX = 0;
                                dY = 0;
                                dWidth = 1100;
                                dHeight = 1450;
                                window.onePageCanvas = document.createElement("canvas");
                                onePageCanvas.setAttribute('width', 1100);
                                onePageCanvas.setAttribute('height', 1450);
                                ctx = onePageCanvas.getContext('2d');
                                ctx.drawImage(srcImg, sX, sY, sWidth, sHeight, dX, dY, dWidth, dHeight);
                                canvasDataURL = onePageCanvas.toDataURL("image/png");
                                width = onePageCanvas.width;
                                height = onePageCanvas.clientHeight;
                                pdf.setPage(i + 1);
                                pdf.addImage(canvasDataURL, 'PNG', 35, 30, (width * 0.5), (height * 0.5));
                            }
                            pdf.save('testfilename.pdf');
                        }
                    });
    
    0 讨论(0)
  • 2020-11-27 14:24
     var doc = new jsPDF('p', 'mm');
    
     var imgData = canvas.toDataURL('image/png');
     var pageHeight= doc.internal.pageSize.getHeight();
     var pageWidth= doc.internal.pageSize.getWidth();
    
     var imgheight = $('divName').height() * 25.4 / 96; //px to mm
     var pagecount = Math.ceil(imgheight / pageHeight);
    
     /* add initial page */
     doc.addPage('l','mm','a4');
     doc.addImage(imgData, 'PNG', 2, 0, pageWidth-4, 0);
    
     /* add extra pages if the div size is larger than a a4 size */
     if (pagecount > 0) {
         var j = 1;
         while (j != pagecount) {
             doc.addPage('l','mm','a4');
             doc.addImage(imgData, 'PNG', 2, -(j * pageHeight), pageWidth-4, 0);
             j++;
         }
     }
    
    0 讨论(0)
  • 2020-11-27 14:25

    Below is my code but the problem is that the document doesn't split to display the other part of the document in a new page.

    Please improve this code.

    <script type='text/javascript'>
    $(document).on("click", "#btnExportToPDF", function () {
        var table1 =
        tableToJson($('#table1')[0]),
        cellWidth =42,
        rowCount = 0,
        cellContents,
        leftMargin = 2,
        topMargin = 12,
        topMarginTable =5,
        headerRowHeight = 13,
        rowHeight = 12,
        l = {
            orientation: 'p',
            unit: 'mm',
            format: 'a3',
            compress: true,
            fontSize: 11,
            lineHeight: 1,
            autoSize: false,
            printHeaders: true
        };
    
        var doc = new jsPDF(l,'pt', 'letter');
    
        doc.setProperties({
            title: 'Test PDF Document',
            subject: 'This is the subject',
            author: 'author',
            keywords: 'generated, javascript, web 2.0, ajax',
            creator: 'author'
        });
        doc.cellInitialize();
    
        $.each(table1, function (i, row)
        {
            rowCount++;
    
            $.each(row, function (j, cellContent) {
    
                if (rowCount == 1) {
                    doc.margins = 1;
                    doc.setFont("Times New Roman");
                    doc.setFontType("bold");
                    doc.setFontSize(11);
    
                    doc.cell(leftMargin, topMargin, cellWidth, headerRowHeight, cellContent, i)
                }
                else if (rowCount == 2) {
    
                    doc.margins = 1;
                    doc.setFont("Times ");
                    doc.setFontType("normal");
                    // or for normal font type use ------ doc.setFontType("normal");
    
                    doc.setFontSize(11);
    
                    doc.cell(leftMargin, topMargin, cellWidth, rowHeight, cellContent, i);
                }
                else {
    
                    doc.margins = 1;
                    doc.setFont("Times  ");
                    doc.setFontType("normal ");
                    doc.setFontSize(11);
                    doc.cell(leftMargin, topMargin, cellWidth, rowHeight, cellContent, i);
                    // 1st=left margin    2nd parameter=top margin,     3rd=row cell width      4th=Row height
                }
            })
        })
        doc.save('sample Report.pdf');
    });
    
    function tableToJson(table) {
    
        var data = [];
    
        // first row needs to be headers
        var headers = [];
    
        for (var i=0; i<table.rows[0].cells.length; i++) {
            headers[i] = table.rows[0].cells[i].innerHTML.toLowerCase().replace(/ /gi,'');
        }
    
        // go through cells
        for (var i=1; i<table.rows.length; i++) {
    
            var tableRow = table.rows[i];
    
            var rowData = {};
    
            for (var j=0; j<tableRow.cells.length; j++) {
                rowData[ headers[j] ] = tableRow.cells[j].innerHTML;
            }
    
            data.push(rowData);
        }
    
        return data;
    }
    </script>
    
    0 讨论(0)
  • 2020-11-27 14:29

    I have the same working issue. Searching in MrRio github I found this: https://github.com/MrRio/jsPDF/issues/101

    Basically, you have to check the actual page size always before adding new content

    doc = new jsPdf();
    ...
    pageHeight= doc.internal.pageSize.height;
    
    // Before adding new content
    y = 500 // Height position of new content
    if (y >= pageHeight)
    {
      doc.addPage();
      y = 0 // Restart height position
    }
    doc.text(x, y, "value");
    
    0 讨论(0)
  • 2020-11-27 14:30

    Automatically not split data to multi pages. You may split manually.

    If your ( rowCount * rowHeight ) > 420mm ( A3 Height in mm ) add new page function. ( Sorry I can't edit your code without run ) After add new page leftMargin, topMargin = 0; ( start over ) I added sample code with yours. I hope it's right.

    else {
        doc.margins = 1;
        doc.setFont("Times  ");
        doc.setFontType("normal ");
        doc.setFontSize(11);
        if ( rowCount * rowHeight > 420 ) {
            doc.addPage();
            rowCount = 3; // skip 1 and 2 above
        } else {
            // now rowcount = 3 ( top of new page for 3 )
            // j is your x axis cell index ( j start from 0 on $.each function ) or you can add cellCount like rowCount and replace with
            // rowcount is your y axis cell index
            left = ( ( j ) * ( cellWidth + leftMargin );
            top = ( ( rowcount - 3 ) * ( rowHeight + topMargin );
            doc.cell( leftMargin, top, cellWidth, rowHeight, cellContent, i);
            // 1st=left margin    2nd parameter=top margin,     3rd=row cell width      4th=Row height
        }
    }
    

    You can convert html directly to pdf lossless. Youtube video for html => pdf example

    0 讨论(0)
提交回复
热议问题