【问题标题】:ASP.NET Webforms Nreco PDF Generator - Exit with code 1, due to unknown errorASP.NET Webforms Nreco PDF 生成器 - 由于未知错误,退出并显示代码 1
【发布时间】:2020-11-11 22:16:44
【问题描述】:

我有一个内置在 ASP.NET Webforms VB.NET 中的 Web 应用程序。我正在创建一种利用 WKHTMLTOPDF 和 NReco PDF Generator 库通过 url 导出页面的方法。

在我的本地开发过程中,除了奇怪的“进程已在使用”之外,当我重新启动我的电脑并且之前什至没有执行任何操作时,一切似乎都正常。

当我将网站上传到我们的服务器时,PDF 库似乎永远无法工作,我遇到了一个不太明显的错误。当我尝试将任何页面导出为任何内容,甚至将文本导出为 pdf 时,我会在我们的日志中看到:

Inner Exception Type: NReco.PdfGenerator.WkHtmlToPdfException
Inner Exception: Exit with code 1, due to unknown error. (exit code: 1)
Inner Source: NReco.PdfGenerator
Inner Stack Trace: 
   at NReco.PdfGenerator.HtmlToPdfConverter.CheckExitCode(Int32 exitCode, String lastErrLine, Boolean outputNotEmpty)
   at NReco.PdfGenerator.HtmlToPdfConverter.InvokeWkHtmlToPdf(PdfSettings pdfSettings, String inputContent, Stream outputStream)
   at NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdfInternal(WkHtmlInput[] htmlFiles, String inputContent, String coverHtml, String outputPdfFilePath, Stream outputStream)

Exception Type: System.Exception
Exception: Cannot generate PDF: Exit with code 1, due to unknown error. (exit code: 1)
Source: Exporting
Stack Trace: 
   at NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdfInternal(WkHtmlInput[] htmlFiles, String inputContent, String coverHtml, String outputPdfFilePath, Stream outputStream)
   at NReco.PdfGenerator.HtmlToPdfConverter.GeneratePdf(String htmlContent, String coverHtml, String outputPdfFilePath)
   at PROJECT.Export.DownloadFiles(List`1 list) in C:\Users\NAME\source\repos\PROJECT\Export.aspx.vb:line 252.

我已经确保项目包含所有相关文件,我什至在服务器和开发机器上都安装了 WKHTMLTOPDF。利用 CMD,我可以生成任何 PDF,但在我的应用程序中,它似乎总是在我们的实时生产服务器上失败。

我生成 PDF 的代码非常简单:

If check = True Then
                Try
                    'Generate PDF
                    'htmlToPdf.GeneratePdfFromFile(exp.URL, Nothing, "..\" & pathURL)
                    htmlToPdf.GeneratePdf("<h3> TEST PDF </h3> ", Nothing, "..\" & pathURL)

                    'Set download
                    download = True

                    'Catch exception, log it
                Catch ex As Exception
                    ExceptionUtility.LogException(ex, "Exporting")
                End Try

End If

谁能提供任何关于我如何克服这个问题的见解?我昨天花了一整天的时间在论坛上搜索,但没有找到解决方案!

非常感谢任何帮助。

【问题讨论】:

  • 这看起来像一个类似的问题 - 你检查了吗? stackoverflow.com/questions/52980828/…
  • 我将尝试安装任何丢失的 C++ 文件,然后重试!
  • 仅供参考我在服务器上的当前文件在图像中:imgur.com/a/jQE4vVQ
  • 另外,我不确定,如果这些文件不存在,wkhtmltopdf 是否可以通过服务器上的命令行工作?我可以通过 CMD 生成 PDF,但不能通过托管的 Web 应用程序。
  • @Homungus 我设法安装了所有丢失的文件,但不幸的是我没有发现任何变化。正如我所怀疑的,当我的本地版本完美运行时,wkhtmltopdf 通过命令行工作,而不是通过我的托管站点。

标签: asp.net vb.net pdf wkhtmltopdf nreco


【解决方案1】:

退出代码 1 表示 wkhtmltopdf 由于某种原因无法呈现您的 HTML 输入。 要获取更多详细信息(完整的 wkhtmltopdf 的控制台日志输出),您可以处理 LogReceived 事件。

PdfGenerator 使用 System.Diagnostics.Process API 执行 wkhtmltopdf,因此它的工作方式应该与在命令行中运行它(在同一台服务器上)完全相同。如果这是托管在 IIS 中的 ASP.NET 应用程序,则可能会出现差异,并且在某些情况下,应用程序池可能被配置为使用相当严格的凭据。

【讨论】:

  • 感谢您的回复。我会调查这个 LogReceived 事件。我们也通过 IIS 来托管它,而不是通过任何其他方式。
  • 由于某种原因,LogRecieved 似乎不作为 HtmlToPdf 的成员存在。我正在为此使用 NReco,但找不到生成您提到的此日志的方法。
  • LogReceived 是 HtmlToPdfConverter 类的成员:nrecosite.com/doc/NReco.PdfGenerator/?topic=html/…
  • 感谢您的回复,我可以通过您的网站看到这存在。但是,使用如下代码,我在代码中看不到此选项... Dim htmlToPdf2 = New NReco.PdfGenerator.HtmlToPdfConverter() htmlToPdf2.LogRecieved - 不存在 :(
  • @Kyran 我不知道为什么你看不到它,“LogReceived”事件在 HtmlToPdfConverter 类中 100% 存在(在所有版本中)。
猜你喜欢
  • 2017-06-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-01
相关资源
最近更新 更多