Articles in this section
Category / Section

Efficient HTML to PDF Conversion with Multithreading in .NET Core Using C#

3 mins read

The Syncfusion® HTML-to-PDF converter is a .NET PDF library designed for converting webpages, SVG, MHTML, and HTML files to PDF using C#. It leverages the Blink rendering engine (used by Google Chrome) to ensure high-fidelity rendering. The converter delivers reliable and accurate results, preserving all graphics, images, text, fonts, and the original layout of the HTML document or webpage.

This guide outlines how to optimize performance and how to handle large-scale HTML conversion tasks using Parallel.ForEach method. It ensures efficient CPU utilization and significantly reduces conversion times.

Steps for implementing multithreaded HTML to PDF conversion:

  1. Create a New Project: Start a new Console application in .NET Core to facilitate the HTML-to-PDF conversion process.
    Console.png
  2. Install Required Packages: Add the Syncfusion.HtmlToPdfConverter.Net.Windows NuGet package from Nuget.org to your project.
    HTMLtoPDFWindows.png
  3. Set Up Your Environment: In the Program.cs file, include these namespaces.
    C#
  using Syncfusion.HtmlConverter;
  using Syncfusion.Pdf;
  using Syncfusion.Drawing;
  using System.Threading.Tasks;
  1. Implement the Conversion Logic with Multithreading: Use the following code sample to perform parallel HTML-to-PDF conversions.

C#

class Program
{
   static void Main()
   {
       string text = System.IO.File.ReadAllText(Path.GetFullPath("page1.html"));
       text += System.IO.File.ReadAllText(Path.GetFullPath("page2.html"));
       IEnumerable<int> works = Enumerable.Range(0, 100);
       Parallel.ForEach(works, index => ConvertHTMLPDF(text));
       Console.WriteLine("PDF Conversion completed.");
   }
   static byte[] ConvertHTMLPDF(string html)
   {
       BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings()
       {
           PdfPageSize = PdfPageSize.A4,
           ViewPortSize = new Syncfusion.Drawing.Size(800, 1200),

       };
       //Initialize HTML to PDF converter.
       HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter()
       {
           ConverterSettings = blinkConverterSettings
       };

       using (var document = htmlConverter.Convert(html, string.Empty))
       {
           using (var stream = new MemoryStream())
           {
               document.Save(stream);
               string outputName  = Guid.NewGuid().ToString();
               System.IO.File.WriteAllBytes("Output " + outputName + ".pdf", stream.ToArray());
               return stream.ToArray();
           }
       }
   }
}

A complete working sample can be downloaded from HTML-to-PDF-Multithreading.

Conclusion
I hope you enjoyed learning on how to convert HTML to PDF with multithreading using C# in .NET Core.

You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied