Giter Site home page Giter Site logo

js-epub's Introduction

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.

js-epub's People

Contributors

augustl avatar

Stargazers

 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.