Display dynamic header using Rotativa pdf in MVC

I wanted to print header data that is dynamic and will come from the controller.

So, how can I display this dynamic data in the header using Rotativa pdf.

My header data includes name, address, contact information and other additional information that are dynamic and generated by the controller.

I created a pdf with a static header as shown below using the html page

string header = Server.MapPath("~/Static/NewFolder/PrintHeader.html"); string footer = Server.MapPath("~/Static/NewFolder/PrintFooter.html"); string customSwitches = string.Format("--header-html \"{0}\" " + "--header-spacing \"0\" " + "--footer-html \"{1}\" " + "--footer-spacing \"10\" " + "--footer-font-size \"10\" " + "--header-font-size \"10\" ", header, footer); return new ViewAsPdf("SchedulePrintPdf", modelData) { CustomSwitches = customSwitches, PageOrientation = Orientation.Portrait, PageMargins = { Top = 20, Bottom = 22 }, SaveOnServerPath = filePath, FileName = Path.GetFileName(fileName) }; 

This works well with a static header.

Now I need the header text to jump from this controller dynamically.

+6
source share
1 answer

I had a similar specification, and I understood it with an additional print view.

There you can get additional data from the controller and enable a special CSS style. When using bootstrap, consider that the resolution used for PDF printing is very small, so you need to use the col-xs-* classes.

In my case, Print-View was called by ResultPrint.cshtml, and in the controller I had this function:

  public ActionResult GeneratePDF(int id) { InputPrintModel model = db.InputPrintModel.Find(id); if (model == null) { return HttpNotFound(); } try { return new ViewAsPdf("ResultPrint", model); } catch (Exception ex) { // Error Dialog + Logging return View("Result", model); } } 

which was called in my Result.cshtml as follows:

 @Html.ActionLink("Generate PDF", "GeneratePDF", new { id = Model.Id }) 

EDIT

When you look at this answer fooobar.com/questions/380758 / ... , you can see that you can use .cshtml files in your user settings (I have not tested this code)

 public ActionResult ViewPDF() { string cusomtSwitches = 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() { // get custom data for view return View(model); } 
+1
source

All Articles