如何使用異步和多線程生成 PDF

This article was translated from English: Does it need improvement?
Translated
View the article in English

異步和多線程在生成 在 C# 和 VB.NET 中處理 PDF 文件 批量處理或高效能。


C# NuGet 程式庫用于 PDF

安裝與 NuGet

Install-Package IronPdf
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

C# NuGet 程式庫用于 PDF

安裝與 NuGet

Install-Package IronPdf
Java PDF JAR

下載 DLL

下載DLL

手動安裝到您的項目中

立即開始在您的專案中使用IronPDF,並享受免費試用。

第一步:
green arrow pointer

查看 IronPDFNuget 快速安裝和部署。已被下載超過800萬次,它正用C#改變PDF。

C# NuGet 程式庫用于 PDF nuget.org/packages/IronPdf/
Install-Package IronPdf

請考慮安裝 IronPDF DLL 直接下載並手動安裝到您的專案或GAC表單: IronPdf.zip

手動安裝到您的項目中

下載DLL

非同步範例

IronPDF 完全支援非同步,包含其非同步渲染方法,如 RenderHtmlAsPdfAsync 方法。

:path=/static-assets/pdf/content-code-examples/how-to/async-async.cs
using IronPdf;
using System.Threading.Tasks;

// Instantiate ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

string[] htmlStrings = {"<h1>Html 1</h1>", "<h1>Html 2</h1>", "<h1>Html 3</h1>"};

// Create an array to store the tasks for rendering
var renderingTasks = new Task<PdfDocument>[htmlStrings.Length];

for (int i = 0; i < htmlStrings.Length; i++)
{
    int index = i; // Capturing the loop variable
    renderingTasks[i] = Task.Run(async () =>
    {
        // Render HTML to PDF
        return await renderer.RenderHtmlAsPdfAsync(htmlStrings[index]);
    });
}

// Wait for all rendering tasks to complete
// await Task.WhenAll(renderingTasks);
Imports IronPdf
Imports System.Threading.Tasks

' Instantiate ChromePdfRenderer
Private renderer As New ChromePdfRenderer()

Private htmlStrings() As String = {"<h1>Html 1</h1>", "<h1>Html 2</h1>", "<h1>Html 3</h1>"}

' Create an array to store the tasks for rendering
Private renderingTasks = New Task(Of PdfDocument)(htmlStrings.Length - 1){}

For i As Integer = 0 To htmlStrings.Length - 1
	Dim index As Integer = i ' Capturing the loop variable
	renderingTasks(i) = Task.Run(Async Function()
		' Render HTML to PDF
		Return Await renderer.RenderHtmlAsPdfAsync(htmlStrings(index))
	End Function)
Next i

' Wait for all rendering tasks to complete
' await Task.WhenAll(renderingTasks);
VB   C#

多執行緒範例

IronPDF 是執行緒安全的,並且在使用 IronPdf.ChromePdfRenderer 渲染引擎時支援多執行緒。

一個限制是多執行緒在 macOS 機器上有一定的限制。

我們發現 Parallel.ForEach 模式在批次處理 PDF 時非常有幫助。

:path=/static-assets/pdf/content-code-examples/how-to/async-multi-thread.cs
using IronPdf;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;

var queue = new List<string>() { "<h1>Html 1</h1>", "<h1>Html 2</h1>", "<h1>Html 3</h1>" };

// Instantiate ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Create a list to store the rendered PDFs
List<PdfDocument> pdfResults = new List<PdfDocument>();

Parallel.ForEach(queue, html =>
{
    // Render HTML to PDF
    PdfDocument pdf = renderer.RenderHtmlAsPdf(html);

    // You may choose to save the PDF to disk here if needed
    // For this example, we'll store it in the pdfResults list
    lock (pdfResults)
    {
        pdfResults.Add(pdf);
    }
});
Imports IronPdf
Imports System.Collections.Concurrent
Imports System.Collections.Generic
Imports System.Threading.Tasks

Private queue = New List(Of String)() From {"<h1>Html 1</h1>", "<h1>Html 2</h1>", "<h1>Html 3</h1>"}

' Instantiate ChromePdfRenderer
Private renderer As New ChromePdfRenderer()

' Create a list to store the rendered PDFs
Private pdfResults As New List(Of PdfDocument)()

Parallel.ForEach(queue, Sub(html)
	' Render HTML to PDF
	Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)

	' You may choose to save the PDF to disk here if needed
	' For this example, we'll store it in the pdfResults list
	SyncLock pdfResults
		pdfResults.Add(pdf)
	End SyncLock
End Sub)
VB   C#

性能比較

讓我們進行比較。另外,我在渲染中添加了 5 秒的延遲 等待 類來模擬渲染更複雜的HTML。以下是使用上述各種技術的性能比較表。

正常渲染非同步渲染多執行緒渲染
15.75 秒05.59 秒05.68秒