Convert Figma logo to code with AI

futurepress logoepub.js

Enhanced eBooks in the browser.

6,394
1,092
6,394
504

Top Related Projects

The conformance checker for EPUB publications

19,281

The official source code repository for the calibre ebook manager

Quick Overview

EPUB.js is a JavaScript library for rendering EPUB documents in web browsers. It provides a flexible and customizable solution for displaying e-books and other digital publications, supporting various EPUB features and offering a responsive reading experience across different devices and screen sizes.

Pros

  • Cross-platform compatibility, allowing EPUB rendering in modern web browsers
  • Extensive customization options for appearance and functionality
  • Support for EPUB3 features, including fixed-layout and media overlays
  • Active development and community support

Cons

  • Learning curve for developers new to EPUB format and rendering
  • Performance may vary depending on the complexity of the EPUB content
  • Limited support for older browser versions
  • Some advanced EPUB features may require additional plugins or extensions

Code Examples

  1. Basic EPUB rendering:
var book = ePub("path/to/book.epub");
var rendition = book.renderTo("viewer");
rendition.display();
  1. Customizing the reader appearance:
var book = ePub("path/to/book.epub");
var rendition = book.renderTo("viewer", {
  width: 800,
  height: 600,
  spread: "always"
});
rendition.themes.register("dark", {
  body: { color: "#ffffff", background: "#000000" }
});
rendition.themes.select("dark");
  1. Handling reader events:
rendition.on("relocated", function(location) {
  console.log("Current page:", location.start.cfi);
});

rendition.on("keyup", function(e) {
  if (e.key === "ArrowRight") {
    rendition.next();
  } else if (e.key === "ArrowLeft") {
    rendition.prev();
  }
});

Getting Started

To use EPUB.js in your project, follow these steps:

  1. Install EPUB.js via npm:

    npm install epubjs
    
  2. Import EPUB.js in your JavaScript file:

    import ePub from 'epubjs';
    
  3. Create a container element in your HTML:

    <div id="viewer"></div>
    
  4. Initialize and render the EPUB:

    var book = ePub("path/to/book.epub");
    var rendition = book.renderTo("viewer", { width: "100%", height: "100%" });
    rendition.display();
    

This basic setup will render the EPUB in the specified container element. You can further customize the reader's appearance and behavior using the available API methods and options.

Competitor Comparisons

The conformance checker for EPUB publications

Pros of epubcheck

  • Comprehensive validation tool for EPUB files
  • Widely recognized and used in the publishing industry
  • Supports multiple EPUB versions (2.0, 3.0, 3.1, 3.2)

Cons of epubcheck

  • Primarily focused on validation, not rendering or reading
  • Command-line interface may be less user-friendly for some users
  • Can be resource-intensive for large EPUB files

Code comparison

epubcheck (Java):

EpubCheck epubCheck = new EpubCheck(epubFile);
ValidationResult result = epubCheck.validate();
if (result.isValid()) {
    System.out.println("EPUB is valid");
}

epub.js (JavaScript):

var book = ePub("book.epub");
book.renderTo("area");
book.on("rendered", function(section) {
    console.log("Rendered", section.href);
});

Key differences

  • Purpose: epubcheck is for validation, while epub.js is for rendering and reading
  • Language: epubcheck is written in Java, epub.js in JavaScript
  • Usage: epubcheck is often used in publishing workflows, epub.js in web-based readers
  • Features: epubcheck provides detailed error reports, epub.js offers interactive reading experiences

Both projects serve different needs in the EPUB ecosystem, with epubcheck ensuring standards compliance and epub.js focusing on content presentation and interaction.

19,281

The official source code repository for the calibre ebook manager

Pros of Calibre

  • Comprehensive e-book management system with library organization, metadata editing, and format conversion
  • Supports a wide range of e-book formats beyond just EPUB
  • Includes a built-in e-book viewer and editor

Cons of Calibre

  • Larger, more complex application with a steeper learning curve
  • Primarily desktop-focused, less suitable for web-based implementations
  • Heavier resource usage compared to lightweight EPUB readers

Code Comparison

Calibre (Python):

from calibre.ebooks.epub import EpubReader

reader = EpubReader(epub_path)
opf = reader.opf
metadata = opf.metadata

epub.js (JavaScript):

import ePub from 'epubjs';

let book = ePub('book.epub');
book.loaded.metadata.then(function(metadata) {
  console.log(metadata);
});

Summary

Calibre is a full-featured e-book management suite, while epub.js focuses specifically on rendering EPUB files in web browsers. Calibre offers more comprehensive tools for e-book handling but is less suitable for web integration. epub.js provides a lightweight solution for web-based EPUB reading but lacks the extensive management features of Calibre. The choice between them depends on the specific requirements of the project, whether it's a web application or a desktop e-book management system.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Epub.js v0.3

FuturePress Views

Epub.js is a JavaScript library for rendering ePub documents in the browser, across many devices.

Epub.js provides an interface for common ebook functions (such as rendering, persistence and pagination) without the need to develop a dedicated application or plugin. Importantly, it has an incredibly permissive Free BSD license.

Try it while reading Moby Dick

Why EPUB

Why EPUB

The EPUB standard is a widely used and easily convertible format. Many books are currently in this format, and it is convertible to many other formats (such as PDF, Mobi and iBooks).

An unzipped EPUB3 is a collection of HTML5 files, CSS, images and other media – just like any other website. However, it enforces a schema of book components, which allows us to render a book and its parts based on a controlled vocabulary.

More specifically, the EPUB schema standardizes the table of contents, provides a manifest that enables the caching of the entire book, and separates the storage of the content from how it’s displayed.

Getting Started

If using archived .epub files include JSZip (this must precede inclusion of epub.js):

<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.min.js"></script>

Get the minified code from the build folder:

<script src="../dist/epub.min.js"></script>

Set up a element to render to:

<div id="area"></div>

Create the new ePub, and then render it to that element:

<script>
  var book = ePub("url/to/book/package.opf");
  var rendition = book.renderTo("area", {width: 600, height: 400});
  var displayed = rendition.display();
</script>

Render Methods

Default

book.renderTo("area", { method: "default", width: "100%", height: "100%" });

View example

The default manager only displays a single section at a time.

Continuous

book.renderTo("area", { method: "continuous", width: "100%", height: "100%" });

View example

The continuous manager will display as many sections as need to fill the screen, and preload the next section offscreen. This enables seamless swiping / scrolling between pages on mobile and desktop, but is less performant than the default method.

Flow Overrides

Auto (Default)

book.renderTo("area", { flow: "auto", width: "900", height: "600" });

Flow will be based on the settings in the OPF, defaults to paginated.

Paginated

book.renderTo("area", { flow: "paginated", width: "900", height: "600" });

View example

Scrolled: book.renderTo("area", { flow: "scrolled-doc" });

View example

Scripted Content

Scripted content, JavasScript the ePub HTML content, is disabled by default due to the potential for executing malicious content.

This is done by sandboxing the iframe the content is rendered into, though it is still recommended to sanitize the ePub content server-side as well.

If a trusted ePub contains interactivity, it can be enabled by passing allowScriptedContent: true to the Rendition settings.

<script>
  var rendition = book.renderTo("area", {
    width: 600,
    height: 400,
    allowScriptedContent: true
  });
</script>

This will allow the sandboxed content to run scripts, but currently makes the sandbox insecure.

Documentation

API documentation is available at epubjs.org/documentation/0.3/

A Markdown version is included in the repo at documentation/API.md

Running Locally

install node.js

Then install the project dependences with npm

npm install

You can run the reader locally with the command

npm start

Examples

View All Examples

Testing

Test can be run by Karma from NPM

npm test

Building for Distribution

Builds are concatenated and minified using webpack and babel

To generate a new build run

npm run prepare

or to continuously build run

npm run watch

Hooks

Similar to a plugins, Epub.js implements events that can be "hooked" into. Thus you can interact with and manipulate the contents of the book.

Examples of this functionality is loading videos from YouTube links before displaying a chapter's contents or implementing annotation.

Hooks require an event to register to and a can return a promise to block until they are finished.

Example hook:

rendition.hooks.content.register(function(contents, view) {

    var elements = contents.document.querySelectorAll('[video]');
    var items = Array.prototype.slice.call(elements);

    items.forEach(function(item){
      // do something with the video item
    });

})

The parts of the rendering process that can be hooked into are below.

book.spine.hooks.serialize // Section is being converted to text
book.spine.hooks.content // Section has been loaded and parsed
rendition.hooks.render // Section is rendered to the screen
rendition.hooks.content // Section contents have been loaded
rendition.hooks.unloaded // Section contents are being unloaded

Reader

The reader has moved to its own repo at: https://github.com/futurepress/epubjs-reader/

Additional Resources

Gitter Chat

Epub.js Developer Mailing List

IRC Server: freenode.net Channel: #epub.js

Follow us on twitter: @Epubjs

Other

EPUB is a registered trademark of the IDPF.

NPM DownloadsLast 30 Days