How to Convert HTML to PDF with Multithreading Using C# in .NET Core?
Our Syncfusion® HTML-to-PDF converter is a .NET PDF library for converting webpages, SVG MHTML and HTML files to PDF using C#. It uses the popular rendering engine Blink (Google Chrome). It is reliable and accurate. The result preserves all graphics, images, text, fonts, and the layout of the original HTML document or webpage.
HTML-to-PDF conversion for multiple web pages can be executed in parallel using the multithreading concept. Syncfusion’s HTML-to-PDF converter supports multithreading with the Parallel.ForEach method allows efficient and concurrent processing of multiple HTML conversion tasks.
This method optimizes performance by executing multiple instances of the ConvertHTMLToPDF method in parallel. Maximizing CPU utilization significantly reduces conversion time. Each task independently processes a distinct HTML segment, generating separate PDF documents simultaneously. This approach is highly effective for handling large volumes of data efficiently.
Steps for Converting Multiple HTML Pages to PDF Using Multithreading:
- Create a new Console application project.
- Install the Syncfusion.HtmlToPdfConverter.Net.Windows NuGet package as a reference to your console application from Nuget.org.
- Include the following namespaces and code snippets in the Program.cs file.
C#
using Syncfusion.HtmlConverter;
using Syncfusion.Pdf;
using Syncfusion.Drawing;
using System.Threading.Tasks;
- Use the following code sample in Program.cs to perform HTML-to-PDF conversion with multiple HTML documents in parallel using multithreading.
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!