All formats

PHP eBook

LanguagePHP 8.1+
LicenseMIT
Repositorygithub.com/kiwilan/php-ebook
DocumentationREADME
Depends onzip extension for EPUB; kiwilan/php-archive, kiwilan/php-xml-reader
Read / writeyes / basic (assembles an EPUB from files you provide)

PHP eBook reads metadata from e-books with one API for all formats: Ebook::read() returns an Ebook with title, authors, description, publisher, identifiers, language, tags, series and cover. For EPUB it parses container.xml, the OPF package (EPUB 2 and 3, including Calibre’s calibre:series) and the NCX, and can return the chapters with their HTML. It also reads MOBI, FB2, PDF and comic book archives.

It’s built for library catalogs and OPDS servers (it pairs with kiwilan/php-opds). EPUB creation is low-level: you add the mimetype, container.xml, OPF and content files yourself.

Installation#

Shell
composer require kiwilan/php-ebook

Basic usage: read EPUB metadata#

PHP
<?php
require 'vendor/autoload.php';

use Kiwilan\Ebook\Ebook;

$ebook = Ebook::read('book.epub');
if ($ebook === null) {
    exit("not a supported e-book\n");
}

echo 'Title:     ', $ebook->getTitle(), "\n";
foreach ($ebook->getAuthors() as $author) {
    echo 'Author:    ', $author->getName(), "\n";
}
echo 'Language:  ', $ebook->getLanguage(), "\n";
echo 'Publisher: ', $ebook->getPublisher(), "\n";
echo 'Series:    ', $ebook->getSeries(), ' #', $ebook->getVolume(), "\n";
foreach ($ebook->getIdentifiers() as $id) {
    echo 'ID:        ', $id->getValue(), ' (', $id->getScheme(), ")\n";
}

How to extract the cover of an EPUB#

PHP
$ebook = Ebook::read('book.epub');
$cover = $ebook?->getCover();
if ($cover !== null) {
    $cover->saveTo('cover.jpg');
    // or: $data = $cover->getContents(); $base64 = $cover->getContents(true);
}

How to get the chapters of an EPUB#

getChapters() merges the NCX table of contents with the HTML files:

PHP
$epub = Ebook::read('book.epub')?->getParser()?->getEpub();
foreach ($epub->getChapters() as $chapter) {
    echo $chapter->getLabel(), ' (', $chapter->getSource(), "): ";
    echo strlen(strip_tags($chapter->getContent())), " characters\n";
}

How to read raw OPF metadata#

PHP
$opf = Ebook::read('book.epub')?->getParser()?->getEpub()?->getOpf();
echo 'EPUB version: ', $opf->getEpubVersionString(), "\n";
echo 'dc:title:     ', $opf->getDcTitle(), "\n";
print_r($opf->getDcSubject());
print_r($opf->getManifest());

How to count words and pages#

PHP
$ebook = Ebook::read('book.epub');
echo $ebook->getWordsCount(), " words, about ", $ebook->getPagesCount(), " pages\n";

Pages are estimated at 250 words per page. Counting reads every chapter, so it’s done only when you call these methods.

How to create an EPUB#

Ebook::create() returns a ZIP builder; add the files in order, mimetype first:

PHP
$creator = Ebook::create('new.epub');
$creator->addFromString('mimetype', 'application/epub+zip')
    ->addFromString('META-INF/container.xml', $containerXml)
    ->addFromString('OEBPS/content.opf', $opfXml)
    ->addFromString('OEBPS/chapter1.xhtml', $chapterXhtml)
    ->save();

Tips#