Search code examples
c#pdf-generationnreco

Use a global css style for a pdf document with separate header and footer generated with NReco Html-To-Pdf Generator


I'm using NReco HtmlToPdf Generator to generate complex documents, and I need to use repeatable header and footer capabilities on the pdf. Setting the header and footer using ".PageHeaderHtml" and ".PageFooterHtml" properties works, but headers and footers are not styled properly, following CSS rules included in the document (I'm also using Google fonts). It works if I duplicate the css rules and references in the header html and in the footer html (and obviously in the body html). Is there any other way to write the css and references only once? Thanks


Solution

  • Wkhtmltopdf (which is internally used by NReco PdfGenerator) renders header, footer and main content as independent HTML documents. This means that CSS rules from main HTML document are not shared with header/footer templates. You can extract common CSS rules into separate CSS file and reference it from HTML documents where they are needed. If you have no control over main HTML content you can link extra CSS with "--user-style-sheet " command line option.