HTML-to-PDF in C# .NET using Async & MultiThreading

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

Async

IronPDF fully supports Async using its Async Rendering methods such as ChromeRenderingEngine.RenderHtmlAsPdfAsync

//using IronPdf;

var Renderer = new IronPdf.ChromePdfRenderer();
await var doc = Renderer.RenderHtmlAsPdfAsync("<h1>Html with CSS and Images</h1>");
doc.SaveAs("pixel-perfect.pdf");
//using IronPdf;

var Renderer = new IronPdf.ChromePdfRenderer();
await var doc = Renderer.RenderHtmlAsPdfAsync("<h1>Html with CSS and Images</h1>");
doc.SaveAs("pixel-perfect.pdf");
'using IronPdf;

Dim Renderer = New IronPdf.ChromePdfRenderer()
Await var doc = Renderer.RenderHtmlAsPdfAsync("<h1>Html with CSS and Images</h1>")
doc.SaveAs("pixel-perfect.pdf")
VB   C#

Multi-Threading

IronPDF is thread safe and supports multithreading when using the IronPdf.ChromePdfRenderer rendering engine.

One limitation is that multithreading is limited on MacOS machines.

We find the Parallel.ForEach pattern very helpful for batch processing PDFs.

using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

var que = new List<string>(){ "<h1>Html</h1>", "<h1>Html with CSS and Images</h1>"};

var Renderer = new IronPdf.ChromePdfRenderer();

Parallel.ForEach(que, html =>
{
   var doc = Renderer.RenderHtmlAsPdfAsync("<h1>Html with CSS and Images</h1>");
   //....save to disk
});
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

var que = new List<string>(){ "<h1>Html</h1>", "<h1>Html with CSS and Images</h1>"};

var Renderer = new IronPdf.ChromePdfRenderer();

Parallel.ForEach(que, html =>
{
   var doc = Renderer.RenderHtmlAsPdfAsync("<h1>Html with CSS and Images</h1>");
   //....save to disk
});
Imports System
Imports System.Collections.Concurrent
Imports System.Collections.Generic
Imports System.Diagnostics
Imports System.Linq
Imports System.Threading.Tasks

Private que = New List(Of String)() From {"<h1>Html</h1>", "<h1>Html with CSS and Images</h1>"}

Private Renderer = New IronPdf.ChromePdfRenderer()

Parallel.ForEach(que, Sub(html)
   Dim doc = Renderer.RenderHtmlAsPdfAsync("<h1>Html with CSS and Images</h1>")
   '....save to disk
End Sub)
VB   C#