bpi*_*iec 6 c# pdf-generation asp.net-mvc-3 rotativa
我正在尝试在Rotativa库生成的PDF中指定页眉和页脚.正如作者在这里回答的那样,应该可以使用CSS(这里描述).但是,我无法做到这一点.
我在meta标签中加载了样式表:
<link href="print.css" rel="stylesheet" type="text/css" media="print" />
Run Code Online (Sandbox Code Playgroud)
在底部的样式表中:
@page {
@top-left {
content: "TOP SECRET";
color: red
}
@bottom-right {
content: counter(page);
font-style: italic
}
}
Run Code Online (Sandbox Code Playgroud)
然后通过以下方式生成PDF:
public ActionResult ShowPdf()
{
var model = new Model();
return new ViewAsPdf("view.cshtml", model)
{
FileName = "Report.pdf",
CustomSwitches = "--print-media-type"
};
}
Run Code Online (Sandbox Code Playgroud)
然后,PDF的页眉和页脚中没有任何内容.有任何想法吗?
bpi*_*iec 10
我找到了wkhtmltopdf的文档,并在那里描述了如何管理页眉和页脚.
基本上你可以添加--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\""
};
}
Run Code Online (Sandbox Code Playgroud)
(我不知道是否--print-media-type
有必要.)
如果你想在页眉/页脚中显示一个View而不是文本,那么你可以这样做:
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();
}
Run Code Online (Sandbox Code Playgroud)
不要忘记在Footer操作上添加[AllowAnonymous]属性,否则Rotatina无法访问路径.
这是我的做法(完整):
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 });
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
28264 次 |
最近记录: |