Pdfmake: Different page headers and footers

Created on 25 Jan 2015  路  14Comments  路  Source: bpampuch/pdfmake

Is there way to make different headers and footers on pages?
For example: fist document section with one page header, second - with another?
Something like this:
content: [
{header: "one"},
.....
{header: "two"},
]
That's should change header on current page.
In current version header description is placed outside "content", so I can describe it only once.

feature request

Most helpful comment

I've been stucked for weeks knowing what I should do, and I just realized that what we're tweaking here is at .js, meaning you can do programming stuff here.

Here is the template on making your first page header/footer different on the other pages:

header: function(page) { 
          if (page != 1) 
                  return {text: 'Hello!'}
              else
                      return {text: 'Hi!'}
}

if you want to put more stuff on the header/footer:

footer: function(page) { 
          if (page != 1){
             return { columns: [
                    {text: '[LOGO OF CLIENT HERE]'},
                    {image: 'sampleImage.jpg', alignment: 'right', width: 50}
             ]}         
          }
}

Play it here!: http://pdfmake.org/playground.html

_NOTE: The background function is the same as the header, and the currentPage parameter is also the same as the page parameter._

I'll just post this thing incase someone stucked like me! :D

All 14 comments

Hi @gnibeda, in the TW-QEN/pdfmake repository we are currently working on a section/subsection concept. Maybe we can do something like this

{
  footer: function(page, pages, headlines) {
     // headlines is an array of pages on that page
  }
}

Would that help you?

The main problem is that I don't know on which page footer should be. Footer is changed during content. Simply put, in one place of content I need new footers. But I don't know page number before document will be generated, because content length is varying. That's why I can't use callbacks for this.

I think the simplest way would be allowed to place footer inside content(and engine should use this footer on page where it was found and on all after, till then new footer was found).

It concerns not only headers and footers. Also we should be able to change page orientation, page margins during content description. (For example: I need one landscape page in portrait document)

Hi @gnibeda ,

Page headers and footers currently work a bit different in pdfmake. I've currently no good way to do it. But maybe you want to work on a pull request?

As for pageOrientation. See at pull request #180. This would allow you to specify a pull request like this:

{  text: 'Text on landcape', pageBreak: 'before', pageOrientation: 'landscape'}

This page margins are currently not included in there, but easily extendable.

Now I working on simple html to pdf parser and almost done it. May be we should develop plugin support, so I can include it in pdfmake? After that I'll look at problem with footers and headers.

For the html parser, please add your thoughts to #205

Whats the state of this issue? I have also an request.

I want to design the first page of the PDF a little bit different to the other pages.

I am also curious if this is still in progress. I have a two page pdf I am generating but the headers, footers, and background between the 1st and 2nd pages have to be different. thanks!

I've been stucked for weeks knowing what I should do, and I just realized that what we're tweaking here is at .js, meaning you can do programming stuff here.

Here is the template on making your first page header/footer different on the other pages:

header: function(page) { 
          if (page != 1) 
                  return {text: 'Hello!'}
              else
                      return {text: 'Hi!'}
}

if you want to put more stuff on the header/footer:

footer: function(page) { 
          if (page != 1){
             return { columns: [
                    {text: '[LOGO OF CLIENT HERE]'},
                    {image: 'sampleImage.jpg', alignment: 'right', width: 50}
             ]}         
          }
}

Play it here!: http://pdfmake.org/playground.html

_NOTE: The background function is the same as the header, and the currentPage parameter is also the same as the page parameter._

I'll just post this thing incase someone stucked like me! :D

Yes, there is a workaround to make this work. I'm using angularjs and using a scope variable this is kind of possible to achieve.

I've noticed that the header function runs first than the footer function, and these functions are called only after the pages are generated, so you can iterate through dd.contents and retrieve any propertie from each node, including the pages that they are located.

So, if you give an id to an object in content, you can locate it's page when the header function runs for the first time and store it in a scope variable, like:

If (dd.content[i].id) $scope.breakPoints.push(
{
page: dd.content[i].positions[0].pageNumber,
id: dd.content[i].id
}
)

Than, based on the constructed scope variable, you can run filters to see where the currentPage is and with a custom function, based on the id name (and stored in an angular service), you can define different headers and footers for different page ranges.

I think this feature should be available out of the box, and I'm willing to implement it myself sometime, but this trick worslk perfectly. If not with angular, just push the objects into a global variable.

Hi @FelipeMicali. In the header you only get page count, current page and page Size; how are you getting the document definition object and the nodes? Have you changed pdfMake to pass that down (which is a reasonable approach)? Thanks!

Thanks @eldesgonzales, that is exactly what I needed to remember! Now I can control headers and footers by page, and if I put in a little more effort probably allow clients to set the dynamic content as well. Cheers!

Is there any way to get the current page node details in footer function?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Christian24 picture Christian24  路  3Comments

michaelqiji picture michaelqiji  路  3Comments

einfallstoll picture einfallstoll  路  3Comments

CharlyPoppins picture CharlyPoppins  路  3Comments

jokris1 picture jokris1  路  3Comments