# Read MHTML Web Archives and Site Bundles

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

**Packages**

- [OfficeIMO.Mhtml](https://www.nuget.org/packages/OfficeIMO.Mhtml) `dotnet add package OfficeIMO.Mhtml`
- [OfficeIMO.Html](https://www.nuget.org/packages/OfficeIMO.Html) `dotnet add package OfficeIMO.Html`
- [OfficeIMO.Html.Pdf](https://www.nuget.org/packages/OfficeIMO.Html.Pdf) `dotnet add package OfficeIMO.Html.Pdf`

**Works with**

- .NET
- [PowerShell](/docs/pswriteoffice/install/) `Install-Module PSWriteOffice`
- [CLI](/docs/cli/install/) `dotnet tool install --global OfficeIMO.Tool`

**Target frameworks**

netstandard2.0, net8.0, net10.0, net472

**APIs**

- [`MhtmlDocument`](/api/mhtml/officeimo-mhtml-mhtmldocument/)
- [`HtmlSiteBundle`](/api/html/officeimo-html-htmlsitebundle/)
- [`HtmlConversionDocument`](/api/html/officeimo-html-htmlconversiondocument/)
- [`HtmlPdfExportOptions`](/api/html-pdf/officeimo-html-pdf-htmlpdfexportoptions/)
- [`MhtmlResource`](/api/mhtml/officeimo-mhtml-mhtmlresource/)
- [`HtmlPdfRenderRequestResult`](/api/html-pdf/officeimo-html-pdf-htmlpdfrenderrequestresult/)
- [`HtmlRenderEncoder`](/api/html/officeimo-html-htmlrenderencoder/)
- [`HtmlRenderIntentProfile`](/api/html/officeimo-html-htmlrenderintentprofile/)
- [`HtmlRenderMode`](/api/html/officeimo-html-htmlrendermode/)
- [`HtmlRenderOptions`](/api/html/officeimo-html-htmlrenderoptions/)
- [`HtmlRenderRequest`](/api/html/officeimo-html-htmlrenderrequest/)
- [`HtmlSiteBundleOptions`](/api/html/officeimo-html-htmlsitebundleoptions/)

**Cmdlets**

- [`Read-OfficeDocument`](/api/powershell/read-officedocument/)

**Commands**

- [`officeimo reader read`](/docs/cli/extract-and-inspect/)
- [`officeimo html convert`](/docs/cli/html/)
- [`officeimo html render`](/docs/cli/html/)

**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](/compatibility/#package-mhtml), [OfficeIMO.Html support matrix](/compatibility/#package-html), [OfficeIMO.Html.Pdf support matrix](/compatibility/#package-html-pdf)

**Verified**

Examples compiled against the source on 2026-11-14

**Data**

- [OfficeIMO.Mhtml API index](/api/mhtml/index.json)
- [OfficeIMO.Html API index](/api/html/index.json)
- [OfficeIMO.Html.Pdf API index](/api/html-pdf/index.json)
- [PSWriteOffice command catalog](/data/powershell/commands.json)
- [CLI command catalog](/data/cli/commands.json)
- [Capability catalog (JSON)](/data/capabilities.json)

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.

```sh
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#

```csharp
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#

```csharp
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

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

### CLI

```sh
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#

```csharp
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

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

## Render an MHTML file to images

### C#

```csharp
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

```sh
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#

```csharp
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

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

<!-- PowerForge markdown source sha256:EF0D0226B5DA3C67DC535DBF3280463F7A7256C306391652E3062F0F542D0261; title=True -->

<!-- PowerForge markdown artifact sha256:82A61E157DD7F16B37BC181D4BCF7DE2D3848D76B475AEE9601114268F25F3AB -->
