Fixing PDF Rendering Timeout Issues

This article was translated from English: Does it need improvement?
Translated
View the article in English
  • 无法从 HTML 渲染 PDF
  • 无法从 URL 渲染 PDF
  • 从 HTML 生成 PDF 时出错
  • 从 URL 生成 PDF 时出错
  • 未指定的本机异常

这些通用错误消息表明 Chrome 渲染引擎遇到故障。

应用以下代码片段将生成一个“Default.log”日志文件,帮助确定故障原因。

// Enable logging for all events and set the file path for the log file.
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
IronPdf.Logging.Logger.LogFilePath = "Default.log";
// Enable logging for all events and set the file path for the log file.
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All;
IronPdf.Logging.Logger.LogFilePath = "Default.log";
' Enable logging for all events and set the file path for the log file.
IronPdf.Logging.Logger.LoggingMode = IronPdf.Logging.Logger.LoggingModes.All
IronPdf.Logging.Logger.LogFilePath = "Default.log"
$vbLabelText   $csharpLabel

渲染超时

超时是 IronPDF 实现的一项功能,旨在防止应用程序由于长时间的 PDF 渲染持续时间而发生死锁和无响应。 这可以通过管理资源帮助用户,确保诸如 CPU、内存和网络带宽等资源不被耗尽于需要太长时间完成的进程。 如果 PDF 渲染过程未能及时完成,将抛出以下所述的超时错误。

  • 从 HTML 渲染 PDF 时超时 60 秒
  • 从 URL 渲染 PDF 时超时 60 秒
  • 从 HTML 生成 PDF 时超时
  • 从 URL 生成 PDF 时超时
  • 从 HTML 渲染 PDF 时超时
  • 从 URL 渲染 PDF 时超时

在 IronPDF 中,导致超时错误的渲染时间长的原因很多,例如渲染大型或复杂的 PDF,检索远程资源/素材以进行 PDF 渲染,或渲染过程中本身的问题。 然而,通常通过覆盖默认超时时间可以解决问题。

为了覆盖默认设置,用户可以在 ChromePdfRenderOptions 类中访问 Timeout 属性 并将其设置为更高的值。

// Set the timeout for rendering to 120 seconds.
RenderingOptions.Timeout = 120; // seconds
// Set the timeout for rendering to 120 seconds.
RenderingOptions.Timeout = 120; // seconds
' Set the timeout for rendering to 120 seconds.
RenderingOptions.Timeout = 120 ' seconds
$vbLabelText   $csharpLabel

请注意从版本2021.12.4995开始,IronPDF的默认渲染超时时间为60秒。

渲染延迟

在某些情况下,IronPDF 可能会渲染:

  • 空白 PDF
  • 内容不完整的 PDF
  • 缺少图像资产的 PDF

此问题可能是因为 PDF 文档在调用获取内容或资产的资源完成之前被渲染。

为 PDF 渲染器应用渲染延迟通常有助于解决问题。 这将指示渲染器延迟 PDF 渲染,以便内容或资产可以及时加载,然后再进行 PDF 渲染。 为此,只需在 RenderDelay() 方法中访问 WaitFor 包装对象,将所需的持续时间作为方法参数进行设置,然后将其作为 ChromePdfRenderOptions 的属性之一应用于渲染器。

// Set a render delay of 5000 milliseconds (5 seconds) to ensure content and assets are loaded before rendering.
RenderingOptions.WaitFor.RenderDelay(5000);  // milliseconds
// Set a render delay of 5000 milliseconds (5 seconds) to ensure content and assets are loaded before rendering.
RenderingOptions.WaitFor.RenderDelay(5000);  // milliseconds
' Set a render delay of 5000 milliseconds (5 seconds) to ensure content and assets are loaded before rendering.
RenderingOptions.WaitFor.RenderDelay(5000) ' milliseconds
$vbLabelText   $csharpLabel

请注意默认的渲染延迟持续时间为 20 毫秒

除了 RenderDelay() 方法外,WaitFor 包装对象中还有其他可用的方法。

如需有关日志和如何向我们的工程师报告错误以进行审查的更多信息,请参阅 如何为 IronPDF 提出工程支持请求

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。

准备开始了吗?
Nuget 下载 16,154,058 | 版本: 2025.11 刚刚发布