 |
HtmlToPdfConvertHtmlStringAsync(String, String, CancellationToken) Method |
Asynchronously renders the specified HTML string, resolving relative resources against the
given base 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.5
Syntaxpublic Task<PdfDocument> ConvertHtmlStringAsync(
string htmlString,
string baseUrl,
CancellationToken cancellationToken
)
Public Function ConvertHtmlStringAsync (
htmlString As String,
baseUrl As String,
cancellationToken As CancellationToken
) As Task(Of PdfDocument)
Parameters
- htmlString String
- The HTML content to convert. Cannot be null.
- baseUrl String
-
The base URL used to resolve relative paths (images, style sheets, scripts) referenced
by the HTML. May be null when the HTML has no relative resources. An http or https base
URL is also the page's own address and origin: the HTML is served as the response for
that URL, so web fonts, fetch requests, module scripts and cookies of the same site work
as they do for a page converted by URL. A base element inside the HTML still wins.
- 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 htmlString is null 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
ExampleHtmlToPdf converter = new HtmlToPdf();
string html = "<img src='logo.png' /><p>Report</p>";
PdfDocument doc = await converter.ConvertHtmlStringAsync(html,
"https://www.example.com/", cancellationToken);
doc.Save("report.pdf");
doc.Close();Dim converter As New HtmlToPdf()
Dim html As String =
Dim doc As PdfDocument = Await converter.ConvertHtmlStringAsync(html,
"https://www.example.com/", cancellationToken)
doc.Save("report.pdf")
doc.Close()
See Also