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
- Read entire books in your browser because you like it or because it provides functionality (e.g. bookmarklets, extensions) not available in e-readers or ebook software
- Skim or search many ebooks using your browser
- Share a book with someone who has a browser but no e-reader or ebook software
- Link someone to a passage in a book using your browser's "Copy link to highlight" feature
- Text-index books with software that supports HTML but not ebook formats
Sample output
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.
- Chrome:
chrome://settings/fonts
- Firefox: Settings → General → Fonts → Advanced…
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:
- Install a recent version of Calibre using your package manager.
- Install a stable Rust compiler using rustup.
cargo install --locked --git https://github.com/ludios/unbook
macOS:
- Install Calibre using Homebrew, which will conveniently put
ebook-convert
into yourPATH
and also handle updates:- Run the Homebrew install step on https://brew.sh/
brew install calibre
If you don't want to use Homebrew, you can...
add this to yourPATH
:/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
- Install a stable Rust compiler using rustup.
cargo install --locked --git https://github.com/ludios/unbook
Windows, binary:
-
Install Calibre from this page, or with Chocolatey:
choco install -y calibre
-
Modify
Path
in your user or system environment variables: add your Calibre install directory, e.g.C:\Program Files\Calibre2
-
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:
-
Install Calibre from this page, or with Chocolatey:
choco install -y calibre
-
Modify
Path
in your user or system environment variables: add your Calibre install directory, e.g.C:\Program Files\Calibre2
- Install a stable Rust compiler using rustup.
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
- Inline the stylesheet, inline all images using
data:
URIs - Attach the book cover to the top of the HTML
- Attach
metadata.opf
and the Calibre conversion log in a comment - Add
<!DOCTYPE html>
- Add
<meta name="viewport" content="width=device-width" />
for reading on mobile - Add
<meta name="referrer" content="no-referrer" />
- Add
<meta http-equiv="Content-Security-Policy" ...>
to block external scripts and resources from loading - Add Text Fragments Polyfill so that
#:~:text=
links work for Firefox and Safari < 16.1 users (caniuse) - Adjust some CSS
- Set a body
max-width
to avoid overly long lines (default:5in
) - Remove all
text-align: justify
('The Procrustean Bed') - Reduce the brightness of the page, and remove whole-book
background-color
clashing with unbook'sbackground-color
- Adjust the base font size (default:
15px
) - Set a base
font-family
(default:sans-serif
) - Set a minimum font size (default:
13px
) - Set a minimum line height (default:
1.53333333
) - Prevent
<sup>
and<sup>
equivalents from increasing line height - Remove small top and bottom margins from some paragraphs
- Set a body
- When a book uses just one distinct
font-family
within the generic font families of serif and sans-serif, replace thesefont-family
with the basefont-family
.
Limitations
unbook produces a long HTML file without a chapter browser or fancy reader features. It does not save your reading position, though your browser may succeed at this if you never click an #anchor. The output HTML does not provide text adjustment UI (instead, re-run unbook with the settings you like).
Some ebooks, mostly those with many large images, become larger than desired when converted to a single HTML file. They will still work, but they are suboptimal for loading over the network. These books may just be unsuitable for conversion using unbook.
-
unbook does not generate "dark mode" CSS because there is no way to generate an authoritative "dark" version of a book without manual review: consider photos and diagrams; some images need to be inverted while others do not. Some books have more complicated use of color in tables and SVG.
Please use Dark Reader if you need dark mode. On mobile, it is available on iOS for Safari, and on Android for Firefox and Kiwi Browser.
If Dark Reader is not working for
file://
URLs in desktop Chrome, you can enable "Allow access to file URLs" inchrome://extensions/?id=eimadpbcbfnmbkopoojfekhnkhdbieeh
Dark Reader generally does not invert images. If most of the images in a book should be inverted, use this 'Invert all images' bookmarklet.
Embedded fonts are currently lost due to a Calibre limitation.
There is no warranty that anything described here works correctly.
Alternatives which don't quite solve the same problem
- Convert to .epub if necessary and extract as a ZIP. You will get one XHTML file per chapter.
- The calibre Content server.