如何使用异步和多线程生成 PDF

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

异步和线程在生成以下内容时非常有用使用 IronPDF for .NET 在 C# 和 VB.NET 中创建高性能 PDF 文件在翻译过程中,您可以分批或优化性能。

开始使用IronPDF

立即在您的项目中开始使用IronPDF,并享受免费试用。

第一步:
green arrow pointer



异步示例

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模式在批量处理PDFs时非常有帮助。

: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 渲染的 WaitFor 类. 以下是上述各种技术性能的比较表。

正常渲染异步渲染多线程渲染
5.75 秒5.59 秒5.68 秒