2013-03-06 2 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" (또는 유사한 스위치)을 인수 목록에 추가 할 수 있습니다. 그게 전부입니다.

그래서 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

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

은 (전액) 내가 그것을 어떻게

다음
2

인이 100 %

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

}을 작동 그것을 시도 :

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 }); 
} 
관련 문제