Search code examples
c#asp.net-mvcpdfrotativa

C# MVC 5 Converting html view to PDF to send an email attachment in Bussness Layer


I want to email a PDF sales report to all the agents and this PDF need to be generated dynamically for each agent. I used ItextSharp for that but that requires too much fiddling with html code. Finally I landed on Rotativa, it works well but there are almost more then few thousands of agents and I have a separate Business Layer to do this job. How do I make Rotativa to work in business layer?

 var pdfResult = new ViewAsPdf("CreateIMDStatmentReport", _IReportManagement.GetIMDStatmentReportByIMDCode(imdCode));
 var binary = pdfResult.BuildPdf(this.ControllerContext);     
 System.Net.Mail.Attachment data = new Attachment(new MemoryStream(binary), MediaTypeNames.Application.Pdf);
  1. How to get a view in separate Business Layer?
  2. How do I deal with ControllerContext?

Note: In future this view will be stored in database and will be rendered as VirtualPath


Solution

  • I solved it!

    For the view I used the absolute paths.
    And for the ControllerContext ,I passed its object as a parameter from controller.
    Also I used System.Web.Mvc package

    Thank you to all!