2013-03-06 175 views
6

我试图在由Rotativa库生成的PDF中指定页眉和页脚。由于作者回答here,应该可以使用CSS(描述为here)。但是,我无法做到这一点。在Rotativa生成的PDF中显示页眉和页脚

我在meta标签加载的样式表:

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

而在底部的样式表:

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

和:

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

然后通过生成PDF那么PDF的页眉和页脚中将不会显示任何内容。有任何想法吗?

回答

8

我找到了documentation of 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是必要的)

+0

只是测试,你不需要' --print-media-type' – Kendrome 2015-10-28 15:33:03

5

如果你想在标题中显示视图,而不是文本/页脚那么你可以这样做是这样的:

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无法获得访问路径。

+0

我不得不将它改为'http',但它很好地工作。感谢分享! – joetinger 2015-07-21 20:49:43

-1

尝试将工作100%

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

}

2

这里是我是如何做到的(全称):

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