Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsPDF - Print current pagenumber in footer of all pages

Tags:

jspdf

Im trying to print the current page numbers using this: https://github.com/MrRio/jsPDF/pull/260

But jsPDF renders a huge amount of blank pages and crashes :(

Without the footer, every thing works fine and I get a nice PDF with 27 pages, but without footers ofcause

Console errors:

my footer is:

<footer>
    <div style='text-align:center;'>Page <span class="pageCounter"></span>/<span class="totalPages"></span></div>
</footer>

and heres my Jquery part:

var doc = new jsPDF();
var margins = {
  top: 10,
  left: 10,
  right: 10,
  bottom: 20,
  width: 265
};

doc.setProperties({
 title: 'Title',
 subject: 'This is the subject',
 author: 'Author Name',
 keywords: 'generated, javascript, web 2.0, ajax',
 creator: 'Creator Name'
});

length = doc.internal.getNumberOfPages()

doc.fromHTML(response, margins.left, margins.top,{
    'width': margins.width // max width of content on PDF
},
function(){
    doc.save('noter.pdf');
}, margins);
like image 389
Jonas Borneland Avatar asked Oct 29 '25 14:10

Jonas Borneland


1 Answers

You can use this code :

enter image description here

var doc = jsPDF();
doc.addPage();
doc.addPage();
doc.addPage();
doc.addPage();

var pageCount = doc.internal.getNumberOfPages(); //Total Page Number
for(i = 0; i < pageCount; i++) { 
  doc.setPage(i); 
  let pageCurrent = doc.internal.getCurrentPageInfo().pageNumber; //Current Page
  doc.setFontSize(12);
  doc.text('page: ' + pageCurrent + '/' + pageCount, 10, doc.internal.pageSize.height - 10);
}
like image 172
mounirboulwafa Avatar answered Oct 31 '25 10:10

mounirboulwafa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!