【问题标题】:Display dynamic header using Rotativa pdf in MVC net core在 MVC 网络核心中使用 Rotativa pdf 显示动态标题
【发布时间】:2020-12-08 09:45:16
【问题描述】:

我想在 cshtml 中显示动态标题。当我尝试使用 CustomSwitches 传递它时,我收到一个错误,例如 QPaintDevice: Cannot destroy paint device that is beingpaintingpure virtual method called terminate called without an active exception.

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

感谢您的回答。

编辑:使用 Rotativa 的示例

字符串头 = $"/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;

【问题讨论】:

  • 你能分享一些调用Rotativa组件的代码吗?因为我无法重现错误。
  • 谢谢,我刚刚编辑了这个问题,并举例说明了我是如何使用它的。你有一个动态标题工作的例子吗?谢谢。
  • header是指pdf的第一页,还是pdf中每一页的head?
  • 感谢您的回复。我需要在所有页面中使用相同的标题。

标签: c# razor asp.net-core-mvc pdf-generation rotativa


【解决方案1】:

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

另外,如果还是不正确,请将[AllowAnonymous]添加到动作Header。也可以参考answer

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

【讨论】:

  • 这里是所有代码。如果您有任何其他问题,请显示更多关键代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-02
相关资源
最近更新 更多