epub.js icon indicating copy to clipboard operation
epub.js copied to clipboard

How can I get the page number for a specific TOC item

Open OriKuttner opened this issue 4 years ago • 3 comments

For example my TOC (nav file) contains href's to s1.xhtml, s2.xhtml etc. How can I get the page number of each href?

OriKuttner avatar Dec 01 '21 05:12 OriKuttner

I don't think this is possible. Page size is relative. A page on an iPhone is smaller than a page on a 27" desktop monitor, so you can't properly tell how many pages a book or a section is without rendering the entire book first.

You can use percentageFromCfi to roughly know what percentage a CFI (or section) is through a book: https://github.com/futurepress/epub.js/blob/master/documentation/md/API.md#percentagefromcfi

sbatson5 avatar Dec 17 '21 19:12 sbatson5

When epub.js starts it calculates 'locations' I am using this as pages (since there is no exact page as each screen is different) I would like to know the location number from the TOC href.

OriKuttner avatar Dec 18 '21 09:12 OriKuttner

Get a CFI for your href first,

const id = href.split('#')[1]
const section = book.spine.get(href)
await section.load(book.load.bind(book))
const el = id ? section.document.getElementById(id) : section.document.body
const cfi = section.cfiFromElement(el)
section.unload()

Then use percentageFromCfi.

johnfactotum avatar Dec 18 '21 18:12 johnfactotum