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
unbook extracted HTMLZ EPUB It’s Time to Use the Modern Digital Publishing Format for Your Organization’s Documents

Note: 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, consider installing Inter and setting sans-serif to 'Inter Variable Text' or 'Inter Display', which has narrower letter spacing.

Or see below for a usage example to use a different font in the output.

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. Modify Path in your user or system environment variables: add your Calibre install directory, e.g. C:\Program Files\Calibre2

  3. 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. Modify Path in your user or system environment variables: add your Calibre install directory, e.g. C:\Program Files\Calibre2

  3. Install a stable Rust compiler using rustup.
  4. 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

Use the InterDisplay web font:

unbook \
    --base-font-family 'InterDisplay, sans-serif' \
    --append-head '<link rel="stylesheet" href="https://rsms.me/inter/inter.css">' \
    --csp-style-src https://rsms.me/inter/ \
    --csp-font-src https://rsms.me/inter/ \
    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 behind the text of the book, i.e. the <body>. Use "unset" for no color

          [default: #e9e9e9]

      --inside-bgcolor-similarity-threshold <INSIDE_BGCOLOR_SIMILARITY_THRESHOLD>
          ebooks are sometimes wrapped in an element with a white or near-white background color that effectively overrides unbook's inside_bgcolor. This similarity threshold is used when considering whether to replace these background colors: if R, G, and B of inside_bgcolor are all within R, G, and B of inside_bgcolor_similarity_threshold, the unwanted background-color is removed. Set to 0 to never replace, or 1 to always replace

          [default: 0.2]

      --fgcolor <FGCOLOR>
          Foreground color (any CSS color) to use for the text of the book

          [default: #000]

      --link-color-normal <LINK_COLOR_NORMAL>
          Color (any CSS color) to use for normal (unvisited) links

          [default: #00e]

      --link-color-visited <LINK_COLOR_VISITED>
          Color (any CSS color) to use for visited links

          [default: #551a8b]

      --link-color-active <LINK_COLOR_ACTIVE>
          Color (any CSS color) to use for active (being clicked) links

          [default: #f00]

      --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]

      --csp-default-src <CSP_DEFAULT_SRC>
          Space-separated entries to add to Content-Security-Policy default-src

          [default: ]

      --csp-font-src <CSP_FONT_SRC>
          Space-separated entries to add to Content-Security-Policy font-src

          [default: ]

      --csp-img-src <CSP_IMG_SRC>
          Space-separated entries to add to Content-Security-Policy img-src

          [default: ]

      --csp-style-src <CSP_STYLE_SRC>
          Space-separated entries to add to Content-Security-Policy style-src

          [default: ]

      --csp-media-src <CSP_MEDIA_SRC>
          Space-separated entries to add to Content-Security-Policy media-src

          [default: ]

      --csp-script-src <CSP_SCRIPT_SRC>
          Space-separated entries to add to Content-Security-Policy script-src

          [default: ]

      --csp-object-src <CSP_OBJECT_SRC>
          Space-separated entries to add to Content-Security-Policy object-src

          [default: ]

  -h, --help
          Print help (see a summary with '-h')

  -V, --version
          Print version

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