pdfmake icon indicating copy to clipboard operation
pdfmake copied to clipboard

different headers/footers for different sections?

Open hujhax opened this issue 10 years ago • 12 comments

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.

hujhax avatar May 10 '15 07:05 hujhax

Hi, this is definitely an interesting feature - so if you have a working solution, I would like to have a look into it.

jthoenes avatar May 10 '15 08:05 jthoenes

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(" / ");
    }
};

hujhax avatar May 17 '15 01:05 hujhax

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.

jthoenes avatar May 18 '15 07:05 jthoenes

Great idea!

(There's no reason that should have the same performance hit as addPageBreaksIfNecessary [b31c3811262127abfc41ac029c62f9082a1a2c4a], right?)

hujhax avatar May 23 '15 18:05 hujhax

It's highly possible that there will be the same performance issue with that.

jthoenes avatar May 26 '15 19:05 jthoenes

Ouch!

hujhax avatar Jun 12 '15 02:06 hujhax

+1 for this but please also add it for the header section.

bchr02 avatar Jan 12 '16 16:01 bchr02

Is there a plan on merging @hujhax PR? It is a 3 years old PR , but for a very good feature...

dushkostanoeski avatar Mar 17 '18 11:03 dushkostanoeski

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,

  1. I can get access to the dd from the footer
  2. 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.

simpsojo avatar Aug 31 '18 14:08 simpsojo

what is the state of this feature?

Any chance to help?

ditiem avatar Jul 24 '19 11:07 ditiem

Any plans to get this in?

MildTomato avatar Nov 01 '19 05:11 MildTomato

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.

MildTomato avatar Sep 20 '20 05:09 MildTomato

Implemented by commit https://github.com/bpampuch/pdfmake/commit/7620919075983594e5a94aa7486b882a9063522e as new section node.

liborm85 avatar May 07 '25 13:05 liborm85

Released in version 0.3.0-beta.18. Documentation of section node is available here.

liborm85 avatar May 09 '25 08:05 liborm85