2013-03-06 13 views
6

Rotativaライブラリで生成されたPDFにヘッダーとフッターを指定しようとしています。作者がhereと答えたので、CSSを使用することができるはずです(hereと記載)。しかし、私はこれを行うことができません。Rotativaで生成されたPDFにヘッダーとフッターを表示する

私はメタタグにロードされたスタイルシートがあります。

<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"(または同様のスイッチ)を追加するだけで、それだけです。だから、それは可能でしょうRo​​tativaでそれを使用して

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

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

はそうRotatinaはパスへのアクセスを得ることができないフッターアクションの[のAllowAnonymous]属性を追加することを忘れないでください。

+0

私は 'http 'に変更しなければなりませんでしたが、うまく動作します。共有ありがとう! – joetinger

-1

それはここで

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

}

2

100%に動作します、それを試してみてください、私は(完全に)それをやった方法です:

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 }); 
} 
関連する問題