Display dynamic header using Rotativa pdf in MVC
Asked Answered
C

1

7

I wanted to print header data which are dynamic and will come from controller.

So how can I display that dynamic data in header using Rotativa pdf.

My header data include Name, Address, Contact info and other additional information, which are dynamic and generated from controller side.

I have created pdf with static header as below by using 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 is working well with Static header.

Now I need the header text will go from this controller dynamically.

Chapa answered 31/8, 2015 at 8:6 Comment(0)
S
1

I had a similar specification once and realized it with an extra View for Printing.

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

In my case the Print-View was called 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 like this:

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

EDIT

When you look at this answer https://mcmap.net/q/743180/-displaying-headers-and-footers-in-a-pdf-generated-by-rotativa you can see, that you can use .cshtml files in your CustomActions (I did not test 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);
}
Suu answered 31/8, 2015 at 9:43 Comment(8)
Hello, I have already done with this with static header. But now, I have problem with displaying dynamic header text. So how can pass that dynamic header to my header.html page and access in that page.Chapa
You can create an extra model for your Print View, in which you put your dynamic data. Then you can load the model's data in your PrintView.cshtml like in any other viewSuu
Hello, I have already done with the pdf body portion with extra model. But I have problem with Pdf header portion where I can not load .cshtml page, I can only load header by using .html page. Please see my question's description, I have updated my question. And suggest me how can I deal with html page only. Thanks.Chapa
I don't think that you can load data from the controller in a normal html page. why don't you put the content of your html files into the cshtml and fill it too with data from the model?Suu
Oh, I think I misunderstood you there. You want to include the header with CustomSwitches. I never did this, but I found a similar question and quite a good answer here: https://mcmap.net/q/743180/-displaying-headers-and-footers-in-a-pdf-generated-by-rotativa I will edit my post accordinglySuu
Hello, I have tried above logic for rendering header by cshtml view, but it does not working. It not getting any view and giving run time error.Chapa
Can you please post some code and the exact error message?Suu
Hello, I have tried the above solution, but after downloading the PDF, it shows can`t load the PDF document. Any other solution?Pragmatism

© 2022 - 2024 — McMap. All rights reserved.