我在我的MVC5项目中使用了Rotativa.MVC 2.0.3。我一直在代码编辑器中收到错误,说明" ActionAsPDF不包含CustomSwitches"的定义。我尝试使用ViewAsPDF,如下面的链接所示,结果相同。 以下是我发现的有关如何向Rotativa PDF添加页眉/页脚的几个来源:
我的控制器中的代码是:
using Rotativa.MVC;
public ActionResult CertificatePDF(int? wo_nbr)
{
var PDFresult = new ActionAsPdf("Certificate", new { wo_nbr = wo_nbr })
{
FileName = "Certificate.PDF",
CustomSwitches = "fill in details later"
};
return PDFresult;
}
所以问题是:如何在Rotativa 2.0.3中实现CustomSwitch?或者,CustomSwitches定义在哪里? PDF没有CustomSwitches标签,因此非常简单地渲染,所以我知道Rotativa正在做它的工作。
答案 0 :(得分:1)
在寻找别的东西的时候,我偶然发现了this post来回答我的问题。
基本上,我必须引用Rotativa.Core并创建一个DriverOptions对象。
以下是工作代码的样子:
using Rotativa.MVC;
using Rotativa.Core;
public ActionResult CertificatePDF(int? wo_nbr)
{
string CustomSwitch = "--footer-line --footer-font-size \"8\" --footer-left \"Printed: \"" + DateTime.Now.Date.ToString("yyyy-MM-dd");
var rotativaOptions = new DriverOptions() { CustomSwitches = CustomSwitch };
var PDFresult = new ActionAsPdf("Certificate", new { wo_nbr = wo_nbr })
{
RotativaOptions = rotativaOptions
};
return PDFresult;
}
同样,我正在使用Rotativa 2.0.3。希望这个答案可以帮助别人!