Her*_*rin 7 pdf asp.net-mvc wkhtmltopdf rotativa
我想打印动态的标题数据,它将来自控制器.
那么如何使用Rotativa pdf在标题中显示动态数据.
我的标题数据包括名称,地址,联系信息和其他附加信息,这些信息是动态的,并从控制器端生成.
我使用html页面创建了带有静态头的pdf,如下所示
string header = Server.MapPath("~/Static/NewFolder/PrintHeader.html");
string footer = Server.MapPath("~/Static/NewFolder/PrintFooter.html");
string customSwitches = string.Format("--header-html \"{0}\" " +
"--header-spacing \"0\" " +
"--footer-html \"{1}\" " +
"--footer-spacing \"10\" " +
"--footer-font-size \"10\" " +
"--header-font-size \"10\" ", header, footer);
return new ViewAsPdf("SchedulePrintPdf", modelData)
{
CustomSwitches = customSwitches,
PageOrientation = Orientation.Portrait,
PageMargins = { Top = 20, Bottom = 22 },
SaveOnServerPath = filePath, FileName = Path.GetFileName(fileName)
};
Run Code Online (Sandbox Code Playgroud)
这适用于Static标头.
现在我需要动态地从这个控制器发送标题文本.
我曾经有过类似的规范,并通过额外的打印视图实现了它。
在那里您可以从控制器获取附加数据并包含特殊的 CSS 样式。当您使用 bootstrap 时,请考虑用于 PDF 打印的分辨率非常小,因此您必须使用这些col-xs-*类。
在我的例子中,打印视图被称为 ResultPrint.cshtml,在控制器中我有这个函数:
public ActionResult GeneratePDF(int id)
{
InputPrintModel model = db.InputPrintModel.Find(id);
if (model == null)
{
return HttpNotFound();
}
try
{
return new ViewAsPdf("ResultPrint", model);
}
catch (Exception ex)
{
// Error Dialog + Logging
return View("Result", model);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的 Result.cshtml 中这样调用:
@Html.ActionLink("Generate PDF", "GeneratePDF", new { id = Model.Id })
Run Code Online (Sandbox Code Playgroud)
编辑
当您查看此答案/sf/answers/1858148421/时,您可以看到,您可以在 CustomActions 中使用 .cshtml 文件(我没有测试此代码)
public ActionResult ViewPDF()
{
string cusomtSwitches = 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()
{
// get custom data for view
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8211 次 |
| 最近记录: |