2013-03-06 13 views
6

Próbuję określić nagłówki i stopki w pliku PDF wygenerowanym przez bibliotekę Rotativa. Ponieważ autor odpowiedział na here, powinno być możliwe użycie CSS (opisany here). Jednak nie jestem w stanie tego zrobić.Wyświetlanie nagłówków i stopek w pliku PDF wygenerowanym przez Rotativa

Mam stylów załadowany w tagu meta:

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

A w arkuszu stylów na dole:

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

A potem generowania PDF przez:

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

I wtedy nic nie pojawia się w nagłówku i stopce pliku PDF. Jakieś pomysły?

Odpowiedz

8

Znalazłem documentation of wkhtmltopdf i jest tam opisane, jak zarządzać nagłówkami i stopkami.

Zasadniczo wystarczy dodać --header-center "text" (lub podobne przełączniki) do listy argumentów i to wszystko.

więc używając go z Rotativa byłoby:

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

(. Nie wiem, czy --print-media-type jest konieczne)

+0

tylko przetestowane i nie potrzebne ' --print-media-type' – Kendrome

5

Jeśli chcesz wyświetlić widok zamiast tekstu w nagłówku/stopka następnie można to zrobić tak:

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

nie zapomnij dodać [AllowAnonymous] atrybut działania stopka inaczej Rotatina nie może uzyskać dostępu do ścieżki.

+0

Musiałem zmienić go na 'http', ale działa ładnie. Dzięki za udostępnienie! – joetinger

-1

spróbować to będzie działać w 100%

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

}

2

Oto jak to zrobiłem (w całości):

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 }); 
} 
Powiązane problemy