Jspdf: How do I add page numbering to a jsPDF document

Created on 18 Jun 2013  Â·  7Comments  Â·  Source: MrRio/jsPDF

How do I add page numbering to a jsPDF document? Is there any API avaialble.

Most helpful comment

I ended up using the above doc.internal.getNumberOfPages() by adding this code just before outputting/saving the pdf. So that there is no need to keep track of added pages during pdf creation when for example using autoTables.

const pages = doc.internal.getNumberOfPages();
const pageWidth = doc.internal.pageSize.width;  //Optional
const pageHeight = doc.internal.pageSize.height;  //Optional
doc.setFontSize(10);  //Optional

for (let j = 1; j < pages + 1 ; j++) {
      let horizontalPos = pageWidth / 2;  //Can be fixed number
      let verticalPos = pageHeight - 10;  //Can be fixed number
      doc.setPage(j);
      doc.text(`${j} of ${pages}`, horizontalPos, verticalPos, {align: 'center'  //Optional text styling});
}

// Then save
doc.save('thePDF.pdf');

All 7 comments

hi,
actually, I proceed like that:

var doc = new jsPDF();
doc.page=1;

// then use this as a counter.

function footer(){
doc.text(150,285, 'page ' + doc.page);
doc.page ++;
};

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

Hope it helped,
FC

Thanks so much. Actually I ended up doing the same :-). Just didn't get a chance to reply back to the thread.

Thanks so much for getting back to me on this

Sg

Sent from my iPhone

On Jun 25, 2013, at 9:56 AM, FlyingC [email protected] wrote:

hi,
actually, I proceed like that:

var doc = new jsPDF();
doc.page=1;

// then use this as a counter.

function footer(){
doc.text(150,285, 'page ' + doc.page);
doc.page ++;
};

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

—
Reply to this email directly or view it on GitHub.

You're welcome.

I'm trying to add a new page as the first page fill up. I have 3 textareas separated by "\n\n". The second textarea has the potential to be really long. When I create the pdf, the text simply goes to the end of the first page and stops. Help please!

Is there any way to add total number of pages to the header? Like 1/100, 2/100, ..., 100/100?

Yes, with "doc.internal.getNumberOfPages()"

I ended up using the above doc.internal.getNumberOfPages() by adding this code just before outputting/saving the pdf. So that there is no need to keep track of added pages during pdf creation when for example using autoTables.

const pages = doc.internal.getNumberOfPages();
const pageWidth = doc.internal.pageSize.width;  //Optional
const pageHeight = doc.internal.pageSize.height;  //Optional
doc.setFontSize(10);  //Optional

for (let j = 1; j < pages + 1 ; j++) {
      let horizontalPos = pageWidth / 2;  //Can be fixed number
      let verticalPos = pageHeight - 10;  //Can be fixed number
      doc.setPage(j);
      doc.text(`${j} of ${pages}`, horizontalPos, verticalPos, {align: 'center'  //Optional text styling});
}

// Then save
doc.save('thePDF.pdf');
Was this page helpful?
0 / 5 - 0 ratings

Related issues

MelanieCroce picture MelanieCroce  Â·  4Comments

MaxCodeDE picture MaxCodeDE  Â·  4Comments

glaier picture glaier  Â·  3Comments

mellisa0109 picture mellisa0109  Â·  3Comments

mackersD picture mackersD  Â·  4Comments