Search code examples
pdfheaderconvertersfooterwkhtmltopdf

WkHtmlToXSharp How to add Headers and Footers?


I'm building an HTML to PDF converter with the WkHtmlToXSharp (QT webkit) library, and was wondering if someone knows how to add headers and footers to the document? I've seen a few questions about this library here, but couldn't find anything about the headers and footers.

In the wkhtmltopdf manual (http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf-0.9.9-doc.html) there are documentation about headers and footers, but I couldn't find anything in the .NET wrapper library (WkHtmlToXSharp), it is probably not implemented?

Thanks for any help or suggestions!


Solution

  • Here are some snippets of how I do it:

    public class HeaderFooterSettings
    {
        public string HtmlUrl { get; set; }
        public string Right { get; set; }
        public string Spacing { get; set; }
    }
    
    public class PdfObjectSettings
    {
        private WebSettings _webSettings = new WebSettings();
        private LoadSettings _loadSettings = new LoadSettings();
        private HeaderFooterSettings _headerSettings = new HeaderFooterSettings();
        private HeaderFooterSettings _footerSettings = new HeaderFooterSettings();
    
        public string Page { get; set; }
        public string Cover { get; set; }
        public bool ProduceForms { get; set; }
        public bool PagesCount { get; set; }
    
    
        public HeaderFooterSettings Header { get { return _headerSettings; } }
        public HeaderFooterSettings Footer { get { return _footerSettings; } }
        public WebSettings Web { get { return _webSettings; } }
        public LoadSettings Load { get { return _loadSettings; } }
    
    
        // TODO: Add remaining settings..
        //see the following page for settings format http://www.cs.au.dk/~jakobt/libwkhtmltox_0.10.0_doc/pagesettings.html
    }
    

    Here is the main logic of setting the header and footer URL:

    var objectSettings = new PdfObjectSettings();
    objectSettings.Header.HtmlUrl = headerHtmlUrl;
    objectSettings.Header.Spacing = ConfigurationManager.AppSettings["ContentSpacing"];
    objectSettings.Footer.HtmlUrl = footerHtmlUrl;
    

    I hope this helps.

    Rafi