简体   繁体   中英

Hide Print button and nav menu in PDF using Rotativa PDF generator in Asp MVC 5

I want to convert the page content to PDF. So that I have installed nuget package rotativa and when I try to print a view It shows the entire page in PDF along with the print button. I don't want show the print button and navigation menu in my desired output PDF.

当前输出PDF的图像

To achieve this I have created new view (Preview.cshtml) which contains the data which I need to export as 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);
}

Rotativa does not render as print media, but as a browser, you must force rendering as print. Send the " --print-media-type " argument

Doing this you can use css to hide if the media for print

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

or use the bootstrap to hide the impression

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

How to make the rotativa understand how to printer Ex:

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

Complete example:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM