Core concepts
The book is written as you go
An EPUB is a ZIP archive. MejiroEPUB writes that archive front to back while you are still building it:
| When | What is written |
|---|---|
new Document(...) | the container files and the stylesheet |
cover() / image() | the image file, copied in chunks |
section() | that section's XHTML — and then it is forgotten |
finalize() (or save() / output()) | the table of contents, the package document, the ZIP directory |
Between calls, the library keeps a small record per section (its id, file name and title, about 1 KB) and nothing of its text. So memory grows with the number of sections, never with their length, and a body you pass as a generator is held one line at a time.
Measured on a real serial of 132 episodes and 551,046 characters: 0.13 s in the library, a peak of 0.52 MB above the starting point, a 0.65 MB EPUB. The test suite holds the library to this on every change — eight times the text must cost less than twice the memory — because a leak here would not change a single byte of the output, and so no other test could catch it.
The one rule it imposes: order
Because the archive is written front to back, what comes first in the book must be given first:
cover()must be called before the firstsection(). Calling it later throws aLogicException; the sections already written cannot be moved behind it.- Sections appear in the order you add them.
- Images can be written at any time before
finalize(), and placed in any later section.
Sections
A section is one episode, one chapter: one XHTML file and one entry in the table of contents.
$book->section('第一話 旅立ち', $body);
The title is a single line used both as the heading and in the table of contents. Notation in it is reduced to plain text — ruby keeps its base, a footnote is dropped — rather than expanded, since a contents entry cannot hold ruby.
The body is either a string or any iterable (an array, a generator, a database cursor) whose items are:
- strings, read with the document's notation — a string with line breaks in it is split into lines;
Paragraphobjects, built from parts and never reinterpreted;Imageobjects returned byimage(), placed as a block of their own.
One line is one paragraph
Every line becomes one paragraph. This is how web novels are written — the line break itself is part of the writing — and it is also how MejiroPDF reads text.
\r\nand\rare treated as\n.- A blank line is kept as a blank paragraph, in a form reading systems do not collapse. Writers use blank lines on purpose.
- Leading full-width spaces are kept. In Japanese text they are the indent, typed by the writer; the stylesheet adds no indent of its own.
- No notation spans a line break.
Finishing
$book->save('/path/to/book.epub'); // finalize, then copy to a file
$bytes = $book->output(); // finalize, then return the bytes
$book->finalize(); // when writing to your own stream (a "sink")
Without a sink, the EPUB is assembled in a php://temp buffer (memory up to
2 MB, a temporary file beyond), so save() and output() can hand it over at
the end. With a sink, the bytes have already gone where you sent them — see
Serving downloads.
A book with no sections is not a valid EPUB, so finalizing one throws
ME-PKG-7001. Adding to a book after it is finalized throws ME-PKG-7002.
Reproducible output
Pass modified: and DocumentInfo::$identifier, and the same input gives a
byte-identical EPUB:
$book = new Document(
new DocumentInfo('吾輩は猫である', 'ja', identifier: 'https://example.com/novels/42'),
modified: new DateTimeImmutable('2026-12-06T09:30:00+09:00'),
);
modified is recorded as the book's modification time and on every archive
entry. It defaults to now.