Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

EPUB library for Javascript

branch: master
README.textile

JSEpub

Reads and handles the EPUB e-book format.

Download

http://github.com/downloads/augustl/js-epub/js-epub.min.js

A note on XMLHttpRequest

There have been numerous reports of problems with JSEpub when XMLHttpRequst/XHR/AJAX have been used to download EPUBs.

The problem is encoding related. The easiest way to get around it, given browsers are harsh environments, is to Base64 encode the epub on the server, and Base64 decode it again on the client.

// Server side, here with Ruby. The approach applies to any language.
def serve_epub
  @response.body << Base64.encode64(File.read("/path/to/my.epub"))
end

// Client side
jQuery.get("/epubs/dynamic/my.epub", function (data) {
    var decodedData = someBase64DecoderHere(data); // Find a base64 decoder on google or whatever. 
    new JSEpub(decodedData);
});

Usage

var epubFile = ...; // Use HTML5 files or download via XHR.
var epub = new JSEpub(epubFile);

var showFirstPage = function () {
    // The spine lists all pages in correct order
    var spine = epub.opf.spine[0];

    // The manifest contains the href of the file
    var href = epub.opf.manifest[spine]["href"];

    // All the files are stored in here, indexed by href
    var doc = epub.files[href];

    // The doc is a DOMparser created DOM. You may want to
    // work with a string version of the page contents.
    var html = new XMLSerializer().serializeToString(doc);

    // Use "html" to your hearts content!
}

// Each time something happens in the processing, the callbakc is called. The
// step is a number, representing the actual step that was performed. You
// can use this to render status messages to the user as you process the file.
// The processor will also insert a tiny delay between each operation, so that
// you don't get the browser slow script warnings.
epub.processInSteps(function (step, extras) {
    var msg;

    if (step === 1) {
        msg = "Unzipping";
    } else if (step === 2) {
        msg = "Uncompressing " + extras;
    } else if (step === 3) {
        msg = "Reading OPF";
    } else if (step === 4) {
        msg = "Post processing";
    } else if (step === 5) {
        msg = "Finishing";
        showFirstPage();
    }

    // Render the "msg" here.
});

The library is pretty low level, to give you all the flexibility you need when you handle your EPUBs.

Installing and using

JSEpub has the following dependencies:

  • JSUnzip. EPUB files are packaged as a Zip
    archive.
  • JSInflate. Used to decompress the
    compressed files in the Zip archive.

Include these scripts on your page before you include js-epub. They don’t require any configuration or initialization.

Something went wrong with that request. Please try again.