1

I want to create a pdf file using Rotativa framework from ApiController. In my project there is no any view. Content of pdf (HTML) is in the database.

Is it any way to generate pdf without View and ControllerContext ? Controller context is accessible only in Controller not in APIController...

I found this https://github.com/JustMaier/Rotativa/tree/ed7678eefdffb3995f5b6a3e9afb18903c648df8 but it needs View

Problem is in method BuildFile()...

Start from here

var a = new Rotativa.ViewAsPdf(pdfResult);
a.FileName = request.ResultFileName;
byte[] f = a.BuildFile();

ControlerContext can be null here

 public byte[] BuildFile(ControllerContext context = null)
    {
        if (this.WkhtmlPath == string.Empty)
            this.WkhtmlPath = context == null ? HttpContext.Current.Server.MapPath("~/Rotativa") : context.HttpContext.Server.MapPath("~/Rotativa");

        var fileContent = this.CallTheDriver(context);

        if (string.IsNullOrEmpty(this.SaveOnServerPath) == false)
        {
            File.WriteAllBytes(this.SaveOnServerPath, fileContent);
        }

        return fileContent;
    }

...but here can't

    protected override byte[] CallTheDriver(ControllerContext context)
    {
        // use action name if the view name was not provided
        string viewName = ViewName;
        if (string.IsNullOrEmpty(viewName))
            viewName = context.RouteData.GetRequiredString("action");

        ViewEngineResult viewResult = GetView(context, viewName, MasterName);
        string html = context.GetHtmlFromView(viewResult, viewName, Model);
        byte[] fileContent = WkhtmltopdfDriver.ConvertHtml(this.WkhtmlPath, this.GetConvertOptions(), html);
        return fileContent;
    }

What am i doing wrong ?

emerog
  • 69
  • 3
  • 10

2 Answers2

0

Rotativa does not seem to be suitable for your scenario. It would be easier to use an alternative library like DinkToPdf instead, which doesn't need neither view nor controller context. You should be able to use the HTML from your database with it.

If you really need to use Rotativa, you could try the following:

  • add a dummy view to which you pass the HTML from your database using a ViewModel
  • create a dummy context, as shown in this question
user7217806
  • 2,014
  • 2
  • 10
  • 12
0

Try this: from API controller called MVC controller it worked for me

API Controller

public class DocumentsController : ApiController
{
        public HttpResponseMessage Pdf()
        {
            PDFController controller = new PDFController();
            RouteData route = new RouteData();
            route.Values.Add("action", "getPdf"); // ActionName
            route.Values.Add("controller", "PDF"); // Controller Name
            System.Web.Mvc.ControllerContext newContext = new 
            System.Web.Mvc.ControllerContext(new HttpContextWrapper(System.Web.HttpContext.Current), route, controller);
            controller.ControllerContext = newContext;
            var actionPDF = controller.getPdf(); 
            HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
            response.Content = new ByteArrayContent(actionPDF);// new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
            response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
            response.Content.Headers.ContentDisposition.FileName = "SamplePDF.PDF";
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
            return response;
        }            
}

MVC Controller

    public class PDFController : Controller
    {
            public Byte[]  getPDF()
            {
            var actionPDF = new Rotativa.ViewAsPdf("YOUR_VIEW_NAME") )
            {

                PageSize = Rotativa.Options.Size.A4,
                PageOrientation = Rotativa.Options.Orientation.Landscape,
               PageMargins = { Left = 1, Right = 1 }
            };
            byte[] applicationPDFData = actionPDF.BuildPdf(this.ControllerContext);
            return applicationPDFData;
        }
   }
Santosh P
  • 125
  • 1
  • 3