Modifying epub files to include ‘sort by’ metadata

As most people know, books are usually organised by the surname of the (first) author, alphabetically. As some others will know, ebooks are not always great at this; iTunes sometimes thinks it should sort a book by first name instead of last name, and my Nook sorts by the last word of the first author’s name regardless of where their surname is. I’ve written about my tinkering with Calibre and ebooks before and this blog post is a short snippet that builds on that. As before, I’m using Calibre to perform the tweaks.

I’ve been trying to fix this problem by including something that looks like the following in the OPF file of my epub files:

<dc:creator opf:file-as="Beagle, Peter S." opf:role="aut">Peter S. Beagle</dc:creator>

The dc:creator tag is used by the epub specification to identify the contents of the tag as a creator of the work. The opf:role="aut" is there to identify Peter S. Beagle specifically as the author of the work. opf:file-as="Beagle, Peter S." is obviously there to make it clear what the sort criterion should be for this file.

There’s only one problem with including this line: it doesn’t always work. Sometimes it does, but often my Nook will complain of an unreadable file (even though Calibre will read it just fine). So, what’s up? I have been puzzling over this issue for ages, and today I finally cracked it.

At the start of your book, there will be a <package> tag, and then below that a <metadata> tag. In order for your epub to be able to use the opf:role and opf:file-as attributes, the <metadata> tag has to look like this (or at least include this part):

<metadata xmlns:opf="">

Making sure that your <metadata> tag is correct solves the problem. Wahey!

Setting a cover image on an ePub ebook

As some of you may already know, I recently received a Barnes & Noble Nook for Christmas. This has been a great gift and I’m thoroughly enjoying using it to read a lot more literature, but there’s a problem that has been recurring: some publishers don’t properly set the cover art on an e-book, meaning the cover doesn’t show up on my Nook. Since, for me, everything really should be perfect (it’s a disease, trust me), I have been attempting to rectify this problem with my ebooks and I’ve finally come upon the solution. It’s fairly simple to do, as long as you’re a little comfortable with editing in HTML.

The first thing to do if you’re having trouble is to download Calibre and install it. Calibre is a fairly clunky piece of open-source software but it’s very full-featured and allows for ebooks to be edited. Once you’ve installed Calibre, run it and drag the problematic ebook into the library. Now, there are two ways to proceed.

One way (and arguably the easy way) is to click ‘Edit Metadata’, set the correct cover image and then convert the file into an epub; Calibre will embed the correct cover image into the new file. However, this method doesn’t sit well with me and so I’m going to outline how I do it below.

Right-click on the ebook and click ‘Tweak Book’, then click ‘Explode Book’. Look for a file that ends in ‘.opf’ (this will probably be in a folder called OEBPS) and open this file in your favourite text editor.1 There will be, somewhere in this file, a line that says <manifest>. You want to find the ID of the cover within this tag. It’ll probably be something like

<item id="cover_image" href="cover.jpg" media-type="image/jpeg">

and so the ID is cover_image in this case. Now go up to the <metadata> tag and insert a piece of text before the end:

<meta name="cover" content="cover_image"/>

This will let the ePub file know where the cover image is contained, and should therefore show up properly on your Nook. It’s important to note that it has to be in this order: if content comes before name, it will not display correctly. This is because of a bug in the Nook’s implementation of the EPUB specification.

  1. I use TextWrangler on my Macs and Notepad++ on my PCs; your mileage will vary.