Displaying headers and footers in a PDF generated by Rotativa
Asked Answered
E

5

7

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?

Elseelset answered 6/3, 2013 at 14:51 Comment(0)
E
12

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.)

Elseelset answered 18/3, 2013 at 14:9 Comment(1)
Just tested and you don't need the --print-media-typeEddaeddana
A
9

If you wanted to display a View instead of text in the header/footer then you can do so like this:

public ActionResult ViewPDF()
{
      string customSwitches = string.Format("--print-media-type --allow {0} --footer-html {0} --footer-spacing -10",
                Url.Action("Footer", "Document", new { area = ""}, "https"));


     return new ViewAsPdf("MyPDF.cshtml", model)
                {
                    FileName = "MyPDF.pdf",
                    CustomSwitches = customSwitches
                };
}

[AllowAnonymous]
public ActionResult Footer()
{
    return View();
}

Don't forget to add the [AllowAnonymous] attribute on the Footer action otherwise Rotatina can't get access to the path.

Amplifier answered 24/10, 2014 at 9:16 Comment(2)
I had to change it to http but it works nicely. Thanks for sharing!Byelection
where to put the footer.html in the directory?Geralyngeraniaceous
J
5

Here is how I did it (in full):

public ActionResult PrintPDF(int? selectedSiteRotaId, int selectedSiteId)
{
    string footer = "--footer-center \"Printed on: " + DateTime.Now.Date.ToString("MM/dd/yyyy") + "  Page: [page]/[toPage]\"" + " --footer-line --footer-font-size \"9\" --footer-spacing 6 --footer-font-name \"calibri light\"";

    return new ActionAsPdf("RenderPDF", new { selectedSiteRotaId = selectedSiteRotaId, selectedSiteId = 7 }) 
    {
        FileName = "PDF_Output.pdf",
        PageOrientation = Orientation.Landscape,
        MinimumFontSize = 10, 
        //PageMargins  = new Margins(5,5,5,5),
        PageSize = Size.A3,
        CustomSwitches = footer
    };

    //var pdfResult = new ActionAsPdf("RenderPDF", new { selectedSiteRotaId = selectedSiteRotaId, selectedSiteId = 7 })
    //{
    //    FileName = "PDF_Output.pdf",
    //    PageOrientation = Orientation.Landscape,
    //    MinimumFontSize = 10
    //};

    //var binary = pdfResult.BuildPdf(this.ControllerContext);

    //return File(binary, "application/pdf");
}


public ActionResult RenderPDF(int? selectedSiteRotaId, int selectedSiteId)
{
    return RedirectToAction("Index", "PrintPDF", new { selectedSiteRotaId = selectedSiteRotaId, selectedSiteId = 7 });
}
Joaquin answered 5/1, 2016 at 12:42 Comment(0)
G
-1
string customSwitches = string.Format("--header-spacing \"0\" --footer-spacing \"0\"  --footer-html {0}   ", Url.Action("Footer", "Invoice", new { }, "http"));
        return new ViewAsPdf(saleInvoiceVm)
        {
            PageOrientation = Orientation.Portrait,
            PageSize = Rotativa.AspNetCore.Options.Size.A4,
            PageMargins = { Left = 5, Bottom = 25, Right = 7, Top = 10 },
            CustomSwitches = customSwitches,
        };
Gastight answered 30/12, 2020 at 18:54 Comment(1)
Please fix your answer by putting all of your code into a code blockPict
J
-2

try it it will work 100 %

 return new ViewAsPdf("MyPDF.cshtml", model)
            {
                FileName = "MyPDF.pdf",
                CustomSwitches = customSwitches
            };

}

Jacoby answered 23/11, 2015 at 11:9 Comment(1)
Need to define customSwitches pls.Septennial

© 2022 - 2024 — McMap. All rights reserved.