2013-03-06 109 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 }); 
} 
相關問題