HTML-to-PDF in C# .NET using Async & MultiThreading
Async and Threading is useful when genearting PDFs in C# and VB.Net in batches or for high performance.
Async
IronPDF fully supports .Net Framework 4, and as such working with async functions requires a few extra lines of code. Task.Run requires .Net Framework 4.5+, or for the Microsoft Byte-Code Library to be installed into a .Net 4 project via NuGet.
//using IronPdf;
private static async Task<IronPdf.PdfDocument> RenderPdfAsync(string Html, IronPdf.PdfPrintOptions PrintOptions = null)
{
return await Task.Run(() => RenderPdf(Html, PrintOptions));
}
private static IronPdf.PdfDocument RenderPdf(string Html, IronPdf.PdfPrintOptions PrintOptions = null)
{
var Renderer = new IronPdf.HtmlToPdf();
if (PrintOptions != null)
{
Renderer.PrintOptions = PrintOptions;
}
PdfDocument Pdf = Renderer.RenderHtmlAsPdf(Html);
return Pdf;
}
//using IronPdf;
private static async Task<IronPdf.PdfDocument> RenderPdfAsync(string Html, IronPdf.PdfPrintOptions PrintOptions = null)
{
return await Task.Run(() => RenderPdf(Html, PrintOptions));
}
private static IronPdf.PdfDocument RenderPdf(string Html, IronPdf.PdfPrintOptions PrintOptions = null)
{
var Renderer = new IronPdf.HtmlToPdf();
if (PrintOptions != null)
{
Renderer.PrintOptions = PrintOptions;
}
PdfDocument Pdf = Renderer.RenderHtmlAsPdf(Html);
return Pdf;
}
'using IronPdf;
Private Shared Async Function RenderPdfAsync(ByVal Html As String, Optional ByVal PrintOptions As IronPdf.PdfPrintOptions = Nothing) As Task(Of IronPdf.PdfDocument)
Return Await Task.Run(Function() RenderPdf(Html, PrintOptions))
End Function
Private Shared Function RenderPdf(ByVal Html As String, Optional ByVal PrintOptions As IronPdf.PdfPrintOptions = Nothing) As IronPdf.PdfDocument
Dim Renderer = New IronPdf.HtmlToPdf()
If PrintOptions IsNot Nothing Then
Renderer.PrintOptions = PrintOptions
End If
Dim Pdf As PdfDocument = Renderer.RenderHtmlAsPdf(Html)
Return Pdf
End Function
VB C#
Multi-Threading
IronPDF endevours to be thread safe. For multithreading support we reccomend using the Nuget Package IronPdf.Threading.