Skip to main content

Search OfficeIMO

Enter a topic, API type, or PowerShell command.

Documentation Search all OfficeIMO/

Read MHTML Web Archives and Site Bundles

Edit on GitHub

Open MHTML web archives or zipped websites, read their HTML and resources, create new archives, and convert them to PDF or images offline.

Packages
Works with
  • .NET
  • PowerShell Install-Module PSWriteOffice
  • CLI dotnet tool install --global OfficeIMO.Tool
Target frameworks
netstandard2.0, net8.0, net10.0, net472
Limits
Archives are rendered offline from their saved state; scripts do not run, and missing resources are fetched only when you supply a fetcher through MhtmlRemoteResourcePolicy. OfficeIMO.Mhtml support matrix, OfficeIMO.Html support matrix, OfficeIMO.Html.Pdf support matrix
Verified
Examples compiled against the source on

A saved web page usually arrives in one of two containers. An MHT/MHTML file is a MIME message that holds the page and its images and stylesheets; OfficeIMO.Mhtml reads and writes it as an MhtmlDocument. A ZIP of a website holds the same files as ordinary entries; HtmlSiteBundle in OfficeIMO.Html loads it without extracting anything to disk. Both expose the page as an HtmlConversionDocument, so every HTML conversion works on them.

dotnet add package OfficeIMO.Mhtml
dotnet add package OfficeIMO.Html.Pdf

Create and save an MHTML archive

Build an archive from HTML plus the resources it references by cid: or Content-Location.

C#

using System.Text;
using OfficeIMO.Mhtml;

var stylesheet = new MhtmlResource(
    Encoding.UTF8.GetBytes("h1 { color: navy }"),
    contentType: "text/css",
    contentLocation: "https://example.com/site.css");

var archive = new MhtmlDocument(
    "<html><head><link rel=\"stylesheet\" href=\"site.css\"></head><body><h1>Saved page</h1></body></html>",
    new[] { stylesheet },
    contentLocation: "https://example.com/index.html",
    subject: "Saved page");

archive.Save("saved-page.mhtml");

Read an MHTML file

Load parses the MIME structure, picks the root HTML part and resolves its base URI. MimeDiagnostics lists recovered or ambiguous parts, such as duplicate Content-ID values.

C#

using OfficeIMO.Mhtml;

MhtmlDocument archive = MhtmlDocument.Load("saved-page.mhtml");

Console.WriteLine($"Subject: {archive.Subject}");
Console.WriteLine($"Base URI: {archive.BaseUri}");
Console.WriteLine(archive.HtmlDocument.NormalizedHtml);

foreach (MhtmlResource resource in archive.Resources) {
    Console.WriteLine($"{resource.ContentType} {resource.ContentLocation ?? resource.ContentId}");
}

foreach (var diagnostic in archive.MimeDiagnostics) {
    Console.WriteLine(diagnostic);
}

PowerShell

Read-OfficeDocument -Path .\saved-page.mhtml -As Text

CLI

officeimo reader read saved-page.mhtml --format markdown --output saved-page.md

Convert an MHTML file to PDF

ConfigureRenderOptions points the renderer at the archive's embedded resources. The same options object is then passed to the PDF conversion.

C#

using OfficeIMO.Html.Pdf;
using OfficeIMO.Mhtml;

MhtmlDocument archive = MhtmlDocument.Load("saved-page.mhtml");

var options = new HtmlPdfExportOptions();
archive.ConfigureRenderOptions(options);

archive.HtmlDocument.SaveAsPdf("saved-page.pdf", options);

CLI

officeimo html convert saved-page.mhtml --output saved-page.pdf

Render an MHTML file to images

C#

using OfficeIMO.Drawing;
using OfficeIMO.Html;
using OfficeIMO.Html.Pdf;
using OfficeIMO.Mhtml;

MhtmlDocument archive = MhtmlDocument.Load("saved-page.mhtml");

var options = new HtmlRenderOptions {
    ViewportWidth = 1280,
    Mode = HtmlRenderMode.Continuous,
    Format = OfficeImageExportFormat.Png
};
archive.ConfigureRenderOptions(options);

archive.HtmlDocument.SaveAsImage("saved-page.png", options);

CLI

officeimo html render saved-page.mhtml --profile screen-full-page --encoder png --output saved-page-images.zip

Convert a zipped website to PDF

HtmlSiteBundle selects index.html (or the single HTML entry) unless you name one in EntryPath. It checks entry counts, decoded sizes, compression ratios and checksums, and rejects path traversal. Archive images, CSS and fonts are used only when ResourcePolicy.AllowEmbeddedPackageResources is on.

C#

using OfficeIMO.Html;
using OfficeIMO.Html.Pdf;

HtmlSiteBundle bundle = await HtmlSiteBundle.LoadAsync("saved-site.zip",
    new HtmlSiteBundleOptions { EntryPath = "articles/report.html" });

Console.WriteLine($"{bundle.EntryPath}: {bundle.EntryNames.Count} entries");

var options = new HtmlPdfExportOptions();
options.ResourcePolicy.AllowEmbeddedPackageResources = true;

HtmlPdfRenderRequestResult result = await bundle.RenderToPdfResultAsync(
    HtmlRenderRequest.Create(HtmlRenderIntentProfile.PrintPaged, HtmlRenderEncoder.Pdf, options));
File.WriteAllBytes("saved-site.pdf", result.ToBytes());

CLI

officeimo html convert saved-site.zip --input-format site-bundle --entry-path articles/report.html --output saved-site.pdf

Example source