Async PDF Generation
IronPDF supports asynchronous PDF generation, allowing for optimized performance and efficiency in PDF generation tasks. By using the await
keyword along with IronPDF's RenderHtmlAsPdfAsync
method, you can efficiently create PDF documents without hurting your system's performance.
Steps to Asynchronous PDF Generation
using IronPdf;
public async Task GeneratePdfAsync()
{
// Create a new ChromePdfRenderer instance
var renderer = new ChromePdfRenderer();
// Use the RenderHtmlAsPdfAsync method to render HTML to PDF asynchronously
var pdf = await renderer.RenderHtmlAsPdfAsync("<h1>Html with CSS and Images</h1>");
// Save the generated PDF to a specified file
pdf.SaveAs("async_example.pdf");
}
using IronPdf;
public async Task GeneratePdfAsync()
{
// Create a new ChromePdfRenderer instance
var renderer = new ChromePdfRenderer();
// Use the RenderHtmlAsPdfAsync method to render HTML to PDF asynchronously
var pdf = await renderer.RenderHtmlAsPdfAsync("<h1>Html with CSS and Images</h1>");
// Save the generated PDF to a specified file
pdf.SaveAs("async_example.pdf");
}
Imports IronPdf
Public Async Function GeneratePdfAsync() As Task
' Create a new ChromePdfRenderer instance
Dim renderer = New ChromePdfRenderer()
' Use the RenderHtmlAsPdfAsync method to render HTML to PDF asynchronously
Dim pdf = Await renderer.RenderHtmlAsPdfAsync("<h1>Html with CSS and Images</h1>")
' Save the generated PDF to a specified file
pdf.SaveAs("async_example.pdf")
End Function
To begin, we create a new ChromePdfRenderer
instance, utilizing IronPDF's powerful rendering engine to generate pixel-perfect PDF documents. Once this is done, we have access to the method we need to generate a PDF document from the given HTML string. At this stage, we use the await
keyword to pause the execution of our async method until the PDF generation is complete. The RenderHtmlAsPdfAsync
method takes the HTML input and renders it into a PDF asynchronously, improving performance by avoiding blocking operations.
The final step is to save the generated PDF document to a specified file name and location using the SaveAs
method. Beyond this basic example, IronPDF's asynchronous PDF generation methods can be used in situations such as generating large batches of PDF documents in a more performance-friendly manner.
Click here to view the How-to Guide, including examples, sample code, and files