Skip to main content

MejiroEPUB

A modern, stream-first, and memory-efficient EPUB 3 generation engine for PHP 8.3+.

download.php
use MejiroEPUB\Document;
use MejiroEPUB\DocumentInfo;
use MejiroEPUB\Text\Notation;
use MejiroEPUB\Text\WritingMode;

$book = new Document(
new DocumentInfo(
title: $novel->title,
language: 'ja',
author: $novel->author,
),
writingMode: WritingMode::VERTICAL_RL,
notation: Notation::novel(), // |漢字《かんじ》
);

// Straight from the database.
foreach ($episodes as $episode) {
$book->section($episode->title, $episode->body);
}

$book->save('novel.epub');

Memory does not follow the length of the book

Each episode is written into the EPUB the moment it is given, then forgotten. What stays is one table-of-contents entry, so a serial of a thousand episodes costs what one does.

550K
characters
132 episodes of a real serial
0.52
MB
peak memory while writing it
0.13
seconds
in the library, start to finish

Measured on a published serial. A test also asserts that eight times the text costs less than twice the memory, on every change. How it works →

Built for

  • Web-novel sites offering a whole work as an EPUB, from the manuscripts they already store.
  • Vertical Japanese — right-to-left pages, ruby, emphasis dots, tate-chu-yoko.
  • Texts you also typeset with MejiroPDF: one manuscript, a PDF and an EPUB.
  • Text written by other people. Markup is escaped and broken input is cleaned.

Deliberately not

  • It does not read or edit existing EPUBs. It writes them.
  • It does not make fixed-layout books. Comics and picture books need a page-by-page layout.
  • It does not choose fonts or sizes. E-book readers pick their own, and the stylesheet leaves them that choice.

Before you depend on it

AGPLv3

Strong copyleft, and the network clause counts: if your users reach your application over a network, you must offer them its complete source under AGPLv3. Read this before you build on it, not after.

What that means →
There is no support

None is promised. Bug reports are welcome and are used to improve the library, but there is no response time and no obligation to act on any one of them.

Pull requests are not accepted

Dual licensing requires sole copyright, so external contributions cannot be merged. Issues are open: report a defect and it will be read, and fixed by the author if it is real.

Report a defect →

The same manuscript, as a PDF

MejiroPDF is the sister library: it typesets the same text into a vertical PDF, with the same notation and the same streaming design.

MejiroPDF →