A library to generate EPUB from HTML
npm install epub-gen-memoryGenerate EPUB books from HTML with a simple API in Node.js or the browser.
See JSZip Support table for which engines are supported. In addition, browsers need to support fetch as well as DOMParser and XMLSerializer to replace node-fetch and htmlparser2 (plus ecosystem).
------
This EPUB library will generate the needed files, as well as download all referenced images. Note that all fonts and all images respectively are downloaded in parallel in batches, so please keep that in mind when adjusting fetchTimeout.
Note that in the browser, only images on servers with CORS enabled can be downloaded.
On the server (with Node.js), image paths can also start with file://, in which case they are read directly from disc. The part after file:// must be a valid path for your system, as read by fs.readFile.
Install the lib and add it as a dependency (recommended), run in your project directory:
``shell`
npm install epub-gen-memory --save
Then put this in your code:
`js
import epub from 'epub-gen-memory';
epub(options).then(
content => console.log("Ebook Generated Successfully!"),
err => console.error("Failed to generate Ebook because of ", err)
);
`
See JSZip documentation on how to get the zip to the user. For a nodejs example please see the tests.
In environments where SharedArrayBuffer is not available, you might want to instead import from epub-gen-memory/sabstub, which includes a non-functional stub:
`js`
import epub from 'epub-gen-memory/sabstub';
The package also includes a browserifyd bundle (UMD) as epub-gen-memory/bundle. It is possible to use the bundle if you want to build for the browser. The bundle is also available from a CDN: UNPKG (latest, latest 1.x). The bundle also includes the proper return type for the browser (Promise instead of Promise).
`js`
import epub from 'epub-gen-memory/bundle';
Note: This library was written in TypeScript and thus uses ESM exports, but it was compiled to CommonJS, so you can also use the following:
`js`
const epub = require('epub-gen-memory').default;
`ts`
import epub, { EPub, optionsDefaults, chapterDefaults } from 'epub-gen-memory';
import type { Options, Content, Chapter, Font } from 'epub-gen-memory';
- optionsOrTitle: Options | string if string, then equivalent to { title: (see below)content
- : Chapter[] (see below)version
- varargs: : 3 | 2, verbose: boolean (in any order or not at all)Promise
- Returns: Promise
- Browser Returns:
- contructor(options: Options, content: Chapter[])render(): Promise
- genEpub(): Promise
- (Browser Promise)generateAsync(options: JSZipGeneratorOptions): Promise<...>
- see JSZip.generateAsync
protected:
- generateTemplateFiles(): PromisedownloadAllFonts(): Promise
- downloadAllImages(): Promise
- makeCover(): Promise
-
- version: number (default 3) Epub versionOptions
- Returns: defaults
- index: number of the chapterChapter
- Returns: defaults
- title: string author
Title of the book
- : string | string[] (optional, default ['anonymous']) "Alice"
Name of the author for the book, e.g. or ["Alice", "Bob"]publisher
- : string (optional, default anonymous) description
Publisher name
- : string (optional) cover
Book description
- : string | File (optional) "http://abc.com/book-cover.jpg"
Book cover image URL, e.g. or File object containing image bytestocTitle
- : string (optional, default Table of Contents) tocInTOC
Title of the Table of Contents
- : boolean (optional, default true) numberChaptersInTOC
Whether to show the TOC entry in the reader's Table of Contents; only for EPUB2
- : boolean (optional, default true) prependChapterTitles
Automatically number entries in TOC
- : boolean (optional, default true) date
Automatically put the title of the chapter before the content
- : string (optional, default today) lang
Publication date
- : string (optional, default en) css
Language code of the book
- : string (optional) "body{background: #000}"
CSS string, replaces our default styles, e.g: fonts
- : Font[] (optional) version
Array of fonts to include, see below
- : number (optional, default 3) 3
Version of the generated EPUB, for the latest version (http://idpf.org/epub/30) or 2 for the previous version (http://idpf.org/epub/201)fetchTimeout
- : number (optional, default 20000) AbortController
Timeout time for requests, in milliseconds; Browsers need to support and signals for this to workretryTimes
- : number (optional, default 3) batchSize
How many times to retry fetching resources
- : number (optional, default 100) ignoreFailedDownloads
The size of the batches to use when downloading files
- : boolean (optional, default false) verbose
Instead of throwing, emit a warning and write an empty file if a font or image fails to download
- : boolean | ((type, ...args) => void) (optional, default false) 'log'
Whether to log progress messages; If a function is provided, the first argument will either be or 'warn'
Within each chapter object:
- title: string (optional, default Chapter [number]) author
Chapter title
- : string | string[] (optional) content
Chapter author, generates info below chapter title
- : string excludeFromToc
HTML String of the chapter content, image sources are downloaded
- : boolean (optional, default false) beforeToc
Don't list chapter in Table of Contents
- : boolean (optional, default false) filename
List chapter before Table of Contents heading
- : string (optional) url
Custom name for chapter file
- : string (optional)
External link below chapter title
Within each font object:
- filename: string url
Name under which the font should be downloaded, including file extension
- : string ttf
URL where to find font, for best compatibility use (TrueType) fonts
You can then use the fonts as such (assuming you have a font with filename Merriweather.ttf):
`css`
@font-face {
font-family: "Merriweather";
font-style: normal;
font-weight: normal;
src : url(./fonts/Merriweather.ttf);
}
_Only includes major changes_
- Fixed file names
Internal chapter files are no longer given invalid names if the chapter title includes non-alphanumeric characters
- Optionally disable input validation
In the browser, by default, validation is not bundled due to size. To force use/not use, see the files lib/util/predicates{-nop}.ts.File` for cover
- Support
Useful in the browser to allow user-uploaded files without temporary storage
_From Lewis Carroll "Alice's Adventures in Wonderland", based on text at https://www.cs.cmu.edu/~rgs/alice-table.html and images from http://www.alice-in-wonderland.net/resources/pictures/alices-adventures-in-wonderland._
Please see the tests for the code used. EPUBs are generated next to the built test files.
This library is based on the work of @cyrilis.