Search code examples
c#asp.net-corewkhtmltopdfnreco

How to bind dynamic values in the header and how to hide header for the specific pages?


I am using wkhtmltopdf (0.12.6). I cannot achieve the following two things.

  • I want to bind dynamic values in the header based on the content.
  • I want to hide headers based on the pages.

When I use the wkhtmltopdf, I can inject only one header html. I don't know how to do multiple page header or how to do change the header value.


Solution

  • In wkhtmltopdf page header/footer are rendered as separate HTML documents. This means that they don't have direct access to the 'current page' content; however, special 'tokens' - like current page number, total number of pages, current 'section' - are passed as query parameters and they are accessible with JS inside header/footer HTML templates.

    Take a look to this wkhtmltopdf help ("Footers And Headers" section): https://wkhtmltopdf.org/usage/wkhtmltopdf.txt

    You can paste similar block and customize (or even hide) header content depending on the page number (or current 'section' - in wkhtmltopdf this corresponds to the value of 'last' / / tags). Note that you can hide the content, but height of the header/footer (allocated space on the page) will be the same on all pages because wkhtmltopdf calculates the necessary margin only once (for the first page with the header) and then use the same value for all next pages.