How do I use Async & MultiThreading for Html-To-PDF?

Async and Threading is useful when genearting PDFs in C# and VB.Net in batches or for high performance.

Async

IronPDF fully supports .Net Framework 4, and as such working with async functions requires a few extra lines of code. Task.Run requires .Net Framework 4.5+, or for the Microsoft Byte-Code Library to be installed into a .Net 4 project via NuGet.

//using IronPdf;

private static async Task<IronPdf.PdfDocument> RenderPdfAsync(string Html, IronPdf.PdfPrintOptions PrintOptions = null)
{
    return await Task.Run(() => RenderPdf(Html, PrintOptions));
}

private static IronPdf.PdfDocument RenderPdf(string Html, IronPdf.PdfPrintOptions PrintOptions = null)
{
    var Renderer = new IronPdf.HtmlToPdf();
    if (PrintOptions != null)
    {
        Renderer.PrintOptions = PrintOptions;
    }

    PdfDocument Pdf = Renderer.RenderHtmlAsPdf(Html);

    return Pdf;
}
'using IronPdf;

Private Shared Async Function RenderPdfAsync(ByVal Html As String, Optional ByVal PrintOptions As IronPdf.PdfPrintOptions = Nothing) As Task(Of IronPdf.PdfDocument)
	Return Await Task.Run(Function() RenderPdf(Html, PrintOptions))
End Function

Private Shared Function RenderPdf(ByVal Html As String, Optional ByVal PrintOptions As IronPdf.PdfPrintOptions = Nothing) As IronPdf.PdfDocument
	Dim Renderer = New IronPdf.HtmlToPdf()
	If PrintOptions IsNot Nothing Then
		Renderer.PrintOptions = PrintOptions
	End If

	Dim Pdf As PdfDocument = Renderer.RenderHtmlAsPdf(Html)

	Return Pdf
End Function
VB   C#

Multi-Threading

For high performance, IronPDF fully supports multithreading. The easiest way to achieve this by using the native command Paralllel.ForEach

https://docs.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-write-a-parallel-foreach-loop-with-partition-local-variables