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.

Bartłomiej Semańczyk
  • 59,234
  • 49
  • 233
  • 358
Herin
  • 704
  • 3
  • 18
  • 34

1 Answers1

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://stackoverflow.com/a/26544977/2660864 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);
}
Community
  • 1
  • 1
katho2404
  • 359
  • 1
  • 6
  • 20
  • 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. – Herin Aug 31 '15 at 09:59
  • 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 view – katho2404 Aug 31 '15 at 10:50
  • 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. – Herin Aug 31 '15 at 11:21
  • 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? – katho2404 Aug 31 '15 at 13:11
  • 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: http://stackoverflow.com/a/26544977/2660864 I will edit my post accordingly – katho2404 Aug 31 '15 at 13:20
  • 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. – Herin Aug 31 '15 at 14:15
  • Can you please post some code and the exact error message? – katho2404 Aug 31 '15 at 14:28
  • Hello, I have tried the above solution, but after downloading the PDF, it shows can`t load the PDF document. Any other solution? – Ishan Shah Feb 12 '21 at 10:04