epublib Documentation

repository·master·Indexed 22 days ago

https://github.com/psiegman/epublib

A Java library for reading, writing, and manipulating EPUB files. It includes a core engine for programmatic EPUB creation using Book, Metadata, and EpubWriter classes, as well as a suite of tools for cleanup, conversion, and viewing. Features include the Fileset2Epub tool for generating EPUBs from HTML or .chm files, a Java Swing-based EPUB viewer, and compatibility with Android projects via the epublib-core module.

Tokens
2.1K
Snippets
5
Records
6
Agent score
28%

What's inside epublib

  1. Setup epublib in an Android project

    master

    The epublib-core module is compatible with Android. To integrate it, add the following repository and dependencies to your app/build.gradle file. Note that org.slf4j and xmlpull are excluded from the core dependency to avoid conflicts, and slf4j-android is added for logging support.

    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'
    }
  2. Create an EPUB programmatically with Java

    master

    You can build an EPUB file from scratch using the Book, Metadata, and EpubWriter classes.

    1. Initialize a new Book object.
    2. Configure Metadata by adding a title via metadata.addTitle() and authors via metadata.addAuthor(new Author(firstName, lastName)).
    3. Set a cover image using book.setCoverImage(Resource).
    4. Add content sections (chapters) using book.addSection(title, Resource). This returns a TOCReference which can be used to nest sub-sections.
    5. Add supporting resources like CSS or images to book.getResources().
    6. Use EpubWriter to write the Book object to a file.
    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();
        }
      }
    }
  3. Reference Fileset2epub command line arguments

    master

    The Fileset2Epub tool accepts the following arguments to configure the generated EPUB:

    • --author [lastname,firstname]: Sets the author name.
    • --cover-image [image to use as cover]: Specifies the image to be used as the book cover.
    • --input-ecoding [text encoding]: The encoding of the input HTML files. If character encoding issues occur (e.g., 'funny characters'), try using iso-8859-1, windows-1252, or utf-8.
    • --in [input directory]: The directory containing the input files.
    • --isbn [isbn number]: The ISBN number for the book.
    • --out [output epub file]: The path where the resulting EPUB file will be saved.
    • --title [book title]: The title of the book.
    • --type [input type, can be 'epub', 'chm' or empty]: Specifies the input format.
    • --xsl [html post processing file]: A file used for HTML post-processing.
  4. Use epublib command line tools

    master

    The epublib tools allow you to manipulate existing EPUB files via the CLI. You can set the author or the cover image of an EPUB file using the following commands:

    Set the author: Use --in for the input file, --out for the output file, and --author to specify the author (comma-separated names).

    Set the cover image: Use --in for the input file, --out for the output file, and --cover-image to specify the path to the new cover image.

    # 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