C# 在 Rotativa 生成的 PDF 中显示页眉和页脚

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15250505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:40:39  来源:igfitidea点击:

Displaying headers and footers in a PDF generated by Rotativa

c#asp.net-mvc-3pdf-generationrotativa

提问by bpiec

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

我正在尝试在 Rotativa 库生成的 PDF 中指定页眉和页脚。正如作者在此处回答的那样,应该可以使用 CSS(此处描述)。但是,我无法做到这一点。

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:

然后通过以下方式生成PDF:

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?

然后在 PDF 的页眉和页脚中什么也没有出现。有任何想法吗?

采纳答案by bpiec

I've found a documentation of wkhtmltopdfand it is described there how to manage headers and footers.

我找到了wkhtmltopdf文档,其中描述了如何管理页眉和页脚。

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

基本上,您可以将--header-center "text"(或类似的开关)添加到参数列表中,仅此而已。

So using it with Rotativa it would be:

因此,将它与 Rotativa 一起使用将是:

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

(不知道有--print-media-type没有必要。)

回答by Charly

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.

不要忘记在页脚操作上添加 [AllowAnonymous] 属性,否则 Rotatina 无法访问路径。

回答by Ali

try it it will work 100 %

试试看,它会 100% 有效

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

}

}

回答by t_plusplus

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 });
}