2013-03-06 16 views
6

Ich versuche, Kopf- und Fußzeilen in einem PDF von rotativa Bibliothek erzeugt angeben. Wie der Autor here beantwortet soll es möglich sein, mit Hilfe von CSS (beschrieben here). Ich kann das aber nicht machen.Anzeige von Kopf- und Fußzeilen in einem PDF erzeugt durch rotativa

Ich habe ein Stylesheet in dem Meta-Tag geladen:

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

Und im Stylesheet am Boden:

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

Und dann PDF Erzeugung von:

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

Und dann erscheint nichts in der Kopf- und Fußzeile der PDF. Irgendwelche Ideen?

Antwort

8

habe ich ein documentation of wkhtmltopdf gefunden, und es wird beschrieben, wie Kopf- und Fußzeilen zu verwalten.

Grundsätzlich können Sie nur --header-center "text" (oder ähnliche Schalter) in die Argumentliste hinzufügen und das ist alles.

mit ihm also mit rotativa wäre es:

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

(. Ich weiß nicht, ob --print-media-type notwendig ist)

+0

gerade getestet und Sie brauchen nicht die ' --print-media-Baumuster zur – Kendrome

5

Wenn Sie eine Ansicht anstelle von Text in der Kopfzeile angezeigt werden wollen/Fußzeile dann können Sie dies wie folgt aus:

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

Sie sonst nicht vergessen Aktion die [AllowAnonymous] Attribut auf der Fußzeile hinzufügen Rotatina kann keinen Zugriff auf den Weg bekommen.

+0

ich musste es 'http' ändern, aber es funktioniert gut. Danke für das Teilen! – joetinger

-1

versuchen sie es funktioniert 100%

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

}

2

Hier ist, wie ich es tat (vollständig):

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