node-html-pdf icon indicating copy to clipboard operation
node-html-pdf copied to clipboard

Question: Set Page Headers Without Knowing Number of Pages

Open abouillon opened this issue 10 years ago • 9 comments

Is there a way to set a page header on all PDF pages except the first page without knowing how many pages the resulting file will have?

abouillon avatar May 18 '16 13:05 abouillon

Yes. just set add some html to your page:

<div id="pageHeader">Content on all pages</div>
<div id="pageHeader-first"></div>

marcbachmann avatar May 18 '16 13:05 marcbachmann

I do that, but I have no idea how many pages will be generated by the PDF itself. Each file will have a different number of pages. I need to skip the first page and set the id to go from pageHeader-2 all the way to pageHeader-n. Is this possible without knowing the number of pages that will be generated?

abouillon avatar May 18 '16 13:05 abouillon

<div id="pageHeader">Content on all pages</div>

Is the default one. You don't need to enumerate everything. That will be used as fallback in case pageHeader-${index} isn't present.

marcbachmann avatar May 18 '16 13:05 marcbachmann

Do you need more? Maybe I misunderstood your problem.

marcbachmann avatar May 18 '16 13:05 marcbachmann

Using pageHeader-${index} works, but I cannot hard code a set number for the index itself because I do not know how many pages will be generated. For example, I can generate one PDF with 5 pages and then another PDF that will have 20 pages. I would need to be able to handle both of those without knowing for sure how many pages will be generated. Currently the only option I can think of is to maybe use some Javascript on the template to generate the markups up to a very large number in order to catch it.

abouillon avatar May 18 '16 13:05 abouillon

You can use both together. Then all the pages use the default header and page 1 uses the empty one. It should work like that.

<body>
  <div id="pageHeader-1">Header of first page</div>
  <div id="pageHeader">Fallback header for all other pages</div>
  <h1>Some content</h1>
  <h1 style="page-break-before: always;">Second page content</h1>
  <h1 style="page-break-before: always;">Third page content</h1>
</body>

marcbachmann avatar May 18 '16 14:05 marcbachmann

That does not work. The markup for the header on all other pages displays on the first page as well.

abouillon avatar May 18 '16 14:05 abouillon

Then that's a bug. That should be handled in here: https://github.com/marcbachmann/node-html-pdf/blob/master/lib/scripts/pdf_a4_portrait.js#L152-L157

marcbachmann avatar May 18 '16 14:05 marcbachmann

That does handle it, but not in the way I was expecting it to be handled. What happens is that the content of the element gets displayed on the proper page, but the empty element gets display on all of the other pages. I was expecting that the element would be removed or set to display: none on subsequent pages.

abouillon avatar May 18 '16 15:05 abouillon