Manually creating an EPUB ebook

posted in: Technology | 0

There’s no obligation for a writer to know anything about the structure and content of an EPUB ebook.

It’s quite possible, if you use an ebook creation or conversion tool, to happily produce ebooks with absolutely no knowledge of the EPUB format at all. And you might argue that it’s like driving a car; you don’t have to know how to put the engine together, you just have to be competent behind the wheel.

I was happy initially to apply existing conversion tools to my word formatted or single html files. It was enough to just successfully produce epub or kindle formatted documents that I could read in an ereader.

But now this isn’t enough. I want to apply formats and layout to the text, and add structure to the table of contents, and so on. And that means understanding a little better the way an ebook is structured. The remainder of this post is dedicated to describing the contents of an EPUB ebook.

An EPUB ebook is a structured collection of files whose contents comply with the EPUB publication format, an open standard maintained by the International Digital Publishing Forum (IDPF). The files are collected into a container, a zip file with a .ebup file extension.

The EPUB standard has three components: a content markup standard (Open Publication Structure – OPS), a packaging standard (Open Packaging Format – OPF), and a container standard (Open Container Format – OCF). Most of this post relates to the OCF container standard.

The OCF standard sets out a number of mandatory files and a set of optional additions, depending on the nature of the ebook you want to produce. In its most basic form, the following files and folders (shown in caps with a / suffix) are required:

Zip Container:
  mimetype
META-INF/
  container.xml
OEBPS/
  content.opf
  toc.ncx
  cover.html
  chapter01.html
  chapter02.html
  …

There is a degree of flexibility in how an EPUB file should be structured. For instance, inside the OEBPS folder you can group the files in a number of ways; the text can be collected into a single file, or into chapters, and the chapter files can be collected into their own folder. Image files can also be stored in a folder, as can a stylesheet.

I want my manually created EPUB to fit the structure used by Sigil, the open-source EPUB editing tool. So, the file structure I am going to use in the example below is going to follow the structure used by Sigil.

For the purposes of this example, I am listing the requirements for a simple, non-DRM encrypted book. It will have a cover image, text broken into chapters and formatted according to a css stylesheet, and is configured for navigation through the ereader’s go to function.

Basic EPUB container structure:
mimetype
META-INF/
  container.xml
OEBPS/
  content.opf
  toc.ncx
  Styles/
    stylesheet.css
  Images/
    cover.jpg
  Text/
    cover.xhtml
    chapter01.html
    chapter02.html
    …

Steps to create the EPUB file.

The following steps assume that the relevant files have already been created in a folder structure. Details of each file will be covered in another post.

  1. Open a terminal and navigate to the relevant root folder
  2. Create the zip file with the mimetype as the first file:
    1. type: zip -0Xq my-ebook.epub mimetype
    2. the attributes do the following:
      • -0 indicates no compression, this should be applied to the mimetype file
      • -X do not save extra file attributes e.g. file times
      • -q quiet mode, eliminates informational messages during compression
  3. Add the folders META-INF and OEBPS and their files:
    1. type: zip -Xr9Dq my-ebook.epub *
    2. the attributes do the following:
      • -X do not save extra file attributes e.g. file times
      • -r recursive zip acts on all contents of the folders
      • -9 slowest compression method
      • -D do not create entries in archive for directories
      • -q quiet mode, eliminates informational messages during compression

Steps to create a mimetype file

The minetype file is a special text file that identifies the zip file as an EPUB container. To create this file:

  1. open a text file and type application/epub+zip
  2. make sure there a no additional spaces or carriage returns
  3. save the file with the name mimetype

Other files in the EPUB container

The zipped file created above is a valid EPUB ebook. The content and structure of the other files in this container will be covered in a later post.