Parallel PDF Generation

Through IronPDF's support for asynchronous, multi-threaded PDF generation, parallel programming is a breeze. Through this, you can do two or more tasks at the same time, or use the Parallel.ForEach loop as we have in this code example for PDF generation.

Steps for Parallel PDF Generation

Here is an example that demonstrates how to use Parallel.ForEach to generate PDFs concurrently:

using System.Collections.Generic;
using System.Threading.Tasks;
using IronPdf;

public class PdfGenerator
{
    public void GeneratePdfsParallel()
    {
        // Instantiate ChromePdfRenderer to use its methods for rendering HTML to PDF.
        var renderer = new ChromePdfRenderer();

        // Define a list of HTML strings to be converted to PDF.
        var htmlStrings = new List<string> { "<h1>Html#1</h1>", "<h1>Html#2</h1>", "<h1>Html#3</h1>" };

        // Use Parallel.ForEach to process the HTML strings concurrently.
        Parallel.ForEach(htmlStrings, html =>
        {
            // Render HTML string as a PDF document.
            var pdf = renderer.RenderHtmlAsPdf(html);

            // You can save each generated PDF file or manipulate it further as needed.
            // Example: Save each PDF with a unique filename.
            pdf.SaveAs($"{html.GetHashCode()}.pdf");
        });
    }
}
using System.Collections.Generic;
using System.Threading.Tasks;
using IronPdf;

public class PdfGenerator
{
    public void GeneratePdfsParallel()
    {
        // Instantiate ChromePdfRenderer to use its methods for rendering HTML to PDF.
        var renderer = new ChromePdfRenderer();

        // Define a list of HTML strings to be converted to PDF.
        var htmlStrings = new List<string> { "<h1>Html#1</h1>", "<h1>Html#2</h1>", "<h1>Html#3</h1>" };

        // Use Parallel.ForEach to process the HTML strings concurrently.
        Parallel.ForEach(htmlStrings, html =>
        {
            // Render HTML string as a PDF document.
            var pdf = renderer.RenderHtmlAsPdf(html);

            // You can save each generated PDF file or manipulate it further as needed.
            // Example: Save each PDF with a unique filename.
            pdf.SaveAs($"{html.GetHashCode()}.pdf");
        });
    }
}
Imports System.Collections.Generic
Imports System.Threading.Tasks
Imports IronPdf

Public Class PdfGenerator
	Public Sub GeneratePdfsParallel()
		' Instantiate ChromePdfRenderer to use its methods for rendering HTML to PDF.
		Dim renderer = New ChromePdfRenderer()

		' Define a list of HTML strings to be converted to PDF.
		Dim htmlStrings = New List(Of String) From {"<h1>Html#1</h1>", "<h1>Html#2</h1>", "<h1>Html#3</h1>"}

		' Use Parallel.ForEach to process the HTML strings concurrently.
		Parallel.ForEach(htmlStrings, Sub(html)
			' Render HTML string as a PDF document.
			Dim pdf = renderer.RenderHtmlAsPdf(html)

			' You can save each generated PDF file or manipulate it further as needed.
			' Example: Save each PDF with a unique filename.
			pdf.SaveAs($"{html.GetHashCode()}.pdf")
		End Sub)
	End Sub
End Class
$vbLabelText   $csharpLabel

Explanation

  1. ChromePdfRenderer: The first thing we need to do when using parallel PDF generation with IronPDF is to create a new ChromePdfRenderer instance. This gives us access to the powerful Chromium-based rendering engine and its methods for generating high-quality PDF documents from HTML strings.

  2. HTML Strings List: Next, we define our list of HTML strings, which here contains three HTML strings that we intend to convert into PDF documents.

  3. Parallel Processing: The next step is to use Parallel.ForEach to process these strings concurrently. This allows the application to generate multiple PDF documents simultaneously, significantly improving performance when dealing with a large number of items—such as generating large batches of PDF documents.

  4. Render and Save PDFs: Inside the Parallel.ForEach block, we call the RenderHtmlAsPdf method. This method takes the current HTML string for each iteration through the htmlStrings list and converts it into a PDF document. You can then save the PDF documents or manipulate them further using IronPDF's other PDF editing features.

For more detailed information and example codes, you can refer to IronPDF's How-to Guide.