winnovative pdf - 内存不足和其他随机异常
我们有一个 asp.net 3.5 应用程序,允许用户生成许多图表并通过 pdf 导出。这对于较小的 pdf(少于 100 页)来说效果很好,但是当我们处理较大的 pdf 时,我们会遇到随机错误。我们看到的一些错误是:
--System.OutOfMemoryException
--无法呈现 HTML 字符串。无法从 html 字符串获取图像。尝试设置 LoadHtmlConcurrencyLevel = 1..
--克隆错误。没内存了..
--等待转换完成超时。
--System.OutOfMemoryException:内存不足。 在 System.Drawing.Image.FromStream(流流,布尔值 useEmbeddedColorManagement,布尔值 validateImageData) 在 System.Drawing.Image.FromStream(Stream stream)
如果我多次运行大报告,我通常会得到不同的异常。有时我可能会导致 IIS 崩溃,并且我必须执行 iisreset 才能恢复应用程序。
这是我们运行的代码。我们使用图表(png 图像)创建 PDF 文档,然后将其导出到字节数组并将其放入内存流中。我们将内存流传递给旋转一些图像等的函数,然后调用 doc.save 方法将其导出。
Dim mainPageBytes() As Byte = PDF.GetBytes
Dim stream As New System.IO.MemoryStream(mainPageBytes)
Dim existingDoc As New PDFCreator.Document(stream)
Dim doc As PDFCreator.Document = GetDocument(mainPageBytes, GetChartingPageNumbers(PDF.ConversionSummary), pageOrientation, user, existingDoc)
doc.Save(response, True, Me.DocumentName)
we have an asp.net 3.5 application that allows users to generate many charts and export them via pdf. This works fine for smaller pdfs (less than 100 pages), but when we do larger ones, we get random errors. some of the errors we have seen are:
--System.OutOfMemoryException
--Could not render the HTML string. Could not get image from html string. Try to set LoadHtmlConcurrencyLevel = 1..
--Clone error. Out of memory..
--Timeout waiting for conversion to finish.
--System.OutOfMemoryException: Out of memory.
at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
at System.Drawing.Image.FromStream(Stream stream)
If I run the big report multiple times, i usually get different exceptions. Sometimes I can get IIS to crash and I have to do a iisreset to get the application back up.
Here is the code we run. We create the PDF document with the charts (png images) and then export it out to a byte array and put it in a memorystream. We pass the memory stream to a function that rotates some of the images, etc and then call the doc.save method to export it.
Dim mainPageBytes() As Byte = PDF.GetBytes
Dim stream As New System.IO.MemoryStream(mainPageBytes)
Dim existingDoc As New PDFCreator.Document(stream)
Dim doc As PDFCreator.Document = GetDocument(mainPageBytes, GetChartingPageNumbers(PDF.ConversionSummary), pageOrientation, user, existingDoc)
doc.Save(response, True, Me.DocumentName)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
IIS 对在其上运行的脚本有内存和运行时方面的限制。据推测,您的脚本超出了运行时和/或内存限制。这些可以在 IIS 配置设置中进行设置,但它们的存在通常是有原因的(为了防止单个脚本耗尽服务器上的所有内存,或者防止脚本在无限循环中永远运行,您可能会这样做)除非重新启动 IIS,否则无法退出。)
打开调试(这会禁用这些限制)并通过将
queryObj("PeakWorkingSetSize")
输出到日志档案。IIS has limits on the scripts that run on it, both for memory and runtime. Presumably your script is going over the runtime and/or memory limits. These can be set in the IIS configuration settings, but they're generally there for a reason (to prevent a single script from eating up all the memory on the server, or to prevent a script from running forever in an infinite loop which you would have no way of exiting short of restarting IIS.)
Turn on debugging (which disables those limits) and determine how much memory your scripts are actually using when they crash by outputing
queryObj("PeakWorkingSetSize")
to a log file.您是否按照产品文档中的建议在 64 位进程中运行转换器?您可以在我们的在线查看部署要求文档。在 32 位模式下,.NET 的可用内存非常有限。在 IIS 中,您必须确保 32 位应用程序标志为 false。
此外,为了减少转换包含大量大图像的 HTML 页面时的内存使用量,您可以将 ImagesScalingEnabled 属性设置为 false。您可以在设置图像缩放和JPEG 压缩级别演示。
Do you run the converter in a 64-bit process as recommended in product documentation? You can check the deployment requirements in our online documentation. In 32-bit mode the available memory for .NET is quite limited. In IIS you have to have to make sure that the 32-bit applications flag is false.
Also, in order to reduce the memory usage when converting HTML pages with many and large images you can set the ImagesScalingEnabled property to false. You can find a complete sample code for this feature in set images scaling and JPEG compression level demo.