# epub2

> Parse ePub electronic book files with Node.JS

Latest version **3.0.2** (published 2023-09-20) · ISC license · 0 weekly downloads

## Install

```sh
npm install epub2
pnpm add epub2
yarn add epub2
bun add epub2
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.2 |
| Published | 2023-09-20 |
| First published | 2018-02-01 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 126.3 KB |
| Known vulnerabilities | 0 (+2 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 26 |
| Author | Andris Reinman |
| Maintainers | bluelovers |
| Keywords | archive, async, book, books, chapter, comic, epub, epub-maker, epub-maker2, extract, file, image, manifest, metadata, mobile, node-novel, novel, parse, promise, raw, volume, zip |

## Links

- npm: https://www.npmjs.com/package/epub2
- Repository: https://github.com/bluelovers/ws-epub
- Homepage: https://github.com/bluelovers/ws-epub/tree/master/packages/epub2#readme
- Issues: https://github.com/bluelovers/ws-epub/issues
- npm.io page: https://npm.io/package/epub2

## Dependencies (6)

- [tslib](https://npm.io/package/tslib.md) ^2.6.2
- [xml2js](https://npm.io/package/xml2js.md) ^0.6.2
- [adm-zip](https://npm.io/package/adm-zip.md) ^0.5.10
- [bluebird](https://npm.io/package/bluebird.md) ^3.7.2
- [crlf-normalize](https://npm.io/package/crlf-normalize.md) ^1.0.19
- [array-hyper-unique](https://npm.io/package/array-hyper-unique.md) ^2.1.4

## Alternatives

- [lodash.startswith](https://npm.io/package/lodash.startswith.md) — 769.7K weekly downloads
- [@tarojs/service](https://npm.io/package/@tarojs/service.md) — 33.9K weekly downloads
- [io.extendreality.tilia.indicators.spatialtargets.unity](https://npm.io/package/io.extendreality.tilia.indicators.spatialtargets.unity.md) — 131 weekly downloads
- [@rtarojs/taro](https://npm.io/package/@rtarojs/taro.md) — 90 weekly downloads
- [node-branch-io](https://npm.io/package/node-branch-io.md) — 50 weekly downloads

## Recent versions

- 3.0.2 (latest) — 2023-09-20
- 3.0.1 — 2022-11-03
- 2.0.9 — 2022-05-16
- 2.0.7 — 2021-07-11
- 2.0.6 — 2021-07-08
- 2.0.4 — 2021-02-12
- 2.0.3 — 2020-08-13
- 2.0.2 — 2020-06-20
- 2.0.1 — 2020-06-09
- 1.3.8 — 2019-07-19
- 1.3.7 — 2019-02-18
- 1.3.6 — 2019-02-10
- 1.3.5 — 2018-08-28
- 1.3.4 — 2018-03-18
- 1.3.3 — 2018-02-07
- … 11 more at https://npm.io/package/epub2/versions

## README

# epub2

**epub2** is a node.js module to parse EPUB electronic book files.

**NB!** Only ebooks in UTF-8 are currently supported!.

***this module fork from*** `epub`

## Installation

    npm install epub2 zipfile

Or, if you want a pure-JS version (useful if used in a Node-Webkit app for example):

    npm install epub2

## Usage

* [example](test/example/example.ts)
* [example Promise](test/example/example2.ts)

```ts
const EPub = require("epub2/node");
import * as EPub from 'epub2/node';

const EPub = require("epub2").EPub;
import EPub from 'epub2';
import { EPub } from 'epub2';
```

```ts
var epub = new EPub(epubfile, imagewebroot, chapterwebroot);
```

Where

  * **epubfile** is the file path to an EPUB file
  * **imagewebroot** is the prefix for image URL's. If it's */images/* then the actual URL (inside chapter HTML `<img>` blocks) is going to be */images/IMG_ID/IMG_FILENAME*, `IMG_ID` can be used to fetch the image form the ebook with `getImage`. Default: `/images/`
  * **chapterwebroot** is the prefix for chapter URL's. If it's */chapter/* then the actual URL (inside chapter HTML `<a>` links) is going to be */chapters/CHAPTER_ID/CHAPTER_FILENAME*, `CHAPTER_ID` can be used to fetch the image form the ebook with `getChapter`. Default: `/links/`

Before the contents of the ebook can be read, it must be opened (`EPub` is an `EventEmitter`).

### async

```ts
let epub = await EPub.createAsync(epubfile, imagewebroot, chapterwebroot);
```

```ts
EPub.createAsync(epubfile, imagewebroot, chapterwebroot)
	.then(function (epub)
	{
		...
	})
	.catch(function (err)
	{
		console.log("ERROR\n-----");
		throw err;
	})
;
```

### old way

```ts
    epub.on("end", function(){
    	// epub is now usable
    	console.log(epub.metadata.title);

    	epub.getChapter("chapter_id", function(err, text){});
    });
    epub.parse();
```

## metadata

Property of the *epub* object that holds several metadata fields about the book.

```ts
    epub = new EPub(...);
    ...
    epub.metadata;
```

```ts
    // raw metadata
    epub.metadata[EPub.SYMBOL_RAW_DATA];
```

Available fields:

  * **creator** Author of the book (if multiple authors, then the first on the list) (*Lewis Carroll*)
  * **creatorFileAs** Author name on file (*Carroll, Lewis*)
  * **title** Title of the book (*Alice's Adventures in Wonderland*)
  * **language** Language code (*en* or *en-us* etc.)
  * **subject** Topic of the book (*Fantasy*)
  * **date** creation of the file (*2006-08-12*)
  * **description**

## flow

*flow* is a property of the *epub* object and holds the actual list of chapters (TOC is just an indication and can link to a # url inside a chapter file)

```ts
    epub = new EPub(...);
    ...
    epub.flow.forEach(function(chapter){
    	console.log(chapter.id);
    });
```

Chapter `id` is needed to load the chapters `getChapter`

## toc
*toc* is a property of the *epub* object and indicates a list of titles/urls for the TOC. Actual chapter and it's ID needs to be detected with the `href` property


## getChapter(chapter_id, callback)

Load chapter text from the ebook.

```ts
    var epub = new EPub(...);
    ...
    epub.getChapter("chapter1", function(error, text){});
```

## getChapterRaw(chapter_id, callback)

Load raw chapter text from the ebook.

## getImage(image_id, callback)

Load image (as a Buffer value) from the ebook.

```ts
    var epub = new EPub(...);
    ...
    epub.getImage("image1", function(error, img, mimeType){});
```

## getFile(file_id, callback)

Load any file (as a Buffer value) from the ebook.

```ts
    var epub = new EPub(...);
    ...
    epub.getFile("css1", function(error, data, mimeType){});
```

## EPub.libPromise

change Promise class

```ts
EPub.libPromise = Promise;
```

---
_Source: https://npm.io/package/epub2 · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
