PDF Headers and Footers as Views

date Mar 30, 2016

With RotativaHQ, starting from version 1.3.0, You can create PDF docs with nice headers and footers simply by defining a parameter on ViewAsPdf:

public ActionResult MyAction()
{
    return new ViewAsPdf()
    {
        HeaderView = "MyPdfHeader",
        FooterView = "MyPdfFooter"
    };
}

The header and footer view should be complete views (not partial). They can contain anything you could put in a normal view, @ViewBag variables, the @Model shared with the main ViewAsPdf, images, css files. Anything useful to customize it and give to your PDF files a professional look.

Header and Footer may also contain special tokens that will be replaced at PDF construction time. For now we have implemented the possibility to add the page number and the total page count. To insert them in footers and headers you should simply add [page] to get the current page number and [topage] to get the total page count. A sample header or footer view could look like this:

@{ Layout = null; }
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>HeaderTestHeader</title>
</head>
<body>
    <img src="~/Content/logo_header.png" />
    <div>
        page [page] of [topage]
    </div>
</body>
</html>