More Links

An ePub Tutorial‎ > ‎

Preparing the Basic Structure

Notes

The structure for ePub is more strict than for Mobipocket or, indeed, many other ebook formats.

Here’s a step-by-step guide. After this, you might want to copy this directory and its contents somewhere else so you can use it as a template instead of doing this annoying thing over and over again.

Another alternative is to use the Ruby Epub Tools, although this tutorial doesn't cover using the tools (for more information, see An EBookery Workflow).


Create a base directory.

It doesn’t have to be perfectly named after the book itself, since the actual title is included in a metadata file we’ll create later.

Velveteen Rabbit - Root Folder


In the base directory, create the following folders:

META-INF and content.

I’ll note that I’m an odd duck, because I like to square away my content in a separate directory so that the text/images are separate from the metadata about the book.

Velveteen Rabbit - META INF and content directories


Create a mimetype file.

It’s not going to be complicated; it just contains the single line:

application/epub+zip

It should have no suffix at all (e.g., it should just be called “mimetype”, not “mimetype.txt”) nor extra spacing or empty lines.

Velveteen Rabbit - mimetype


Create a basic toc.ncx file.

This lists navigation points (like chapters). What you put here will show up in the left part of Adobe Digital Editions.

Velveteen Rabbit - Basic toc.ncx

We’re going to add more to this as we add more files to our content directory, but we’ll start with the skeleton.

toc.ncx Template

<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">
<head>
<meta name="dtb:uid" content="Spontaneous Derivation [2008.12.10-21:02:00]"/>
<meta name="dtb:depth" content="1"/>
<meta name="dtb:totalPageCount" content="0"/>
<meta name="dtb:maxPageNumber" content="0"/>
</head>
<docTitle>
<text>The Velveteen Rabbit</text>
</docTitle>
<navMap>
</navMap>
</ncx>

You should replace the title between the <text></text>, and replace the content attribute of <meta name="dtb:uid" content="..."/> with your own unique id. This id will be the same as the one in the metadata.opf file, coming up next…


Create a basic metadata.opf file.

We’re going to add more to this as we add more files to our content directory, but we’ll start with the skeleton.

Velveteen Rabbit - Basic metadata.opf

metadata.opf Template

The contents of metadata.opf (and this time the suffix needs to be .opf) is the following XML:

<package xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="bookid">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
<dc:title>The Velveteen Rabbit or How Toys Become Real</dc:title>
<dc:creator opf:file-as="Williams, Margery" opf:role="aut">Margery Williams</dc:creator>
<dc:creator opf:file-as="Nicholson, William" opf:role="ill">William Nicholson</dc:creator>
<dc:language>en-US</dc:language>
<dc:identifier id="bookid">Spontaneous Derivation [2008.12.10-21:02:00]</dc:identifier>
<dc:rights>Public Domain</dc:rights>
</metadata>
<manifest>
<item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml"/>
</manifest>
<spine toc="ncx">
</spine>
</package>

dc: (Dublin Core) tags

The meanings of some of the dc: tags in the metadata:

<dc:title>

Real title of the work.

<dc:creator>

Author/contributor of the work. The name format should be first name, last name. You’ll also want:

opf:file-as=”[last name], [first name]“
For the sanity of ebook libraries that need to file books by last name first.

opf:role=”[aut|ill|etc]“
Useful for indicating writers (”aut”) versus illustrators (”ill”), translators (”trl”), and more (see this table of the OPF spec for more roles).

You can have multiple creators, and thus multiple <dc:creator> tags.

<dc:language>

The language code for this book; needs to be an IETF language code; for more on this, see Language Code on Wikipedia.

epubcheck alerts you if the language code is invalid.

<dc:identifier>
An ID string that uniquely identifies your book. For its id="something" attribute, "something" must the unique-identifier="something" on the very first line (you may need to scroll right to see it). This something is just the name of the schema of the id string.

I just use “bookid” as the something, because it’s the most flexible, and then a string with my website name and a date/time stamp. You can use ISBN strings, or really anything, so long as it’s likely unique.

You can even have multiple <dc:identifier> if for some reason an ebook store/system cares about a specific format.

<dc:rights>
Unlike the above dc: tags, this isn’t necessary for epub standards-compliance, but I like to include it. It’s nice to notify people that a work is Public Domain versus the different types of Creative Commons, so as to help forestall nasty emails.

If you have copyright plus a license (like creative commons), you can have multiple <dc:rights>—one for the copyright, and one for the license.

There are other, optional dc tags; see Ainsworth’s guide.


Create a container.xml file in the META-INF directory.

Very simple and very boilerplate; the only thing you really care about is that the full-path of the rootfile points to the above metadata.opf.

Velveteen Rabbit - Basic container.xml

This file belongs inside META-INF, not in the root directory.

container.xml Template

<?xml version="1.0"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="metadata.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>

Now we're ready to move on to Adding Content.