Render and Convert HTML
Edit on GitHubParse HTML or MHTML, control resource access, render images, and convert web content into Office and PDF artifacts. Includes examples and API links.
OfficeIMO.Html owns HTML/MHTML parsing, archive resources, URL policy, bounded layout, and image rendering. Destination adapters reuse the same engine so Word, Markdown, RTF, Email, PDF, and EPUB workflows do not each invent another HTML parser.
Render HTML to an image
using OfficeIMO.Html;
HtmlConversionDocument source = HtmlConversionDocument.Parse(
"<h1>Status</h1><p>Generated by OfficeIMO.</p>");
byte[] png = source.ToPng(new HtmlRenderOptions {
ViewportWidth = 720,
Scale = 1.5,
MediaFeatures = new HtmlRenderMediaFeatures {
PreferredColorScheme = HtmlPreferredColorScheme.Light,
ReducedMotion = HtmlReducedMotionPreference.Reduce
}
});
File.WriteAllBytes("status.png", png);
The renderer supports continuous or paged layout and PNG, JPEG, TIFF, SVG, and WebP output. Apply output-pixel and source-resource limits before accepting arbitrary uploads.
Choose a destination adapter
| Destination | Package |
|---|---|
OfficeIMO.Html.Pdf | |
| Word | OfficeIMO.Word.Html |
| Markdown | OfficeIMO.Markdown.Html |
| RTF | OfficeIMO.Html with the focused RTF adapter |
| Reader results | OfficeIMO.Reader.Html |
The generated output gallery includes a runnable invoice workflow with a downloadable PDF and the PNG rendered from the same HTML document and options object.
Convert from the command line
dotnet tool install --global OfficeIMO.Tool
officeimo html convert report.html --output report.pdf
officeimo html convert archive.mhtml --output archive.pdf
officeimo html capabilities --format json
The tool accepts files or standard input, writes files atomically, and defaults to no local or remote resource reads. Embedded data and bounded MHTML resources remain available. Its capability output is generated from the same executable contract as the .NET API and checked-in support matrix.
Apply host styles and PDF settings
using OfficeIMO.Html.Pdf;
using OfficeIMO.Pdf;
var options = new HtmlPdfSaveOptions {
MaxPageCount = 200,
PdfOptions = new PdfOptions().EnableTaggedPdfCatalogMarkers()
};
options.AdditionalStylesheets.Add("@page { margin: 18mm } body { color: hsl(215 35% 20%) }");
byte[] pdf = HtmlConversionDocument.Parse(html).ToPdf(options);
PDF writer settings are snapshotted through PdfOptions; HTML layout and safety settings remain on the shared HtmlRenderOptions base type.
Control resources
Set a resource policy for network URLs, file URLs, data URLs, archive entries, image size, and total expansion. A service handling uploaded HTML should default to no external network or filesystem access and supply approved resources explicitly.
Use semantic conversion when editable headings, paragraphs, lists, links, and tables matter most. Use visual rendering when page appearance is the primary contract.