Search code examples
c#asp.net-mvc-3pdf-generationrotativa

Displaying headers and footers in a PDF generated by Rotativa


I'm trying to specify headers and footers in a PDF generated by Rotativa library. As the author answered here it should be possible using CSS (described here). However, I'm not able to do this.

I have a stylesheet loaded in the meta tag:

<link href="print.css" rel="stylesheet" type="text/css" media="print" />

And in the stylesheet at the bottom:

@page {
    @top-left {
        content: "TOP SECRET";
        color: red
    }
    @bottom-right {
        content: counter(page);
        font-style: italic
    }
}

And then generating PDF by:

public ActionResult ShowPdf()
{
     var model = new Model();
     return new ViewAsPdf("view.cshtml", model)
                {
                    FileName = "Report.pdf",
                    CustomSwitches = "--print-media-type"
                };
}

And then nothing appears in the header and footer of the PDF. Any ideas?


Solution

  • I've found a documentation of wkhtmltopdf (or a better archived version) and it is described there how to manage headers and footers.

    Basically you can just add --header-center "text" (or similar switches) to the argument list and that's all.

    So using it with Rotativa it would be:

    public ActionResult ShowPdf()
    {
         var model = new Model();
         return new ViewAsPdf("view.cshtml", model)
                    {
                        FileName = "Report.pdf",
                        CustomSwitches = "--print-media-type --header-center \"text\""
                    };
    }
    

    (I don't know if --print-media-type is necessary.)