加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 在MVC中使用Rotativa pdf显示动态标头

发布时间:2020-12-16 03:45:37 所属栏目:asp.Net 来源:网络整理
导读:我想打印动态的标题数据,它将来自控制器. 那么如何使用Rotativa pdf在标题中显示动态数据. 我的标题数据包括名称,地址,联系信息和其他附加信息,这些信息是动态的,并从控制器端生成. 我使用html页面创建了带有静态头的pdf,如下所示 string header = Server.Ma
我想打印动态的标题数据,它将来自控制器.

那么如何使用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)
                    };

这适用于Static标头.

现在我需要动态地从这个控制器发送标题文本.

解决方法

我有一个类似的规范,并通过额外的打印视图实现它.

在那里,您可以从控制器获取其他数据,并包含一种特殊的CSS样式.
使用引导程序时,请考虑用于PDF打印的分辨率非常小,因此必须使用col-xs- *类.

在我的情况下,Print-View被称为ResultPrint.cshtml,在Controller中我有这个功能:

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

这在我的Result.cshtml中被调用,如下所示:

@Html.ActionLink("Generate PDF","GeneratePDF",new { id = Model.Id })

编辑

当您查看此答案https://stackoverflow.com/a/26544977/2660864时,您可以看到,您可以在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);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读