C# 在MVC net core中使用Rotativa pdf显示动态标题

C# 在MVC net core中使用Rotativa pdf显示动态标题,c#,razor,asp.net-core-mvc,pdf-generation,rotativa,C#,Razor,Asp.net Core Mvc,Pdf Generation,Rotativa,我想在cshtml中显示动态标题。当我尝试使用CustomSwitches传递它时,我会得到一个类似QPaintDevice的错误:无法销毁正在绘制的绘制设备调用纯虚拟方法terminate调用时没有激活的异常 从mvc控制器调用Rotativa组件(它在类库中)来打印没有标题的动态视图可以正常工作,但不使用标题。有没有办法解决这个问题 谢谢你的回答 编辑:使用Rotativa的示例 字符串头=$“/header.cshtml” var customSwitches=string.Format(

我想在cshtml中显示动态标题。当我尝试使用CustomSwitches传递它时,我会得到一个类似QPaintDevice的错误:无法销毁正在绘制的绘制设备调用纯虚拟方法terminate调用时没有激活的异常

从mvc控制器调用Rotativa组件(它在类库中)来打印没有标题的动态视图可以正常工作,但不使用标题。有没有办法解决这个问题

谢谢你的回答

编辑:使用Rotativa的示例

字符串头=$“/header.cshtml”

var customSwitches=string.Format(“--header html“{0}”+”--header间距“0”,header)


customSwitches
可能找不到/header.cshml。您需要给出正确的路径

    [HttpGet]
    public IActionResult Pdf()
    {
        string customSwitches = string.Format(" --print-media-type --page-offset 2 --footer-center [page] --allow {0} --footer-html {0} --footer-spacing -180 ",
            Url.Action("header", "home", new { area = "" }, "https"));
        var pageList = new List<tbpage>();
        pageList.Add(new tbpage()
        {
            page_name = "1",
            page_no = "a"
        });
        return new ViewAsPdf("PdfDemo", model: pageList)
            {
                PageOrientation = Rotativa.AspNetCore.Options.Orientation.Portrait,  
                IsGrayScale = true,
                MinimumFontSize = 20,
                 PageSize = Rotativa.AspNetCore.Options.Size.A5,
                 PageMargins = { Left = 20, Bottom = 20, Right = 20, Top = 20 },
                CustomSwitches = customSwitches
        };
    }
Pdf

此外,如果仍然不正确,请将
[AllowAnonymous]
添加到操作
标题
。你也可以参考


你能分享一些调用Rotativa组件的代码吗?因为我无法重现错误。谢谢,我刚刚用我如何使用它的exmaple编辑了这个问题。你有动态标题工作的例子吗?谢谢。页眉是指pdf的第一页,或pdf中每一页的页眉?谢谢您的回复。我需要在所有的页面相同的标题。这是所有的代码。如果您有任何其他问题,请显示更多的关键代码。
    [HttpGet]
    public IActionResult Pdf()
    {
        string customSwitches = string.Format(" --print-media-type --page-offset 2 --footer-center [page] --allow {0} --footer-html {0} --footer-spacing -180 ",
            Url.Action("header", "home", new { area = "" }, "https"));
        var pageList = new List<tbpage>();
        pageList.Add(new tbpage()
        {
            page_name = "1",
            page_no = "a"
        });
        return new ViewAsPdf("PdfDemo", model: pageList)
            {
                PageOrientation = Rotativa.AspNetCore.Options.Orientation.Portrait,  
                IsGrayScale = true,
                MinimumFontSize = 20,
                 PageSize = Rotativa.AspNetCore.Options.Size.A5,
                 PageMargins = { Left = 20, Bottom = 20, Right = 20, Top = 20 },
                CustomSwitches = customSwitches
        };
    }
public class tbpage
{
    public string page_name { get; set; }
    public string page_no { get; set; }
}
[AllowAnonymous]
public ActionResult Header()
{
    return View();
}