我正在尝试在 Rotativa 库生成的 PDF 中指定页眉和页脚。正如作者回答here应该可以使用 CSS(描述 here )。但是,我无法执行此操作。
我在元标记中加载了一个样式表:
<link href="print.css" rel="stylesheet" type="text/css" media="print" />
在底部的样式表中:
@page {
@top-left {
content: "TOP SECRET";
color: red
}
@bottom-right {
content: counter(page);
font-style: italic
}
}
然后通过以下方式生成 PDF:
public ActionResult ShowPdf()
{
var model = new Model();
return new ViewAsPdf("view.cshtml", model)
{
FileName = "Report.pdf",
CustomSwitches = "--print-media-type"
};
}
然后 PDF 的页眉和页脚中什么也没有出现。有什么想法吗?
最佳答案
我找到了 documentation of wkhtmltopdf (或更好的 archived version )并且在那里描述了如何管理页眉和页脚。
基本上,您只需将 --header-center "text"
(或类似的开关)添加到参数列表即可。
因此将它与 Rotativa 一起使用将是:
public ActionResult ShowPdf()
{
var model = new Model();
return new ViewAsPdf("view.cshtml", model)
{
FileName = "Report.pdf",
CustomSwitches = "--print-media-type --header-center \"text\""
};
}
(不知道是否需要--print-media-type
。)
关于c# - 在 Rotativa 生成的 PDF 中显示页眉和页脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15250505/