cyrilis / epub-gen

Generate EPUB books from HTML with simple API in Node.js.
430 stars 138 forks source link

FEATURE: Add cover to first page (cover doesn't show on eReaders) #64

Open actuallymentor opened 6 years ago

actuallymentor commented 6 years ago

I noticed that while apps like iBooks respect the cover in the meta, many eReaders (kobo) show the first page of the epub as the title page.

What this results in is the table of contents showing as the book thumbnail.

Is this a known issue?

I'm happy to do a PR for this functionality.

Firsh commented 2 years ago

You can create such a page manually, but you need to "register" it. So when you convert with Calibre to AZW3 it gets recognized therefore the Kindle doesn't show it twice.

{
  title: 'Cover',
  data: `<section epub:type="cover">` +
    `<div class="cover-image"><img alt="Cover" id="coverimage" src="${cover}"/></div>` +
    `</section>`,
  beforeToc: true,
  excludeFromToc: true,
  filename: 'cover.xhtml'
}

If you include it in the toc, you'll end up with a broken link in the AZW3. If you exclude it from the toc, epub-gen also excludes it from the ncx, making the page disappear. Therefore check the attached template (I modified it from the source) which fixes it.

Near the end where the toc is registered in guide, the cover needs to be there as well:

    <guide>
        <reference type="text" title="Table of Content" href="toc.xhtml"/>
        <reference href="cover.xhtml" title="Cover" type="cover"/>
    </guide>

Also, where the spine toc ncx begins notice I played with the conditions to reinclude the Cover-titled page:

    <spine toc="ncx">
        <% content.forEach(function(content, index){ %>
            <% if(content.beforeToc && (!content.excludeFromToc || content.title == 'Cover')){ %>
                <itemref idref="content_<%= index %>_<%= content.id %>"/>
            <% } %>
        <% }) %>
        <itemref idref="toc" />

Download modified template: content.opf.zip

Load it via the top-level option:

        customOpfTemplatePath: path.join(__dirname, 'content.opf.ejs'),