@waptik/epubgen --- a library to make EPUBs from HTML
This is a fork of https://github.com/cpiber/epub-gen-memory and an attempt to denoify
the package.
Generate 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
.
Usage
Install the lib and add it as a dependency (recommended), run in your project directory:
npm install @waptik/epubgen --save
Then put this in your code:
import epub from '@waptik/epubgen';
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.
Note: This library was written in TypeScript and thus uses ESM exports.
API
import epub, { EPub, optionsDefaults, chapterDefaults } from '@waptik/epubgen';
import type { Options, Content, Chapter, Font } from '@waptik/epubgen';
epub(optionsOrTitle, content, [version | verbose][])
-
optionsOrTitle
:Options | string
if string, then equivalent to{ title: <optionsOrTitle> }
(see below) -
content
:Chapter[]
(see below) - varargs:
version
:3 | 2
,verbose
:boolean
(in any order or not at all) - Returns:
Promise<Buffer>
-
Browser Returns:
Promise<Blob>
class EPub
contructor(options: Options, content: Chapter[])
render(): Promise<EPub>
-
genEpub(): Promise<Buffer>
(BrowserPromise<Blob>
) -
generateAsync(options: JSZipGeneratorOptions): Promise<...>
see JSZip.generateAsync
protected:
generateTemplateFiles(): Promise<void>
downloadAllFonts(): Promise<void>
downloadAllImages(): Promise<void>
makeCover(): Promise<void>
optionsDefaults([version])
-
version
:number
(default3
) Epub version - Returns:
Options
defaults
chapterDefaults(index)
-
index
:number
of the chapter - Returns:
Chapter
defaults
Options
-
title
:string
Title of the book -
author
:string | string[]
(optional, default['anonymous']
)
Name of the author for the book, e.g."Alice"
or["Alice", "Bob"]
-
publisher
:string
(optional, defaultanonymous
)
Publisher name -
description
:string
(optional)
Book description -
cover
:string
(optional)
Book cover image URL, e.g."http://abc.com/book-cover.jpg"
-
tocTitle
:string
(optional, defaultTable of Contents
)
Title of the Table of Contents -
tocInTOC
:boolean
(optional, defaulttrue
)
Whether to show the TOC entry in the reader's Table of Contents; only for EPUB2 -
numberChaptersInTOC
:boolean
(optional, defaulttrue
)
Automatically number entries in TOC -
prependChapterTitles
:boolean
(optional, defaulttrue
)
Automatically put the title of the chapter before the content -
date
:string
(optional, default today)
Publication date -
lang
:string
(optional, defaulten
)
Language code of the book -
css
:string
(optional)
CSS string, replaces our default styles, e.g:"body{background: #000}"
-
fonts
:Font[]
(optional)
Array of fonts to include, see below -
version
:number
(optional, default3
)
Version of the generated EPUB,3
for the latest version (http://idpf.org/epub/30) or2
for the previous version (http://idpf.org/epub/201) -
fetchTimeout
:number
(optional, default20000
)
Timeout time for requests, in milliseconds; Browsers need to supportAbortController
and signals for this to work -
retryTimes
:number
(optional, default3
)
How many times to retry fetching resources -
batchSize
:number
(optional, default100
)
The size of the batches to use when downloading files -
ignoreFailedDownloads
:boolean
(optional, defaultfalse
)
Instead of throwing, emit a warning and write an empty file if a font or image fails to download -
verbose
:boolean | ((type, ...args) => void)
(optional, defaultfalse
)
Whether to log progress messages; If a function is provided, the first argument will either be'log'
or'warn'
Chapters
Within each chapter object:
-
title
:string
(optional, defaultChapter [number]
)
Chapter title -
author
:string | string[]
(optional)
Chapter author, generates info below chapter title -
content
:string
HTML String of the chapter content, image sources are downloaded -
excludeFromToc
:boolean
(optional, defaultfalse
)
Don't list chapter in Table of Contents -
beforeToc
:boolean
(optional, defaultfalse
)
List chapter before Table of Contents heading -
filename
:string
(optional)
Custom name for chapter file -
url
:string
(optional)
External link below chapter title
Fonts
Within each font object:
-
filename
:string
Name under which the font should be downloaded, including file extension -
url
:string
URL where to find font, for best compatibility usettf
(TrueType) fonts
You can then use the fonts as such (assuming you have a font with filename Merriweather.ttf
):
@font-face {
font-family: "Merriweather";
font-style: normal;
font-weight: normal;
src : url(./fonts/Merriweather.ttf);
}
Demo Preview:
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.
Credits
This library is based on the work of @cyrilis.