admin管理员组

文章数量:1420918

how to add margin-bottom and top at multiple pages pdf?

I added plugins jspdf and html2canvas.

               var pdf = new jsPDF( 'p', 'mm', [400, 455]);       
               var specialElementHandlers = {
                '#exportthis': function (element, renderer) {
                    return true;
                    }
                };

                margins = {
                    bottom:10,
                    top:10,
                    left:10,
                    right:10
               };


            pdf.addHTML(document.getElementById('exportthis'), 5, 10, {pagesplit: true },
                   function(dispose){
                    var pageCount = pdf.internal.getNumberOfPages(); 
                    for(i = 0; i < pageCount; i++) { 
                    pdf.setPage(i); 
                    pdf.text(195,450, pdf.internal.getCurrentPageInfo().pageNumber + "/" + pageCount +"\n");   
                 }
               pdf.save("Report.pdf");
            },margins);

output

how to add margin-bottom and top at multiple pages pdf?

I added plugins jspdf and html2canvas.

               var pdf = new jsPDF( 'p', 'mm', [400, 455]);       
               var specialElementHandlers = {
                '#exportthis': function (element, renderer) {
                    return true;
                    }
                };

                margins = {
                    bottom:10,
                    top:10,
                    left:10,
                    right:10
               };


            pdf.addHTML(document.getElementById('exportthis'), 5, 10, {pagesplit: true },
                   function(dispose){
                    var pageCount = pdf.internal.getNumberOfPages(); 
                    for(i = 0; i < pageCount; i++) { 
                    pdf.setPage(i); 
                    pdf.text(195,450, pdf.internal.getCurrentPageInfo().pageNumber + "/" + pageCount +"\n");   
                 }
               pdf.save("Report.pdf");
            },margins);

output

Share Improve this question edited Aug 3, 2018 at 13:22 Aliaksandr Pitkevich 8348 silver badges25 bronze badges asked Aug 3, 2018 at 13:19 Rushi daveRushi dave 1642 gold badges6 silver badges20 bronze badges 2
  • Have a look at this: stackoverflow./questions/46012181/… – Sookie Singh Commented Aug 3, 2018 at 13:22
  • I try this but same error and tx for response@Er_sherlockian – Rushi dave Commented Aug 3, 2018 at 13:24
Add a ment  | 

3 Answers 3

Reset to default 2

Do it like this:

pdf.addHTML(document.getElementById('exportthis'), 5, 10, {
        pagesplit: true,
        margin: margins
    },
    function(dispose) {
        var pageCount = pdf.internal.getNumberOfPages();
        for (i = 0; i < pageCount; i++) {
            pdf.setPage(i);
            pdf.text(195, 450, pdf.internal.getCurrentPageInfo().pageNumber + "/" + pageCount + "\n");
        }
        pdf.save("Report.pdf");
    });

JSPDF allows margin and useFor Feature. You can set the margin properties like below:

const pdf = new jspdf('p', 'pt', 'a4'); // For A4 Sheet layout
pdf.addHTML(document.getElementById('print-section'), 25, 50, {
    retina: true,
    pagesplit: true,
    margin: {
      top: 50,
      right: 25,
      bottom: 50,
      left: 25,
      useFor: 'page' // This property is mandatory to keep the margin to supsequent pages
    }
  }, function() {
    pdf.save('test.pdf');
  });

See the output below:

I solved this issue using changes of the addhtml.js file
https://github./MrRio/jsPDF/pull/1450/files and its work for me

本文标签: javascripthow to set top and bottom margin in addHTMLStack Overflow