Asynchronous Conversion
Use the Converter.ConvertAsync
static method to convert HTML to PDF asynchronously. This method offers four overloaded options for converting a URL or HTML text to a PDF document.
Converting to a PDF File
When converting HTML to a PDF file, the ConvertAsync
method is overloaded to accept either a URI or an HTML string.
URI to PDF
Converting HTML to PDF using a URI requires passing a URI (URL or local file) and an output path for the converted PDF to the Converter.ConvertAsync method. It also accepts optional conversion options and returns if successful.
public static Task<bool> ConvertAsync(Uri uri, string outputPath, [ConversionOptions conversionOptions = null])
Public Shared Sub Convert(uri As System.Uri, outputPath As String, Optional conversionOptions As ConversionOptions = Nothing)
See HTML Converter Local Resources for an example converting a local HTML file using a URI.
HTML string to PDF
Converting an HTML string to an output file path asynchronously is also straightforward. The method takes an HTML string, the output path, and conversion options.
public static Task<bool> ConvertAsync(string inputHtml,
string outputPath, [Uri basePath = null],
[ConversionOptions conversionOptions = null])
Public Shared Function Convert(inputHtml As String,
Optional basePath As System.Uri = Nothing, Optional
conversionOptions As ConversionOptions = Nothing) As Byte()
This method also allows specifying a base path.
See HTML Converter Base Tags for an example using base paths.
Converting to a Byte Array
Converting to a byte array is accomplished using the Converter.ConvertAsync static method overloads that return a byte array rather than a file. Note that when converting using input HTML, the method also supports specifying a base path.
public static Task<Byte[]> ConvertAsync(string inputHtml,
[Uri basePath = null], [ConversionOptions
conversionOptions = null])
Public Shared Function Convert(uri As System.Uri, Optional
conversionOptions As ConversionOptions = Nothing)
As Byte()
Example
The following example illustrates processing HTML as a URI asynchronously and an HTML string asynchronously and returning the results of both as a PDF document. It also illustrates processing an HTML string asynchronously and returning the results as a byte array.
public static void RunFile()
{
Uri uri = new Uri(new Uri("file://"), Util.GetPath("./Resources/products.html"));
Task<bool> task = Converter.ConvertAsync(uri, outputPath);
Console.WriteLine("The result: " + task.Result);
}
public static void RunHtml()
{
string sampleHtml = "<html><body><p>This is a very simple HTML string including a Table below.</p>" +
"<h4>Two rows and three columns:</h4><table border=\"1\"><tr><td>100</td><td>200</td>" +
"<td>300</td></tr><tr><td>400</td><td>500</td><td>600</td></tr></table></body></html>";
Task<bool> task = Converter.ConvertAsync(sampleHtml, outputPath);
Console.WriteLine("The result: " + task.Result);
}
public static void RunHtmlReturnByte()
{
string sampleHtml = "<html><body><p>This is a very simple HTML string including a Table below.</p>" +
"<h4>Two rows and three columns:</h4><table border=\"1\"><tr><td>100</td><td>200</td>" +
"<td>300</td></tr><tr><td>400</td><td>500</td><td>600</td></tr></table></body></html>";
Task<Byte[]> task = Converter.ConvertAsync(sampleHtml);
File.WriteAllBytes(outputPath), task.Result);
}
Public Shared Sub RunFile()
Dim uri As New Uri(New Uri("file://"), Util.GetPath("./Resources/products.html"))
Dim task As Task(Of Boolean) = Converter.ConvertAsync(uri, outputPath)
End Sub
Public Shared Sub RunHtml()
' Create a simple HTML string
Dim sampleHtml As String = "<html><body><p>This is a very simple HTML string including a Table below.</p>" &
"<h4>Two rows and three columns:</h4><table border='1'><tr><td>100</td><td>200</td>" &
"<td>300</td></tr><tr><td>400</td><td>500</td><td>600</td></tr></table></body></html>"
' Asynchronously Convert HTML to PDF with the conversion options specified
Dim task As Task(Of Boolean) = Converter.ConvertAsync(sampleHtml, outputPath)
End Sub
Public Shared Sub RunHtmlReturnByte()
' Create a simple HTML string
Dim sampleHtml As String = "<html><body><p>This is a very simple HTML string including a Table below.</p>" &
"<h4>Two rows and three columns:</h4><table border='1'><tr><td>100</td><td>200</td>" &
"<td>300</td></tr><tr><td>400</td><td>500</td><td>600</td></tr></table></body></html>"
Dim task As Task(Of Byte()) = Converter.ConvertAsync(sampleHtml)
File.WriteAllBytes(outputPath), task.Result)
End Sub
Refer to the Converter.ConvertAsync API documentation for more information.