MVC5 application with Rotativa does not work when

2019-09-04 14:08发布

I am using Rotativa for convert the view to PDF. It works on my local but when published on IIS server it gives error as below:

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:

[Exception]
   Rotativa.WkhtmltopdfDriver.Convert(String wkhtmltopdfPath, String switches, String html) +1364
   Rotativa.WkhtmltopdfDriver.ConvertHtml(String wkhtmltopdfPath, String switches, String html) +70
   Rotativa.ViewAsPdf.CallTheDriver(ControllerContext context) +1986
   Rotativa.AsPdfResultBase.BuildPdf(ControllerContext context) +380
   Rotativa.AsPdfResultBase.ExecuteResult(ControllerContext context) +69
   projectName.Models.ViewAsPdf2.GetByte(ControllerContext context) in C:\Users\username\Documents\Visual Studio 2015\Projects\projectName\Models\ModelClass.cs:91
   projectName.Controllers.<ModelClass>d__1.MoveNext() in C:\Users\username\Documents\Visual Studio 2015\Projects\projectName\Controllers\FormsController.cs:40
   System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +144
   System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +84
   System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +143
   System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +23
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +112
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +452
   System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +15
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +32
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +231
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +19
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +51
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

Not sure why the server is showing my local path for the controller context. I am using the default routing. Is there anything specifically that I need to modify?

Here is the code snippet that I use to pass the controller:

    ControllerContext context = new    ControllerContext(Request.RequestContext, this);
    var pdf = new ViewAsPdf2("ModelClass", model);
    byte[] pdfByteArray = pdf.GetByte(context);

Here is the code for GetByte

 public class ViewAsPdf2: PartialViewAsPdf
    {
     public ViewAsPdf2(string viewName, object model) : base (viewName, model) {          
    }
     public byte[] GetByte(ControllerContext context)
     {
            base.PrepareResponse(context.HttpContext.Response);
            base.ExecuteResult(context);
            return base.CallTheDriver(context);
        }
     }

1条回答
唯我独甜
2楼-- · 2019-09-04 14:45

After doing some research here is what I found that resolved the Rotativa not working on server issue for me:

The wkhtmltopdk.exe used by Rotativa to generate PDFs uses the following DLLs

MSVCP120.dll MSVCR120.dll

These two dlls are part of two different install able packages:

  1. MSVCP120.dll comes with Visual C++ Redistributable Packages for Visual Studio 2010 64 bit package

and,

  1. MSVCR120.dll comes with Visual C++ Redistributable Packages for Visual Studio 2013 32-bit package

    How to check if the dlls used by rotativa are in right place or not? It looks like rotativa uses dlls from C:\Windows\SysWOW64

Make sure MSVCP120.dll and MSVCR120.dll are present in the above folder. I was referring to C:\Windows\System32 folder earlier but having the dlls there did not do any good to me and added further to the confusion.

Hope this helps.

查看更多
登录 后发表回答