After the PDF-to-image tool, the next question was whether a PDF could become an EPUB. I explained that pages could go in as pictures or the text could be extracted, and the answer was: text extraction only.
A PDF has no paragraphs
A PDF is a set of instructions: put this glyph at this spot on the page. Reading one page with pdf.js returns hundreds of text fragments with x and y coordinates. Lines are fragments at the same height, and paragraphs are decided at the gap between lines. A new paragraph starts if the line spacing is more than 1.5 times the usual, if the next line is indented and the previous one ended with sentence punctuation, or if the previous line stopped short of the right margin and ended with a period. The same rules apply across page breaks. Odd and even pages can have different margins, so the left margin is measured separately for each.
Test documents
I needed PDFs with a known answer. Eight of this site's making-of articles in Korean and seven in English were typeset as an A5 book and printed to PDF: a large heading per chapter, first-line indents, justified text, the book title at the top, page numbers at the bottom, and bookmarks. The Korean book was made twice, once breaking lines anywhere including mid-word (break-all) and once only between words (keep-all). The spacing of each conversion was then checked against the original, space by space.
Korean at the end of a line
The first run reproduced only 22 of 56 English paragraphs and 12 of 62 Korean ones exactly. In English the cause was that Chrome's automatic hyphenation uses U+2010 rather than an ordinary hyphen; once that was handled, all 3,958 spaces matched.
Korean was harder. A line ending in "난이" followed by "도였다" must be joined, while "예외" followed by "여섯" must be spaced. To decide without a dictionary, the tool gathers statistics from the inside of lines in the same document: how often up to three characters before the break and up to two after appear joined versus with a space, narrowing the window when evidence runs thin, and finally falling back to how often each character starts or ends a word. It also first decides whether the document breaks lines mid-word at all; if it breaks only between words, Korean at a line end always gets a space.
In the keep-all book, most of the remaining errors were around punctuation. Chrome also breaks lines before "『" and "(", and full-width brackets and ㎡ carry blank space inside their glyph width, so pdf.js adds a space there. New rules attach opening brackets to what follows and closing brackets to what precedes, and remove spaces between numbers and units.
Results
Out of 3,092 spaces, the keep-all Korean PDF had 1 error and the break-all one had 21 (99.3%). English had 0 of 3,958. Each EPUB was checked for the mimetype entry at the start of the archive, XML well-formedness and the presence of every file listed in the manifest, then opened in a browser EPUB engine (epub.js) to confirm the title, language, eight contents entries, cover and body text.