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
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.