Jspdf: Dynamically Create Header or Footer

Created on 13 Jul 2017  Â·  6Comments  Â·  Source: MrRio/jsPDF

Has anyone been able to dynamically create a header on every single page or a footer with page a current/total page number?

Most helpful comment

Well... I guess you can.

you have the Methods

  • getNumberOfPages
  • getCurrentPageInfo which returns an object with the property pageNumber
  • setPage

So putting them together:

var doc = jsPDF();
doc.setFontSize(12);
doc.addPage();
doc.addPage();
doc.addPage();
doc.addPage();
doc.addPage();
doc.addPage();
var pageCount = doc.internal.getNumberOfPages();
for(i = 0; i < pageCount; i++) { 
doc.setPage(i); 
doc.text(10,10, doc.internal.getCurrentPageInfo().pageNumber + "/" + pageCount);
}

test it by copy and pasting it here https://parall.ax/products/jspdf

All 6 comments

Well... I guess you can.

you have the Methods

  • getNumberOfPages
  • getCurrentPageInfo which returns an object with the property pageNumber
  • setPage

So putting them together:

var doc = jsPDF();
doc.setFontSize(12);
doc.addPage();
doc.addPage();
doc.addPage();
doc.addPage();
doc.addPage();
doc.addPage();
var pageCount = doc.internal.getNumberOfPages();
for(i = 0; i < pageCount; i++) { 
doc.setPage(i); 
doc.text(10,10, doc.internal.getCurrentPageInfo().pageNumber + "/" + pageCount);
}

test it by copy and pasting it here https://parall.ax/products/jspdf

That would not work if say something extends multiple pages. Then your
pageNumber count would be off would it not?

On Tue, Aug 1, 2017 at 5:06 AM, Aras Abbasi notifications@github.com
wrote:

Well... I guess you can.

you have the Methods

  • getNumberOfPages
  • getCurrentPageInfo which returns an object with the property
    pageNumber
  • setPage

So putting them together:

var doc = jsPDF();
doc.setFontSize(12);
doc.addPage();
doc.addPage();
doc.addPage();
doc.addPage();
doc.addPage();
doc.addPage();
var pageCount = doc.internal.getNumberOfPages();
for(i = 0; i < pageCount; i++) {
doc.setPage(i);
doc.text(10,10, doc.internal.getCurrentPageInfo().pageNumber + "/" + pageCount);
}

test it by copy and pasting it here https://parall.ax/products/jspdf

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/MrRio/jsPDF/issues/1340#issuecomment-319313368, or mute
the thread
https://github.com/notifications/unsubscribe-auth/ANmxwUK3W1mqSMTIDKyhNCh7Y5i-yxXbks5sTup6gaJpZM4OXJNA
.

These are API functions. If somebody would modify/extend jspdf with breaking these functions,then it is anyway violating the rules of good coding. And if they would break the functionality the pdf generation would be broken, because the pagenumber is used for it.

I wrote a template plugin.

1411

If you want to see how to use it check the test-file

https://github.com/arasabbasi/jsPDF/blob/plugin/simpleTemplateEngine/tests/template/standard.spec.js

Thanks arasabbasi

Shouldn't these methods show up in the documentation?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tarekis picture tarekis  Â·  4Comments

MelanieCroce picture MelanieCroce  Â·  4Comments

NoFootDancer picture NoFootDancer  Â·  3Comments

mellisa0109 picture mellisa0109  Â·  3Comments

MaxCodeDE picture MaxCodeDE  Â·  4Comments