How to Generate PDFs with Async and Multithreading
Async and threading are useful when genearting PDFs in C# and VB.NET in batches or for high performance.
How to Generate PDFs with Async and Multithreading
Install with NuGet
Install-Package IronPdf
Download DLL
Manually install into your project
Install with NuGet
Install-Package IronPdf
Download DLL
Manually install into your project
Start using IronPDF in your project today with a free trial.
Check out IronPDF on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming PDF with C#.
Install-Package IronPdf
Consider installing the IronPDF DLL directly. Download and manually install it for your project or GAC form: IronPdf.zip
Manually install into your project
Download DLLAsync Example
IronPDF fully supports Async using its Async Rendering methods such as the RenderHtmlAsPdfAsync
method.
: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);
Multi-Threading Example
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 have found the Parallel.ForEach
pattern very helpful for batch processing 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)
Performance Comparison
Let's perform a comparison. I have additionally added a 5-second delay in rendering with the WaitFor class to simulate rendering more complex HTML. Below is the comparison table of the performance using various techniques described above.
Normal Render | Asynchronous Render | Multithreaded Render |
---|---|---|
15.75 second | 05.59 second | 05.68 second |