How to Generate PDFs with Async and Multithreading

Async and threading are crucial when generating high-performance PDFs in C# and VB.NET with IronPDF in batches or for optimized performance.

Get started with IronPDF

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer



Async Example

IronPDF fully supports async operation using its 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);
$vbLabelText   $csharpLabel

Multi-Threading Example

IronPDF is thread-safe and supports multithreading when using the IronPdf.ChromePdfRenderer rendering engine. Note that multithreading is limited on macOS machines.

The Parallel.ForEach pattern is particularly useful 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)
$vbLabelText   $csharpLabel

Performance Comparison

Let's perform a comparison. A 5-second delay is additionally added in rendering with the WaitFor class for simulating complex HTML rendering. Below is a comparison table of the performance using various techniques described above.

Normal Render Asynchronous Render Multithreaded Render
15.75 seconds 05.59 seconds 05.68 seconds

Frequently Asked Questions

What is the benefit of using async PDF generation?

Using async PDF generation with IronPDF allows the application to perform other tasks while waiting for the PDF to be generated, improving efficiency and responsiveness.

How can I start using async PDF generation?

You can start by downloading IronPDF from NuGet, preparing your HTML content, and using the RenderHtmlAsPdfAsync method to generate PDFs asynchronously.

Is it thread-safe for multithreading?

Yes, IronPDF is thread-safe and supports multithreading using the IronPdf.ChromePdfRenderer rendering engine, although there are limitations on macOS.

What method is used for multithreading in PDF processing?

The Parallel.ForEach method is used for multithreading with IronPDF, which is particularly useful for batch processing PDFs.

Can you provide a sample code for asynchronous PDF generation?

Yes, a sample code is provided in the guide where the RenderHtmlAsPdfAsync method is used to generate a PDF asynchronously in C# with IronPDF.

How does multithreaded PDF processing work?

Multithreaded PDF processing with IronPDF involves using the Parallel.ForEach loop to process each HTML string in parallel and save each resulting PDF to a file.

What is the performance comparison of different PDF generation techniques?

Asynchronous render with IronPDF took 5.59 seconds, multithreaded render took 5.68 seconds, while normal render took 15.75 seconds.

Is there a performance difference between asynchronous and multithreaded PDF generation?

Yes, there is a slight performance difference with IronPDF, with asynchronous render being slightly faster than multithreaded render in the provided comparison.

Why might I choose multithreading over async for PDF processing?

Multithreading with IronPDF is particularly useful for batch processing multiple PDFs simultaneously, leveraging multiple cores for efficiency.

What are the limitations of using multithreading on macOS?

The guide notes there are limitations on macOS for multithreading with IronPDF, though specifics are not detailed in the excerpt provided.

Chipego
Software Engineer
Chipego has a natural skill for listening that helps him to comprehend customer issues, and offer intelligent solutions. He joined the Iron Software team in 2023, after studying a Bachelor of Science in Information Technology. IronPDF and IronOCR are the two products Chipego has been focusing on, but his knowledge of all products is growing daily, as he finds new ways to support customers. He enjoys how collaborative life is at Iron Software, with team members from across the company bringing their varied experience to contribute to effective, innovative solutions. When Chipego is away from his desk, he can often be found enjoying a good book or playing football.