Converter Class

Converter class

One-shot HTML to PDF or PNG conversion. A lone string input is a file path; markup goes through the overloads that also take a base URI. Caller streams are read from their current position and never closed. A file output is written beside the target and moved over it when complete.

public static class Converter

Methods

NameDescription
static ConvertHTML(HtmlSource, SaveOptions, ICreateStreamProvider)Converts source to the streams output creates, one per part.
static ConvertHTML(HtmlSource, SaveOptions, Stream)Converts source to output, left open.
static ConvertHTML(HtmlSource, SaveOptions, string)Converts source to the file outputPath.
static ConvertHTML(string, SaveOptions, string)Converts the HTML file inputPath to the file outputPath.
static ConvertHTML(Stream, string, SaveOptions, Stream)Converts the HTML bytes of input to output; both stay open.
static ConvertHTML(Stream, string, SaveOptions, string)Converts the HTML bytes of input to the file outputPath.
static ConvertHTML(string, string, SaveOptions, Stream)Converts the markup html to output, left open.
static ConvertHTML(string, string, SaveOptions, string)Converts the markup html to the file outputPath.
static IsSupported(PdfConformance)True when this version writes conformance; only Standard in this version.

Examples

Convert a local invoice with the familiar Aspose call.

var result = Converter.ConvertHTML("invoice.html", new PdfSaveOptions(), "invoice.pdf");
Console.WriteLine(result.PageCount);

Convert stream-to-stream without accidental ownership transfer.

using var input = File.OpenRead("report.html");
using var output = File.Create("report.pdf");
Converter.ConvertHTML(input, "https://assets.example/", new PdfSaveOptions(), output);

Produce one print-resolution PNG per page with explicit stream completion.

var images = new ImageSaveOptions {
    Mode = ImageMode.PrintPages, HorizontalResolution = 300, VerticalResolution = 300,
};
images.Css.MediaType = MediaType.Print;
Converter.ConvertHTML(HtmlSource.FromFile("report.html"), images, new PageFiles());

private sealed class PageFiles : ICreateStreamProvider
{
    public Stream GetStream(OutputPart part) => File.Create($"page-{part.Index + 1}.png");

    public void Complete(OutputPart part, Stream stream, OutputOutcome outcome)
    {
        stream.Dispose();
        if (outcome != OutputOutcome.Completed) File.Delete($"page-{part.Index + 1}.png");
    }
}

See Also