使用Asp MVC 5中的Rotativa PDF生成器隐藏PDF中的打印按钮和导航菜单

时间:2018-01-04 11:56:32

标签: javascript c# jquery asp.net-mvc rotativa

我想将页面内容转换为PDF。所以我已经安装了nuget包rotativa,当我尝试打印视图时它以PDF格式显示整个页面以及打印按钮。我不想在我想要的输出PDF中显示打印按钮和导航菜单。

The image of current output PDF

2 个答案:

答案 0 :(得分:1)

为实现这一目标,我创建了一个新视图(Preview.cshtml),其中包含我需要导出为PDF的数据。

public ActionResult ExportCvPdf()
{
    string switches = string.Format("--disable-smart-shrinking --header-html {0} --footer-html {1}",
     Url.Action("Header", "Cotroller", new { area = "Areaname" }, "http"),
     Url.Action("Footer", "Cotroller", new { area = "Areaname" }, "http"));

    return new Rotativa.ViewAsPdf("Preview", data)
      {
        FileName = "Sample-" + DateTime.Now.ToString("yyyyMMdd") + ".pdf",
        PageSize = Size.A4,
        PageMargins = new Margins(30, 15, 20, 15),
        CustomSwitches = switches
      };

}

public ActionResult Preview(ViewModel detail)
{
  return View(detail);
}

答案 1 :(得分:0)

Rotativa不会渲染为打印介质,但是作为浏览器,您必须强制渲染为打印。 发送“ --print-media-type”参数

这样做,您可以使用CSS隐藏要打印的介质

@media print
{    
    .no-print, .no-print *
    {
        display: none !important;
    }
}

或使用引导程序隐藏印象

<input type="button" class="hidden-impression" value="PDF"/>

如何使旋转烤肉架了解如何打印 例如:

    return new ViewAsPdf("Details", obj)
                    {
                        CustomSwitches = "--print-media-type"
}

完整示例:

return new ViewAsPdf("Details", obj)
                {
                    CustomSwitches = "--print-media-type",
                    FileName = $"Talao_{DateTime.Now.Day}-{DateTime.Now.Month}-{DateTime.Now.Year}.pdf",
                    PageOrientation = Orientation.Portrait,
                    PageSize = Size.A4,
                    PageMargins = new Margins(0, 0, 0, 0),
                    PageWidth = 210,
                    PageHeight = 297
                };