解決 PDF 渲染超時問題

This article was translated from English: Does it need improvement?
Translated
View the article in English
  • 無法從 HTML 渲染 PDF
  • 無法從 URL 渲染 PDF
  • 從 HTML 生成 PDF/A 時發生錯誤
  • 從 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

渲染超時

超時 (Timeout) 是 IronPDF 中的一項功能,旨在防止應用程式因 PDF 渲染時間過長而陷入死鎖或無回應的狀態。 這有助於使用者管理資源,確保 CPU、記憶體和網路頻寬等資源不會被耗時過長的程序佔用。 若 PDF 渲染過程未能及時完成,將拋出如下所述的超時錯誤。

  • 從 HTML 渲染 PDF/A 時,_60 秒_後超時
  • 從 URL 渲染 PDF/A 時,_60 秒_後超時
  • 從 HTML 生成 PDF/A 時發生超時
  • 從 URL 生成 PDF 時發生超時
  • 將 HTML 轉為 PDF 時發生超時
  • 從 URL 渲染 PDF/A 時發生超時

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 渲染前及時載入內容或資源。 要達成此目的,只需存取 WaitFor 封裝物件中的 RenderDelay() 方法,將所需的延遲時間設為方法參數,並將其作為 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

請注意預設的 RenderDelay 持續時間為 20 毫秒

除了 RenderDelay() 方法外,WaitFor 封裝物件中還有其他可供使用的方法。

如需進一步了解日誌相關資訊,以及如何向我們的工程師回報錯誤以供審查,請參閱《如何針對 IronPDF 提交工程支援請求》

Curtis Chau
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

除了開發工作之外,Curtis 對物聯網(IoT)抱有濃厚興趣,致力於探索整合硬體與軟體的創新方法。閒暇時,他喜歡玩遊戲和開發 Discord 機器人,將對科技的熱愛與創意相結合。

準備開始了嗎?
Nuget 下載 19,014,616 | 版本: 2026.5 just released
Still Scrolling Icon

還在往下捲動嗎?

想要快速確認成果嗎? PM > Install-Package IronPdf
執行範例 觀看您的 HTML 轉為 PDF。