Click or drag to resize
Pdf Library for .NET

HtmlToPdfConvertUrlAsync(String, CancellationToken) Method

Asynchronously renders the web page at the specified URL and returns the result as a PDF document, stopping when the specified token is cancelled.

Namespace: SelectPdf.Universal
Assembly: SelectPdf.HtmlToPdf.Universal (in SelectPdf.HtmlToPdf.Universal.dll) Version: 26.4
Syntax
public Task<PdfDocument> ConvertUrlAsync(
	string url,
	CancellationToken cancellationToken
)

Parameters

url  String
The absolute URL of the web page to convert. Cannot be null or empty.
cancellationToken  CancellationToken
A token that stops the conversion when it is cancelled.

Return Value

TaskPdfDocument
A task that completes with a PdfDocument containing the converted content. The caller is responsible for saving and closing the returned document.
Exceptions
ExceptionCondition
HtmlToPdfException The task faults with this exception when url is null or empty, or when the conversion fails.
InvalidOperationException The task faults with this exception when this converter is already running a conversion.
OperationCanceledException The task is cancelled when cancellationToken is cancelled before the conversion completes.
Remarks

The conversion produces the same document as ConvertUrl(String), without holding a thread while the page loads and renders; see ConvertUrlAsync(String).

Cancelling the token stops the conversion wherever it is: a conversion still waiting for its turn leaves the queue, and one that is rendering is stopped in the engine. The task then ends cancelled and no document is produced.

Example
using (var timeout = new CancellationTokenSource(TimeSpan.FromSeconds(30)))
{
    HtmlToPdf converter = new HtmlToPdf();
    PdfDocument doc =
        await converter.ConvertUrlAsync("https://selectpdf.com", timeout.Token);
    doc.Save("selectpdf.pdf");
    doc.Close();
}
See Also