Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

different headers/footers for different sections? #283

Open
hujhax opened this issue May 10, 2015 · 12 comments
Open

different headers/footers for different sections? #283

hujhax opened this issue May 10, 2015 · 12 comments

Comments

@hujhax
Copy link

hujhax commented May 10, 2015

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.

@jthoenes
Copy link
Contributor

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

hujhax pushed a commit to surgeforward/pdfmake that referenced this issue May 17, 2015
@hujhax
Copy link
Author

hujhax commented May 17, 2015

Okay, I had a go at adding the feature here: surgeforward@382586f

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

@jthoenes
Copy link
Contributor

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.

@hujhax
Copy link
Author

hujhax commented May 23, 2015

Great idea!

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

@jthoenes
Copy link
Contributor

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

@hujhax
Copy link
Author

hujhax commented Jun 12, 2015

Ouch!

@bchr02
Copy link

bchr02 commented Jan 12, 2016

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

@liborm85 liborm85 changed the title different footers for different sections? different headers/footers for different sections? Jul 26, 2017
@liborm85 liborm85 mentioned this issue Dec 3, 2017
@liborm85 liborm85 added the page label Feb 4, 2018
@dushkostanoeski
Copy link

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

@simpsojo
Copy link

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.

@ditiem
Copy link

ditiem commented Jul 24, 2019

what is the state of this feature?

Any chance to help?

@MildTomato
Copy link

Any plans to get this in?

liborm85 added a commit that referenced this issue Apr 29, 2020
@liborm85 liborm85 mentioned this issue Apr 29, 2020
7 tasks
@MildTomato
Copy link

If anyone is interested, some of the functionality discussed in this thread can be achieved using this solution:
#808 (comment)

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.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

8 participants