Convert Figma logo to code with AI

psiegman logoepublib

a java library for reading and writing epub files

1,043
314
1,043
86

Top Related Projects

10,873

jsoup: the Java HTML parser, built for HTML editing, cleaning, scraping, and XSS safety.

19,281

The official source code repository for the calibre ebook manager

Quick Overview

The epublib project is a Java library that provides a simple and efficient way to read and write EPUB files. It allows developers to easily manipulate EPUB content, including the ability to extract, modify, and create new EPUB files.

Pros

  • Comprehensive EPUB Support: The library supports a wide range of EPUB features, including EPUB 2 and EPUB 3 specifications, making it a versatile tool for working with EPUB files.
  • Ease of Use: The library provides a straightforward API that simplifies the process of interacting with EPUB files, reducing the complexity for developers.
  • Performance: The library is designed to be efficient, with a focus on performance, allowing for fast processing of EPUB files.
  • Active Development: The project is actively maintained, with regular updates and bug fixes, ensuring its continued reliability and compatibility.

Cons

  • Limited Documentation: The project's documentation could be more comprehensive, making it potentially challenging for new users to get started.
  • Dependency on External Libraries: The library relies on several external dependencies, which may increase the complexity of integrating it into a project.
  • Lack of Advanced Features: While the library provides basic EPUB manipulation capabilities, it may not offer the full range of advanced features that some users might require.
  • Java-Specific: The library is written in Java, which may limit its accessibility to developers working in other programming languages.

Code Examples

Here are a few examples of how to use the epublib library:

Reading an EPUB File

EpubReader epubReader = new EpubReader();
Book book = epubReader.readEpub(new FileInputStream("path/to/book.epub"));

// Access the book's metadata
String title = book.getTitle();
String author = book.getAuthor().getFirstname() + " " + book.getAuthor().getLastname();

// Retrieve the book's content
List<Resource> resources = book.getResources();
for (Resource resource : resources) {
    String content = resource.getInputStreamAsString();
    // Process the content
}

This code demonstrates how to read an EPUB file, extract its metadata, and access the book's content.

Creating a New EPUB File

Book book = new Book();
book.setTitle("My New Book");
book.addAuthor("John", "Doe");

// Add content to the book
Resource resource = new Resource("content.html", "<html><body><h1>Hello, EPUB!</h1></body></html>");
book.addResource(resource);

EpubWriter epubWriter = new EpubWriter();
epubWriter.write(book, new FileOutputStream("path/to/new-book.epub"));

This code shows how to create a new EPUB file, set its metadata, and add content to it.

Modifying an EPUB File

EpubReader epubReader = new EpubReader();
Book book = epubReader.readEpub(new FileInputStream("path/to/book.epub"));

// Modify the book's content
Resource resource = book.getResources().get(0);
String content = resource.getInputStreamAsString();
content = content.replace("Hello, EPUB!", "Hello, World!");
resource.setInputStreamContent(content);

EpubWriter epubWriter = new EpubWriter();
epubWriter.write(book, new FileOutputStream("path/to/modified-book.epub"));

This code demonstrates how to read an existing EPUB file, modify its content, and write the updated EPUB file.

Getting Started

To get started with the epublib library, follow these steps:

  1. Add the epublib dependency to your project's build configuration. For example, in a Maven project, add the following to your pom.xml file:
<dependency>
    <groupId>nl.siegmann.epublib</groupId>
    <artifactId>epublib-core</artifactId>
    <version>3.1</version>
</dependency>
  1. Import the necessary classes from the epublib library in your Java code:
import nl.siegmann.epublib.domain.Book;
import nl.

Competitor Comparisons

10,873

jsoup: the Java HTML parser, built for HTML editing, cleaning, scraping, and XSS safety.

Pros of Jsoup

  • Jsoup is a Java library for parsing HTML and XML documents, which can be useful for web scraping and data extraction tasks.
  • Jsoup provides a simple and intuitive API for navigating, searching, and manipulating HTML elements, making it easy to work with web content.
  • Jsoup is widely used and has a large community, with extensive documentation and a wealth of online resources available.

Cons of Jsoup

  • Jsoup is primarily focused on HTML/XML parsing and manipulation, and may not provide the same level of functionality as a dedicated EPUB library like Epublib.
  • Jsoup may not be as well-suited for handling the more complex features and requirements of EPUB files, such as metadata, navigation, and content structure.

Code Comparison

Jsoup (HTML parsing):

Document doc = Jsoup.connect("https://example.com").get();
String title = doc.title();
Elements links = doc.select("a[href]");

Epublib (EPUB parsing):

Book book = new EpubReader().readEpub(new FileInputStream("book.epub"));
String title = book.getTitle();
List<Resource> resources = book.getResources();
19,281

The official source code repository for the calibre ebook manager

Pros of Calibre

  • Calibre is a comprehensive e-book management tool, offering features beyond just EPUB handling, such as conversion between various e-book formats, metadata management, and library organization.
  • Calibre has a large and active community, with extensive documentation and a wide range of plugins and customizations available.
  • Calibre is cross-platform, with versions available for Windows, macOS, and Linux.

Cons of Calibre

  • Calibre's codebase is significantly larger and more complex than epublib, which may make it more challenging for new contributors to get involved.
  • Calibre's focus on being a comprehensive e-book management tool may make it overkill for users who only need basic EPUB handling capabilities.

Code Comparison

Calibre:

from calibre.ebooks.oeb.base import OEBBook
from calibre.ebooks.oeb.polish.container import get_container

container = get_container('path/to/ebook.epub', tweak_mode=True)
book = OEBBook(container)

epublib:

EpubReader epubReader = new EpubReader();
Book book = epubReader.readEpub(new FileInputStream("path/to/ebook.epub"));

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

epublib

Epublib is a java library for reading/writing/manipulating epub files.

It consists of 2 parts: a core that reads/writes epub and a collection of tools. The tools contain an epub cleanup tool, a tool to create epubs from html files, a tool to create an epub from an uncompress html file. It also contains a swing-based epub viewer. Epublib viewer

The core runs both on android and a standard java environment. The tools run only on a standard java environment.

This means that reading/writing epub files works on Android.

Build status

  • Travis Build Status: Build Status

Command line examples

Set the author of an existing epub java -jar epublib-3.0-SNAPSHOT.one-jar.jar --in input.epub --out result.epub --author Tester,Joe

Set the cover image of an existing epub java -jar epublib-3.0-SNAPSHOT.one-jar.jar --in input.epub --out result.epub --cover-image my_cover.jpg

Creating an epub programmatically

package nl.siegmann.epublib.examples;

import java.io.InputStream;
import java.io.FileOutputStream;
 
import nl.siegmann.epublib.domain.Author;
import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.Metadata;
import nl.siegmann.epublib.domain.Resource;
import nl.siegmann.epublib.domain.TOCReference;

import nl.siegmann.epublib.epub.EpubWriter;
 
public class Translator {
  private static InputStream getResource( String path ) {
    return Translator.class.getResourceAsStream( path );
  }

  private static Resource getResource( String path, String href ) {
    return new Resource( getResource( path ), href );
  }

  public static void main(String[] args) {
    try {
      // Create new Book
      Book book = new Book();
      Metadata metadata = book.getMetadata();
       
      // Set the title
      metadata.addTitle("Epublib test book 1");
       
      // Add an Author
      metadata.addAuthor(new Author("Joe", "Tester"));
       
      // Set cover image
      book.setCoverImage(
        getResource("/book1/test_cover.png", "cover.png") );
       
      // Add Chapter 1
      book.addSection("Introduction",
        getResource("/book1/chapter1.html", "chapter1.html") );
       
      // Add css file
      book.getResources().add(
        getResource("/book1/book1.css", "book1.css") );
       
      // Add Chapter 2
      TOCReference chapter2 = book.addSection( "Second Chapter",
        getResource("/book1/chapter2.html", "chapter2.html") );
       
      // Add image used by Chapter 2
      book.getResources().add(
        getResource("/book1/flowers_320x240.jpg", "flowers.jpg"));
       
      // Add Chapter2, Section 1
      book.addSection(chapter2, "Chapter 2, section 1",
        getResource("/book1/chapter2_1.html", "chapter2_1.html"));
       
      // Add Chapter 3
      book.addSection("Conclusion",
        getResource("/book1/chapter3.html", "chapter3.html"));
       
      // Create EpubWriter
      EpubWriter epubWriter = new EpubWriter();
       
      // Write the Book as Epub
      epubWriter.write(book, new FileOutputStream("test1_book1.epub"));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

Usage in Android

Add the following lines to your app module's build.gradle file:

    repositories {
        maven {
            url 'https://github.com/psiegman/mvn-repo/raw/master/releases'
        }
    }

    dependencies {
        implementation('nl.siegmann.epublib:epublib-core:4.0') {
            exclude group: 'org.slf4j'
            exclude group: 'xmlpull'
        }
        implementation 'org.slf4j:slf4j-android:1.7.25'
    }