unbook

unbook is a command-line program for converting a DRM-free .epub, .mobi, .azw, .azw3, .lit, or .chm ebook to a self-contained HTML file. FDP is not supported. In the HTML output, all images are included inline as data: URIs, inspired by SingleFile. unbook adds some CSS to render things nicely on both large screens and mobile. You can open unbook's output HTML in any browser (JavaScript is not required); you'll experience normal scrolling behavior and you can use whatever browser extensions and bookmarklets you like.

Use cases

Sample output

unbook extracted HTMLZ EPUB Alice's Adventures in Wonderland
unbook extracted HTMLZ EPUB The Iliad of Homer

Note 1: unbook --inside-bgcolor white was used to match Project Gutenberg's in-book white background.

Note 2: the unbook output defaults to using your browser's sans-serif and monospace fonts, which you can change in your browser settings (in desktop browsers, anyway).

On Windows, you may want to set sans-serif to Segoe UI. Chrome: chrome://settings/fonts; Firefox: Settings → General → Fonts → Advanced…

Install

If any of this does not work, please file an issue.

Linux:

  1. Install a recent version of Calibre using your package manager.
  2. Install a stable Rust compiler using rustup.
  3. cargo install --locked --git https://github.com/ludios/unbook

macOS:

  1. Install Calibre using Homebrew, which will conveniently put ebook-convert into your PATH and also handle updates:
    1. Run the Homebrew install step on https://brew.sh/
    2. brew install calibre

    If you don't want to use Homebrew, you can... add this to your PATH:

    /Applications/calibre.app/Contents/ebook-viewer.app/Contents/MacOS

    or use:

    unbook --ebook-convert /Applications/calibre.app/Contents/ebook-viewer.app/Contents/MacOS/ebook-convert

  2. Install a stable Rust compiler using rustup.
  3. cargo install --locked --git https://github.com/ludios/unbook

Windows, binary:

  1. Install Calibre from this page, or with Chocolatey:
    choco install -y calibre

  2. Grab the latest build from builds/windows/ and ignore all browser and SmartScreen warnings. Sorry, we don't code-sign. Extract the ZIP and copy unbook.exe to somewhere in your %Path%.

Windows, from source:

  1. Install Calibre from this page, or with Chocolatey:
    choco install -y calibre

  2. Install a stable Rust compiler using rustup.
  3. cargo install --locked --git https://github.com/ludios/unbook

Usage

Write .html file to the same directory:

unbook PATH_TO_EBOOK

Write HTML to some path:

unbook PATH_TO_EBOOK -o out.html

Replace output HTML if it already exists:

unbook -f PATH_TO_EBOOK

Replace typefaces even when there is more than one distinct font stack:

unbook --replace-serif-and-sans-serif always PATH_TO_EBOOK

Don't set any background colors:

unbook --outside-bgcolor unset --inside-bgcolor unset PATH_TO_EBOOK

Use a wider body width:

unbook --max-width 6in PATH_TO_EBOOK

Show all options:

unbook --help
click to see --help
    Convert an ebook to a self-contained HTML file

    Usage: unbook [OPTIONS] <EBOOK_PATH>
    
    Arguments:
      <EBOOK_PATH>
              The path to an .{epub,mobi,azw,azw3,lit,chm} file, or other format that Calibre can reasonably convert to HTMLZ. See https://manual.calibre-ebook.com/faq.html for a list of formats it supports, not all of which will convert nicely to HTMLZ
    
    Options:
      -o, --output-path <OUTPUT_PATH>
              The path for the output .html file. If not specified, it is saved in the directory of the input file, with ".html" appended to the existing extension
    
      -e, --remove-ebook-ext
              Remove the ebook extension before appending ".html".
    
              This is not the default because it makes it harder to find the original ebook file when viewing the .html, and because you may have e.g. both .mobi and .epub with the same name in a directory.
    
      -f, --force
              Replace the output .html file if it already exists
    
          --base-font-size <BASE_FONT_SIZE>
              The base font-size (with a CSS unit) to use for the book text
    
              [default: 15px]
    
          --base-font-family <BASE_FONT_FAMILY>
              The base font-family to use for the book text
    
              [default: sans-serif]
    
          --monospace-font-family <MONOSPACE_FONT_FAMILY>
              The monospace font-family to use
    
              [default: monospace]
    
          --replace-serif-and-sans-serif <REPLACE_SERIF_AND_SANS_SERIF>
              Font stack replacement mode for serif + sans-serif font stacks, treated as one set. The default mode "if-one" replaces fonts when there is just one distinct font stack
    
              [default: if-one]
              [possible values: never, if-one, always]
    
          --replace-monospace <REPLACE_MONOSPACE>
              Font stack replacement mode for monospace font stacks. The default mode "if-one" replaces fonts when there is just one distinct font stack
    
              [default: if-one]
              [possible values: never, if-one, always]
    
          --min-font-size <MIN_FONT_SIZE>
              The minimum font-size (with a CSS unit) to use for the book text. This can be used to work around issues with bad 'em' sizing making fonts far too small
    
              [default: 13px]
    
          --max-width <MAX_WIDTH>
              The max-width (with a CSS unit) to use for the book text
    
              [default: 5in]
    
          --min-line-height <MIN_LINE_HEIGHT>
              The minimum line-height (with an optional CSS unit) to use for the book text
    
              [default: 1.53333333]
    
          --inside-margin-when-wide <INSIDE_MARGIN_WHEN_WIDE>
              The CSS size of the inside margin for book text, when the viewport is wide enough to show the outside margin
    
              [default: 32px]
    
          --inside-margin-when-narrow <INSIDE_MARGIN_WHEN_NARROW>
              The CSS size of the inside margin for book text, when the viewport is not wide enough to show the outside margin
    
              [default: 16px]
    
          --outside-bgcolor <OUTSIDE_BGCOLOR>
              Background color (any CSS color) to use on the outside margin of the book, i.e. the <html> where there is no text. Use "unset" for no color
    
              [default: #888]
    
          --inside-bgcolor <INSIDE_BGCOLOR>
              Background color (any CSS color) to use for the text of the book, i.e. the <body>. Use "unset" for no color
    
              [default: #e9e9e9]
    
          --append-head <APPEND_HEAD>
              Additional HTML to append to <head> in the output HTML
    
              [default: ]
    
          --ebook-convert <EBOOK_CONVERT>
              Path to the Calibre "ebook-convert" executable to use
    
              [default: ebook-convert]
    
          --keep-temporary-htmlz
              Keep the temporary HTMLZ for debugging purposes
    
          --text-fragments-polyfill <TEXT_FRAGMENTS_POLYFILL>
              Which type of Text Fragments polyfill to add (if any) for the benefit of Firefox and Safari < 16.1 users
    
              [default: inline]
              [possible values: none, inline, unpkg]
    
      -h, --help
              Print help information (use `-h` for a summary)
    
      -V, --version
              Print version information
    

Convert many files and overwrite existing conversions:

for i in *.epub; do echo "$i"; unbook -f "$i"; done

Things unbook does to Calibre's HTMLZ output

Limitations

Alternatives which don't quite solve the same problem