In an earlier post, I talked about Kindlegen and how to make it sing. This post talks about the key to all that singing: the OPF file. The OPF file is essentially a list of things for Kindlegen to do. It consists of three parts: the “manifest.” the “spine,” and the “guide.” You feed the OPF file to Kindlegen rather than the HTML file. Let me demystify the OPF file.
First of all, an OPF file is just a plain old ordinary text file. You DO NOT create it with your word processor. You create it with a text editor. If you are running Windows, you have Notepad (dreadful) and Wordpad (not quite so dreadful) available at your fingertips. I use TextPad. Lots of people use Notepad++.
The OPF file is an XML file, so it has some stuff in the header:
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="tut">
Notice right off the bat that I created my own value for the ‘unique-identifer.’ It doesn’t matter what it is, just be consistent between files.
We now come to the
metadata. The word “metadata” means data about data. Sort of data-squared. The metadata starts with, appropriately enough, a metadata tag:
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
Don’t worry what it means, just pick it up verbatim and put it in your file.
Now you need to add some metadata. You can probably figure out what to do from context:
<dc:title>The Unexpected Traveler</dc:title>
<meta name="cover" content="my-cover-image" />
Obviously your title goes in place of mine (The Unexpected Traveler). Copy the next two lines after the titleline verbatim. The line about the cover is just a prelude to actually telling it what the cover image is.
<dc:identifier id="tut" opf:scheme="ISBN">9781375890815</dc:identifier>
Use that line only if you have an ISBN. Hint: for a Kindle book, you don’t need an ISBN.
<dc:publisher>Mt. Sneffels Press</dc:publisher>
<dc:description>The best fantasy novel you've ever read!</dc:description>
Obviously you’ll put your own data in there. The date is the book’s release date. And you’ll want to make your description longer—think of it as the description that goes on the back cover. You close the metadata with the
The next element is the
manifest. Now this doesn’t mean “manifest destiny” or anything so magnificent. It’s just a list. A list of the cargo on a ship is called the ship’s manifest. That’s where the word comes from: blame the shipping industry. But it’s just a list. You start it and close it in the usual way:
What comes after is your list of HTML files. Note that I split my HTML into three parts: the front matter, the table of contents, and the rest. Each gets a mention in the manifest.
<item id="front_matter" media-type="application/xhtml+xml" href="tut_front_matter.html"></item>
The “front matter” is everything from the cover up until the start of the table of contents. Next, I pulled out the table of contents into its own html file:
<item id="item1" media-type="application/xhtml+xml" href="tuttoc.html"></item>
And now comes the rest of the book:
<item id="item2" media-type="application/xhtml+xml" href="tut.html"></item>
Now comes your cover image. In this case, you copy the tag verbatim except you put the file name of your cover image. In this case, I have a concept image that I’m using for testing. (You need to change JPEG to GIF if you’re using a GIF cover image.)
<item id="my-cover-image" media-type="image/jpeg" href="Brett_Concept.jpg"/>
Now this next part is very important. It looks like a table of contents. But, no, it’s an NCX file, which is the subject of another post. Note again that you keep everything verbatim except the actual file name.
<item id="toc" media-type="application/x-dtbncx+xml" href="tutbas.ncx"/>
Okay, the next part is the spine. The spine tells Kindlegen in what order you want things to appear. (Aha, you think, didn’t the manifest convey that information? No. The manifest is just a list. Not an ordered list. Just a list.)
The careful observer will note that the
idref tags refer to
id items in the manifest. Make your items match accordingly. Note especially here that you can break up your book into chunks to make handling easier. For example, each chapter can be its own HTML file. The
spine section sets the order so that Kindlegen can sew things together.
Now there’s just one last thing, the
guide section. You’ll note a suspicious relationship between the items in the
guide section and the navigational items found on a Kindle or Kindle App. Specifically here we tell Kindlegen that the Table of Contents is actually the Table of Contents that it’s looking for. Further, the Welcome is actually the place to which Kindle first opens the book, usually at the beginning of the first chapter.
<reference type="toc" title="Table of Contents" href="tuttoc.html"></reference>
<reference type="text" title="Welcome" href="tut.html#start_here"></reference>
We now close the file:
What could be simpler? (I’m laughing through gritted teeth. It took me hours to figure all this out. Hopefully my notes are a helpful adjunct to Amazon’s rather inadequate explanations.)