Multi Threaded Generation
IronPDF provides thread-safe PDF generation and offers support for multi-threading processes when it comes to generating PDF documents with the ChromePdfRenderer
class as seen in this example. This way, you can handle PDF generation tasks of any size, or batch PDF generation tasks, concurrently without the fear of running into issues such as thread blocking.
Steps to Multi-Threaded PDF Generation
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IronPdf;
public class PdfGenerationExample
{
public async Task GeneratePdfsAsync()
{
// Create an instance of ChromePdfRenderer
var renderer = new ChromePdfRenderer();
// Define a list of HTML strings to be converted into PDFs
var htmlStrings = new List<string>()
{
"<h1>Html#1</h1>",
"<h1>Html#2</h1>",
"<h1>Html#3</h1>"
};
// Use asynchronous programming to render HTML strings to PDFs concurrently
Task<PdfDocument[]> task = Task.WhenAll(
htmlStrings.Select(html => renderer.RenderHtmlAsPdfAsync(html))
);
// Retrieve the results when all tasks are complete
List<PdfDocument> pdfList = (await task).ToList();
// Further manipulation of the PDF documents can be done with pdfList
}
}
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IronPdf;
public class PdfGenerationExample
{
public async Task GeneratePdfsAsync()
{
// Create an instance of ChromePdfRenderer
var renderer = new ChromePdfRenderer();
// Define a list of HTML strings to be converted into PDFs
var htmlStrings = new List<string>()
{
"<h1>Html#1</h1>",
"<h1>Html#2</h1>",
"<h1>Html#3</h1>"
};
// Use asynchronous programming to render HTML strings to PDFs concurrently
Task<PdfDocument[]> task = Task.WhenAll(
htmlStrings.Select(html => renderer.RenderHtmlAsPdfAsync(html))
);
// Retrieve the results when all tasks are complete
List<PdfDocument> pdfList = (await task).ToList();
// Further manipulation of the PDF documents can be done with pdfList
}
}
Imports System.Collections.Generic
Imports System.Linq
Imports System.Threading.Tasks
Imports IronPdf
Public Class PdfGenerationExample
Public Async Function GeneratePdfsAsync() As Task
' Create an instance of ChromePdfRenderer
Dim renderer = New ChromePdfRenderer()
' Define a list of HTML strings to be converted into PDFs
Dim htmlStrings = New List(Of String)() From {"<h1>Html#1</h1>", "<h1>Html#2</h1>", "<h1>Html#3</h1>"}
' Use asynchronous programming to render HTML strings to PDFs concurrently
Dim task As Task(Of PdfDocument()) = System.Threading.Tasks.Task.WhenAll(htmlStrings.Select(Function(html) renderer.RenderHtmlAsPdfAsync(html)))
' Retrieve the results when all tasks are complete
Dim pdfList As List(Of PdfDocument) = (Await task).ToList()
' Further manipulation of the PDF documents can be done with pdfList
End Function
End Class
The first step in generating PDF documents in a multi-threaded environment is to create a new ChromePdfRenderer
instance. This gives us access to IronPDF's powerful rendering engine, capable of producing pixel-perfect PDF documents. Then, we will create a new List of string objects, called htmlStrings. This list is full of the HTML strings that we will be generating PDF documents from in our Multi-Threaded environment.
Next, we need to use asynchronous programming to render all of our HTML strings to PDF files concurrently. htmlStrings.Select(html => renderer.RenderHtmlAsPdfAsync(html))
iterates over our list of HTML strings and calls the RenderHtmlAsPdfAsync
method, accessed through the renderer, on each string found, converting them into PDF documents. Task.WhenAll()
is used to accumulate the asynchronous tasks into a single task that will be completed once all of the individual RenderHtmlAsPdfAsync
calls are finished. Then, it will return a new array of PdfDocument
objects.
Finally, we will retrieve the results of our PDF generation. await task
ensures that the current asynchronous method waits until all the asynchronous PDF rendering tasks are complete and retrieves the resulting array of PdfDocument
objects. ToList()
then converts the array of PdfDocument
objects into the pdfList List for easier further manipulation of the PDF documents.
Click here to view the How-to Guide, including examples, sample code, and files