7

我正在尝试在 Rotativa 库生成的 PDF 中指定页眉和页脚。正如作者在这里回答的那样,应该可以使用 CSS(在此处描述)。但是,我无法做到这一点。

我在元标记中加载了一个样式表:

<link href="print.css" rel="stylesheet" type="text/css" media="print" />

在底部的样式表中:

@page {
    @top-left {
        content: "TOP SECRET";
        color: red
    }
    @bottom-right {
        content: counter(page);
        font-style: italic
    }
}

然后通过以下方式生成 PDF:

public ActionResult ShowPdf()
{
     var model = new Model();
     return new ViewAsPdf("view.cshtml", model)
                {
                    FileName = "Report.pdf",
                    CustomSwitches = "--print-media-type"
                };
}

然后 PDF 的页眉和页脚中没有任何内容。有任何想法吗?

4

5 回答 5

12

我找到了wkhtmltopdf 的文档(或更好的存档版本),并在那里描述了如何管理页眉和页脚。

基本上,您可以将--header-center "text"(或类似的开关)添加到参数列表中,仅此而已。

因此,将它与 Rotativa 一起使用将是:

public ActionResult ShowPdf()
{
     var model = new Model();
     return new ViewAsPdf("view.cshtml", model)
                {
                    FileName = "Report.pdf",
                    CustomSwitches = "--print-media-type --header-center \"text\""
                };
}

(不知道有没有--print-media-type必要。)

于 2013-03-18T14:09:31.927 回答
9

如果您想在页眉/页脚中显示视图而不是文本,则可以这样做:

public ActionResult ViewPDF()
{
      string customSwitches = 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()
{
    return View();
}

不要忘记在页脚操作中添加 [AllowAnonymous] 属性,否则 Rotatina 无法访问路径。

于 2014-10-24T09:16:17.177 回答
5

这是我的做法(完整):

public ActionResult PrintPDF(int? selectedSiteRotaId, int selectedSiteId)
{
    string footer = "--footer-center \"Printed on: " + DateTime.Now.Date.ToString("MM/dd/yyyy") + "  Page: [page]/[toPage]\"" + " --footer-line --footer-font-size \"9\" --footer-spacing 6 --footer-font-name \"calibri light\"";

    return new ActionAsPdf("RenderPDF", new { selectedSiteRotaId = selectedSiteRotaId, selectedSiteId = 7 }) 
    {
        FileName = "PDF_Output.pdf",
        PageOrientation = Orientation.Landscape,
        MinimumFontSize = 10, 
        //PageMargins  = new Margins(5,5,5,5),
        PageSize = Size.A3,
        CustomSwitches = footer
    };

    //var pdfResult = new ActionAsPdf("RenderPDF", new { selectedSiteRotaId = selectedSiteRotaId, selectedSiteId = 7 })
    //{
    //    FileName = "PDF_Output.pdf",
    //    PageOrientation = Orientation.Landscape,
    //    MinimumFontSize = 10
    //};

    //var binary = pdfResult.BuildPdf(this.ControllerContext);

    //return File(binary, "application/pdf");
}


public ActionResult RenderPDF(int? selectedSiteRotaId, int selectedSiteId)
{
    return RedirectToAction("Index", "PrintPDF", new { selectedSiteRotaId = selectedSiteRotaId, selectedSiteId = 7 });
}
于 2016-01-05T12:42:31.463 回答
-1
string customSwitches = string.Format("--header-spacing \"0\" --footer-spacing \"0\"  --footer-html {0}   ", Url.Action("Footer", "Invoice", new { }, "http"));
        return new ViewAsPdf(saleInvoiceVm)
        {
            PageOrientation = Orientation.Portrait,
            PageSize = Rotativa.AspNetCore.Options.Size.A4,
            PageMargins = { Left = 5, Bottom = 25, Right = 7, Top = 10 },
            CustomSwitches = customSwitches,
        };
于 2020-12-30T18:54:50.053 回答
-2

试试吧,它会工作 100%

 return new ViewAsPdf("MyPDF.cshtml", model)
            {
                FileName = "MyPDF.pdf",
                CustomSwitches = customSwitches
            };

}

于 2015-11-23T11:09:35.353 回答