jsPDF Tutorial to Add Footer and Page Numbers to PDF Document Using Loop in Javascript Full Project For Beginners

 

 

 

You have to implement it yourself. You can do something like this:

var doc = new jsPDF();
doc.page=1; // use this as a counter.

function footer(){ 
    doc.text(150,285, 'page ' + doc.page); //print number bottom right
    doc.page ++;
};

// and call footer() after each doc.addPage()

 

 

 

It’s work for me:

I just put coordenates for A4 Paper;

Just add the for before doc.save() like this;

// Create a document

var doc = new jsPDF('p','mm','a4');

// Some stuff
doc.text("Some text", 74, 150);
doc.addPage();
doc.text("Some text", 74, 150);
doc.addPage();
doc.text("Some text", 74, 150);
doc.addPage();
doc.text("Some text", 74, 150);
doc.addPage();
doc.text("Last page", 74, 150);

// PAGE NUMBERING
// Add Page number at bottom-right
// Get the number of pages
const pageCount = doc.internal.getNumberOfPages();

// For each page, print the page number and the total pages
for(var i = 1; i <= pageCount; i++) {
     // Go to page i
    doc.setPage(i);
     //Print Page 1 of 4 for example
    doc.text('Page ' + String(i) + ' of ' + String(pageCount),210-20,297-30,null,null,"right");
}

// Save the doc
doc.save('test.pdf');

Leave a Reply