different headers/footers for different sections?
I stumbled into something that might be a useful feature to add.
For this project I'm working on, different sections of the pdf document need to have different footers. Unfortunately, I have no way of knowing which section starts on which page, so the page-number parameter given to the footer method is no help to me. :(
I ended up adding a property called "footerMode" to certain nodes, as a way of letting pdfMake know which section I was in. Then, in processNode, I kept track of the "current footerMode", re-setting it whenever I hit a node with footerModeset. Meanwhile, I added a footerMode property to the pages, and I kept setting the current page's footerMode to that "current footerMode" value. Finally, in addDynamicRepeatable, I passed the current page's footerMode down from addDynamicRepeatable into nodeGetter as a third parameter, thus giving the footer method access to it.
There are still some flaws to this approach -- I suspect it's not the cleanest code, it adds yet another property onto the pages array-items, and it doesn't account for multiple overlapping sections. But maybe this could be the basis of a more general "section" property? Or have I already overlooked a feature like this that's already there?
Let me know if it would be worth it to set up a pull request for this.
Hi, this is definitely an interesting feature - so if you have a working solution, I would like to have a look into it.
Okay, I had a go at adding the feature here: https://github.com/surgeforward/pdfmake/commit/382586f122dc0855ce47dba61beed4380b79fa27
I tried to make a test for it, but haven't been able to quite wrap my mind around the testing suite yet. FWIW, here is a document that will use the feature:
var outputItems = [];
for (var i = 1; i<100; i++) {
outputItems.push({
text: "Number " + i,
style: "largeText",
footerMode: (i<50) ? 'before' : 'after'
});
}
var doc = {
content: outputItems,
header: "Text",
styles: {
largeText: {
fontSize: 30,
bold: true
}
},
footer: function(pageNo, totalPages, footerMode) {
return [pageNo, totalPages, footerMode].join(" / ");
}
};
I would prefer to pass the nodes of this page (similar to the pageBreakBefore function) to the header and footer function. This way are much more flexible.
Great idea!
(There's no reason that should have the same performance hit as addPageBreaksIfNecessary [b31c3811262127abfc41ac029c62f9082a1a2c4a], right?)
It's highly possible that there will be the same performance issue with that.
Ouch!
+1 for this but please also add it for the header section.
Is there a plan on merging @hujhax PR? It is a 3 years old PR , but for a very good feature...
I created a custom footer that resets page numbering each time I start a new section (which is always on a new page). I was able to achieve that using the current prod release (no fork). In naxmefy 's answer to #1441 I observed that,
- I can get access to the dd from the footer
- by the time I read the dd in the footer the dd has been modified such that each object has a positions property (nested) that shows which page that object landed on
With the above I was able to find the last section that landed on a page no higher than the current page number. Then I just deduct that page number from my current page number to get the section's page number. A bit too complicated/specific (mixed in with Angular) for me to pull together a sample, but hopefully this helps.
what is the state of this feature?
Any chance to help?
Any plans to get this in?
If anyone is interested, some of the functionality discussed in this thread can be achieved using this solution: https://github.com/bpampuch/pdfmake/issues/808#issuecomment-272914212
Obviously it would be ideal if it's officially supported, but the above link is a pretty good way to get dynamic footers/headers in.
Implemented by commit https://github.com/bpampuch/pdfmake/commit/7620919075983594e5a94aa7486b882a9063522e as new section node.
Released in version 0.3.0-beta.18.
Documentation of section node is available here.