在 MVC net core 中使用 Rotativa pdf 显示动态 header

Display dynamic header using Rotativa pdf in MVC net core

我想在 cshtml 中显示动态 header。当我尝试使用 CustomSwitches 传递它时,我收到类似 QPaintDevice: Cannot destroy paint device that is being painted 调用终止的纯虚拟方法,没有活动异常

从 mvc Controller 调用 Rotativa 组件(它在 class 库中)以打印没有 header 的动态视图可以正常工作,但不能使用 header。有没有办法解决这个问题?

感谢您的回答。

编辑:使用 Rotativa 的示例

字符串header = $"/Header.cshtml";

var customSwitches = string.Format("--header-html "{0}" " + "--header-spacing "0" ", header);

        var view = new ViewAsPdf(pdfView.ToViewRoute(culture), model: viewModel)
        {
            PageOrientation = option.PageOrientation,
            IsGrayScale = option.IsGrayScale,
            MinimumFontSize = option.MinimumFontSize,
            PageSize = option.PageSize,
            PageMargins = option.PageMargins,
            CustomSwitches = customSwitches
        };
        return view;

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
        };
    }

型号

public class tbpage
{
    public string page_name { get; set; }
    public string page_no { get; set; }
}

PDF

另外,如果还是不正确,请在动作Header中加上[AllowAnonymous]。也可以参考answer.

[AllowAnonymous]
public ActionResult Header()
{
    return View();
}