Manifest¶
The Manifest class reads and edits the <manifest> section of the OPF file: the list of every resource in the book (documents, styles, images, fonts, navigation).
EpubFile::load() creates it; get it with EpubFile::getManifest(). ContentManager uses it to keep the manifest in sync when files are added or deleted.
Paths and hrefs¶
Manifest hrefs are URLs relative to the OPF file (for example text/chapter%201.xhtml next to EPUB/package.opf). The Manifest translates them to paths relative to the book root (EPUB/text/chapter 1.xhtml), which is what ContentManager expects. Hrefs that would escape the book are rejected; remote resources (EPUB 3 allows e.g. streamed audio) get an empty path.
Key Methods¶
-
__construct(SimpleXMLElement $opfXml, string $opfPath):$opfPathis the OPF location relative to the book root, as returned byParser::parse(). ThrowsInvalidEpubExceptionif the package has no manifest. -
getItems(): array: All items asManifestItemobjects withid,href,path,mediaTypeandproperties.pathis empty for remote resources and for hrefs that point outside the book; such items are listed but have no file. -
get(string $id): ?ManifestItemandfindByPath(string $path): ?ManifestItem: Look up one item. -
add(string $path, ?string $mediaType = null, ?string $id = null): ManifestItem: Adds a file. The media type is guessed from the extension (XHTML, CSS, JavaScript, NCX, SMIL, PLS, XML, JSON, text, WebVTT, the common image, font, audio and video formats;application/octet-streamotherwise) and the id is derived from the file name when not given. Throws if the path is already listed, the id is already used anywhere in the package, or the media type or id is not valid UTF-8 XML text. -
remove(string $id): void: Removes an item and the package references to it (EPUB 2 cover meta, refinements,spine@toc,fallback/media-overlayof other items,<guide>references to its file). Spine entries are not touched; useSpine::remove()(orContentManager::deleteContent(), which does both). -
pathToHref(string $path): stringandhrefToPath(string $href): string: Convert between the two forms. -
addProperty(string $id, string $property): voidandremoveProperty(string $id, string $property): void: Add or remove an EPUB 3 property token (e.g.cover-image,nav) on an item; other tokens are kept. -
isEpub3(): bool: Whether the package is EPUB 3 (version 3.x), whose items carry properties. -
setMediaType(string $id, string $mediaType): void: Changes an item's media type, e.g. after its file was replaced with another format. -
moveItem(string $id, string $path): void: Points an item at another file (path relative to the book root), keeping its id, and updates<guide>references to its old file. The file itself is not moved;ContentManager::moveContent()moves both. Throws if no item has the id or another item already has the path. -
findByHref(string $href): ?ManifestItem: Looks up an item by an href relative to the OPF file (fragments ignored);nullwhen nothing matches or the href points outside the book. -
getGuidePath(string $type): ?string: The book-root path of the EPUB 2<guide>reference of a type such ascoverortoc(matched case-insensitively), ornull. -
removeGuideReferences(string $type): void: Removes the EPUB 2<guide>references of a type (compared case-insensitively), and the guide when none is left. -
getGuideReferences(): arrayandsetGuideReferences(array $references): void: The EPUB 2<guide>asLandmarkobjects that carry the guide's own types (cover,text,title-page, ...); references that point outside the book are left out. Setting replaces the guide (created after the spine when missing) and[]removes it. UseTableOfContents::getLandmarks()andsetLandmarks()to work with EPUB 3 types and the navigation document at the same time. -
getMediaOverlay(string $id): ?stringandsetMediaOverlay(string $id, ?string $overlayId): void: The SMIL item (application/smil+xml) that narrates an XHTML or SVG item (itsmedia-overlayattribute);nullremoves it. EPUB 3 only. See EPUB 3 Features. -
getOpfPath(): string: The OPF location relative to the book root.
Lookups (get(), findByPath(), getItems()) use an index built on first use, so adding thousands of files to a large book stays fast.
Usage Example¶
use PhpEpub\EpubFile;
$epubFile = EpubFile::open('/path/to/your.epub');
$manifest = $epubFile->getManifest();
foreach ($manifest->getItems() as $item) {
echo "{$item->id}: {$item->path} ({$item->mediaType})\n";
}
// Register a font that was copied into the book by other means
$manifest->add('EPUB/fonts/Literata.woff2');
$epubFile->save();