epub4kmp-core

A Kotlin Multiplatform library for reading, writing, and manipulating EPUB files — a KMP fork of epub4j with all JVM-only code replaced by multiplatform equivalents (okio, xmlutil, kotlinx-datetime). Runs on JVM, Android, iOS, macOS, Linux, and Windows.

Try it live: the EPUB reader running on Wasm »

implementation("com.darkrockstudios:epub4kmp-core:0.2.0")

For drop-in Compose reader UI on top of this, see the epub4kmp-compose-ui module.

Reading an EPUB

Create an EpubReader and read from an okio Path (or any Source). The result is a Book:

import io.documentnode.epub4kmp.epub.EpubReader
import okio.FileSystem
import okio.Path.Companion.toPath

val book = EpubReader().readEpub(FileSystem.SYSTEM, "book.epub".toPath())

println(book.title)
book.metadata.authors.forEach { println("${it.firstname} ${it.lastname}") }

For large books, keep heavy resources out of memory by lazy-loading them from the ZIP on demand — pass the MediaTypes you want deferred:

val book = EpubReader().readEpub(
fileSystem = FileSystem.SYSTEM,
zipPath = "book.epub".toPath(),
lazyLoadedTypes = listOf(MediaTypes.JPG, MediaTypes.PNG),
)

Inspecting the book

A Book exposes the EPUB's structure:

book.spine.getSpineReferences()           // reading order
book.tableOfContents.getTocReferences() // navigation tree
book.coverImage // cover Resource, if any

val html = book.spine.getResource(0)?.asString() // first chapter's XHTML

Writing / creating an EPUB

Build or modify a Book and write it with EpubWriter:

import io.documentnode.epub4kmp.epub.EpubWriter
import io.documentnode.epub4kmp.util.ResourceUtil
import okio.buffer

book.addSection("Afterword", ResourceUtil.createResource("Afterword", "afterword.html"))

FileSystem.SYSTEM.sink("out.epub".toPath()).buffer().use { sink ->
EpubWriter().write(book, sink)
}

Hook the read/write pipeline with a BookProcessor when you need to transform content (the default writer uses one to auto-link stylesheets).

Packages

Link copied to clipboard
common
Link copied to clipboard
common

Helpers for building a reader UI: Navigator tracks the current position in a book and emits NavigationEvents, with NavigationHistory for back/forward.

Link copied to clipboard
common

The EPUB object model: Book and everything it holds — Metadata, Resources / Resource, Spine, TableOfContents, Guide, plus the MediaTypes registry and a stylesheet DSL.

Link copied to clipboard
common

The read/write entry points: EpubReader, EpubWriter, and the BookProcessor pipeline for transforming a Book during reading or writing.

Link copied to clipboard
common

Small public helpers, most notably ResourceUtil for creating Resources and parsing them as XML.