 |
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.UniversalAssembly: SelectPdf.Universal (in SelectPdf.Universal.dll) Version: 26.4
Syntaxpublic Task<PdfDocument> ConvertUrlAsync(
string url,
CancellationToken cancellationToken
)
Public Function ConvertUrlAsync (
url As String,
cancellationToken As CancellationToken
) As Task(Of PdfDocument)
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| Exception | Condition |
|---|
| 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.
Exampleusing (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();
}Using timeout As New CancellationTokenSource(TimeSpan.FromSeconds(30))
Dim converter As New HtmlToPdf()
Dim doc As PdfDocument =
Await converter.ConvertUrlAsync("https://selectpdf.com", timeout.Token)
doc.Save("selectpdf.pdf")
doc.Close()
End Using
See Also