PHP eBook
| Language | PHP 8.1+ |
|---|---|
| License | MIT |
| Repository | github.com/kiwilan/php-ebook |
| Documentation | README |
| Depends on | zip extension for EPUB; kiwilan/php-archive, kiwilan/php-xml-reader |
| Read / write | yes / 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#
composer require kiwilan/php-ebook
Basic usage: read EPUB metadata#
<?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#
$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:
$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#
$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#
$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:
$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#
Ebook::isValid('file.epub')checks the file before reading it.$ebook->getMetaTitle()->getSlug()builds a slug from series, volume, title, author and year, useful for file names.- DRM-protected EPUBs may give metadata but no chapter content.