Timeouts and Cancelation

DynamicPDF HTML Converter for .NET provides several timeout settings and cancellation features that help control long-running or stalled HTML-to-PDF conversions. These options allow you to define how long the converter waits for pages to load, commands to execute, and the overall conversion process to complete. In addition to timeout handling, you can explicitly cancel a conversion using a Microsoft CancellationToken.

Timeouts

The Converter.ConversionOptions class has three static timeout properties that allow you to control how long HTML Converter waits during different stages of the conversion process. These timeout values are specified in milliseconds and are useful for preventing long-running or stalled conversions from hanging indefinitely.

You do not usually need to set the default timeout values because the built-in defaults are sufficient for most conversion scenarios.

Timeout Default Value (milliseconds)
DefaultPageLoadTimeout controls how long the converter waits for a page and its resources to load 6000
DefaultCommandTimeout controls the timeout for individual conversion commands 10000
DefaultConversionTimeout controls the maximum amount of time allowed for the entire conversion operation 120000

The ConversionOptions.DefaultPageLoadTimeout raises a System.AggregateException while the ConversionOptions.DefaultConversionTimeout raises a System.Exception.

The time required to convert an HTML document to PDF depends on the complexity of the HTML content and how quickly the Chromium-based rendering engine can process and render the page internally. Html Converter uses a locally installed Edge/Chrome browser for HTML to PDF conversions.

The following code snippet illustrates one way you might handle one of these timeouts occurring.

catch (Exception ex)
 {
     string errorText = ex.ToString();
    //ConversionOptions.DefaultPageLoadTimeout
    if (errorText.Contains("Navigation response not received", StringComparison.OrdinalIgnoreCase)) {
         //"The page load operation timed out."
     }
    //ConversionOptions.DefaultConversionTimeout
     else if (errorText.Contains("Conversion timeout", StringComparison.OrdinalIgnoreCase)) {
         //"The conversion operation timed out."
     }
    else {
        //"An unexpected error occurred during conversion."
    }
 }
Catch ex As Exception

    Dim errorText As String = ex.ToString()

    ' ConversionOptions.DefaultPageLoadTimeout
    If errorText.Contains(
        "Navigation response not received",
        StringComparison.OrdinalIgnoreCase) Then

        ' "The page load operation timed out."

    ' ConversionOptions.DefaultConversionTimeout
    ElseIf errorText.Contains(
        "Conversion timeout",
        StringComparison.OrdinalIgnoreCase) Then

        ' "The conversion operation timed out."

    Else

        ' "An unexpected error occurred during conversion."

    End If

End Try

Cancelation

You can also explicitly cancel processing using the ConversionOptions.CancelToken property, which is a Microsoft CancelationToken instance.

Note that DefaultPageLoadTimeout, DefaultCommandTimeout, and DefaultConversionTimeout are static properties that apply to all Converter calls. In contrast, CancelToken is set on a ConversionOptions instance and applies only to the specific conversion that uses those options.

The following example illustrates using a CancelToken to cancel processing.

public static async Task RunAsync()
{
    ConversionOptions conversionOptions = new ConversionOptions();
    CancellationTokenSource tokenSource = new CancellationTokenSource();
    conversionOptions.CancelToken = tokenSource.Token;
    Task cancelTask = Task.Run(() =>{
                Thread.Sleep(1000);
                tokenSource.Cancel();
            });
    try
    {
        byte[] pdf = await Converter.ConvertAsync(
            new Uri("https://www.dynamicpdf.com"),
            conversionOptions);
        Console.WriteLine("Conversion completed.");
    }
    catch
    {
        if (tokenSource.IsCancellationRequested)
            Console.WriteLine("Conversion canceled.");
        else throw;
    }
}
Public Shared Async Function RunAsync() As Task
	Dim conversionOptions As New ConversionOptions()
	Dim tokenSource As New CancellationTokenSource()
	conversionOptions.CancelToken = tokenSource.Token
	Dim cancelTask As Task = Task.Run(
			Sub()
            Thread.Sleep(1000)
            tokenSource.Cancel()
            End Sub)
	Try
		Dim pdf As Byte() = Await Converter.ConvertAsync(
			New Uri("https://www.dynamicpdf.com"),
			conversionOptions)
		Console.WriteLine("Conversion completed.")
	Catch
		If tokenSource.IsCancellationRequested Then
			Console.WriteLine("Conversion canceled.")
		Else
			Throw
        End If
    End Try
End Function

In this topic