Skip to main content

Search OfficeIMO

Enter a topic, API type, or PowerShell command.

Documentation Search all OfficeIMO/

Convert EPUB to PDF, Markdown and Images

Edit on GitHub

Convert EPUB e-books to PDF or Markdown, render chapters to PNG or SVG images, and save chapter XHTML and text to files for your own converter.

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
EPUB to PDF is a reading-order text conversion through the Reader, not a page-by-page rendering of the book's CSS; use the image export when appearance matters. OfficeIMO.Epub support matrix, OfficeIMO.Epub.Image support matrix, OfficeIMO support matrix
Verified
Examples compiled against the source on

Pick the route by what the output is for. For a printable or searchable copy, convert the EPUB to PDF or Markdown through the OfficeIMO converter or the CLI; both read the chapters in spine order. For a visual preview, OfficeIMO.Epub.Image renders chapters with their stylesheets and images through the HTML renderer. For your own pipeline, load the chapter XHTML and text directly from EpubDocument.

Convert an EPUB to PDF

C#

using OfficeIMO;

OfficeConversionReport report = OfficeConverter.Convert("handbook.epub", "handbook.pdf",
    new OfficeConversionOptions { FileConflictPolicy = OfficePublicationPolicy.Replace });

foreach (OfficeDiagnostic diagnostic in report.Diagnostics) {
    Console.WriteLine($"{diagnostic.Severity} {diagnostic.Code}: {diagnostic.Message}");
}

PowerShell

Convert-OfficeDocument -Path .\handbook.epub -To Pdf -OutputPath .\handbook.pdf

CLI

officeimo convert handbook.epub handbook.pdf

Convert an EPUB to Markdown

The Reader writes the chapters as one Markdown document and can extract images into a folder.

PowerShell

Read-OfficeDocument -Path .\handbook.epub -As Markdown

CLI

officeimo convert handbook.epub handbook.md --assets handbook-images

Render chapters to images

Load the raw chapter HTML and resource bytes so the renderer can use the book's CSS and pictures. The default mode makes one continuous image per chapter; set Mode = HtmlRenderMode.Paged for page-sized images.

C#

using OfficeIMO;
using OfficeIMO.Drawing;
using OfficeIMO.Epub;
using OfficeIMO.Epub.Image;
using OfficeIMO.Html;

EpubDocument book = EpubDocument.Load("handbook.epub", new EpubLoadOptions {
    IncludeRawHtml = true,
    IncludeResourceData = true
});

var options = new EpubImageExportOptions {
    Format = OfficeImageExportFormat.Png,
    ViewportWidth = 800
};

OfficeConversionReport report = book.SaveAsImages("handbook-pages", options);
Console.WriteLine($"{report.ItemsWritten} images written");

Save chapter XHTML and text

IncludeRawHtml keeps each chapter's markup next to its extracted text, which is the starting point for a custom converter.

C#

using OfficeIMO.Epub;

EpubDocument book = EpubDocument.Load("handbook.epub", new EpubLoadOptions {
    IncludeRawHtml = true,
    MaxChapterBytes = 2L * 1024 * 1024
});

Directory.CreateDirectory("chapters");
foreach (EpubChapter chapter in book.Chapters) {
    File.WriteAllText(Path.Combine("chapters", $"chapter-{chapter.Order:000}.txt"), chapter.Text);
    if (chapter.Html != null) {
        File.WriteAllText(Path.Combine("chapters", $"chapter-{chapter.Order:000}.xhtml"), chapter.Html);
    }
}

Convert a CHM help file to EPUB

CLI

officeimo convert manual.chm manual.epub

Example source