Rotativa PDF生成在本地工作,但在IIS 7上无法工作

36

我有一个ASP.NET MVC 5应用程序。它有一个简单的表单,当用户提交它时,表单被存储在缓存中,然后调用另一个控制器动作,此动作将该模型作为视图返回为PDF.

我不需要将其本地保存,只需在浏览器中显示以便用户下载即可。

也不需要数据库,这就是为什么我将模型存储在缓存中的原因。

在Visual Studio 2013本地上,它可以正常工作。但是当我将它发布到IIS 7时,我会遇到一个未处理的执行错误。以下是我获得的堆栈跟踪:

 Unhandled Execution Error
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Exception:

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
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +109
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilterRecursive(IList`1 filters, Int32 filterIndex, ResultExecutingContext preContext, ControllerContext controllerContext, ActionResult actionResult) +890
   System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +97
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +241
   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


Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34248 

我最开始使用的是ActionAsPDF(),但现在我也尝试使用return new ViewAsPDF()。它们都给出了相同的错误和堆栈跟踪。这是否与权限有关?


它真的会抛出 System.Exception 而不是其他派生类吗?这将非常糟糕。 - leppie
这是我收到的所有内容,由于我无法在本地复制它,所以我不知道如何进行调试。 - Robin
栈的第一行似乎有一个参数,听起来像是路径名,我以前在路径和IIS方面遇到过麻烦,这可能是个问题吗? - Robin
1
可能服务器缺少VC++ redist,如此处所示:http://jwhijazi.blogspot.nl/2015/05/solving-rotativa-unhandled-exception.html - rene
谢谢,这确实解决了问题! - Robin
显示剩余2条评论
7个回答

58

这个错误是由于缺少 C++ redistributable Package for Visual Studio 2013 文件中的 (MSVCP120.dll) 文件引起的。

解决方法:

  1. 打开以下可下载的 C++ redistributable Package for Visual Studio 2013 链接: 下载Visual C++ Redistributable Packages for Visual Studio 2013

  2. 点击“下载”,即使您使用的是X64位服务器版本,也要选择文件 (vcredist_x86.exe)。

  3. 安装该文件。

查看Jalal W Hijazi Blog http://jwhijazi.blogspot.com/2015/05/solving-rotativa-unhandled-exception.html了解更多细节。


13
对我起作用的Jalal博客上的提示:「我只是将msvcp120.dll和msvcr120.dll复制到包含wkhtmltopdf.exe的文件夹中,然后一切正常运行。」 - HugoXP
@HugoXP 你应该将这写成一个单独的答案 - 太棒了! - fubo
1
当前的Rotativa需要Visual Studio 2015的Visual C++。 - JoeyZero
5
C++ Redistributable 2013已经失效,我不得不安装Microsoft Visual C++ Redistributable for Visual Studio 2017 (https://support.microsoft.com/en-my/help/2977003/the-latest-supported-visual-c-downloads)才能使其正常运行。 - Rosdi Kasim

18

如答案中链接的博客中所述,如果您不想或无法轻松地在服务器上安装东西,则可以将以下 2 个 dll 文件复制到服务器上的 rotativa 文件夹中。

msvcr120.dll
msvcp120.dll

如果在本地运行正常,应该已经有这两个 dll 文件可用。如果您找到的是与 vs 2015 打包的 x64 版本,那么您需要 x86 版本,因为它们无法正常工作。我在这里找到了正确的版本:

C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\redist\x86\Microsoft.VC120.CRT

升级到 Rotativa 1.7.3 后,这段代码不再起作用。我不得不从 Visual Studio 2017 中复制一些新的 DLL 文件,并将它们放在这里:

C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\redist\x86\Microsoft.VC140.CRT

我不得不复制以下文件:

msvcp140.dll
vcruntime140.dll


1
这对我有用。在服务器上,我尝试包含x64 dlls。然而,它们没有解决问题。所以切换到x86 dlls,它们像魔法一样工作了。非常感谢。 - Sugar Bowl
2
Visual Studio 2019:在此处找到文件: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE - GurjeetSinghDB

10

对我来说,被接受的答案几乎起作用了。 我缺少更新版本的C ++可再发行程序,因此安装旧版本无济于事。

找出你缺少什么最简单的方法是手动运行wkhtmltopdf.exe。 它将提供控制台输出(如果正常工作)或有关缺少什么的消息框。

我缺少MSVCP140.dll,它来自VS 2015 redist,可在此处找到:https://www.microsoft.com/en-gb/download/details.aspx?id=48145


1
如果其他人遇到了这个问题,在此时(2019年6月),我也需要安装它并发现您必须安装x86版本。仅安装x64版本是不起作用的。 - Mark Embling

8

我通过更新从这里下载的安装程序中的wkhtmltopdf文件解决了问题。

enter image description here

现在完美地工作!


哦,这是在 Azure 托管的机器上运行的。服务器 2012 虚拟机并不在我的笔记本电脑上本地运行。 - jamdownian

7
当我从Rotativa 1.6.4升级到1.7.3时,我的网站应用程序上的PDF转换停止工作了。在生产服务器上运行wkhtmltopdf.exe后,我注意到它与msvcp140.dll有问题。
解决方法是安装Visual C++ Redistributable for Visual Studio 2015。 https://www.microsoft.com/en-us/download/details.aspx?id=48145

在升级到1.7.3版本后,我也遇到了同样的问题。安装x86 vs 2015可再发行组件解决了这个问题。 - Rosdi Kasim

2
我遇到了同样的问题。即使错误记录器也没有记录任何内容。
经过一些研究,我尝试用wkhtmltopdf.org网站上最新的版本替换wkhtmltopdf.exe,但仍然没有运行成功。
最后,我将msvcp120.dllmsvcr120.dll复制到可执行文件所在的文件夹中,它就能正常工作了(不需要最新版本的wkhtmltopdf.exe,而是使用了我从Nuget得到的版本)。
在Windows 7中,msvcp120.dllmsvcr120.dll位于以下目录中。

C:\Windows\SysWOW64\


1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接