Skip to content

Instantly share code, notes, and snippets.

@stormwild
Last active January 12, 2024 18:06
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stormwild/86673836eb6153e6ab2e65b4353a289e to your computer and use it in GitHub Desktop.
Save stormwild/86673836eb6153e6ab2e65b4353a289e to your computer and use it in GitHub Desktop.

Epub Tutorial

Pre-requisite

Creating eBooks in the EPUB format

Listing 1. Directory and file layout for a simple EPUB archive (EPUB 2 file structure )

mimetype
META-INF/
   container.xml
OEBPS/
  content.opf
  title.html
  content.html
  stylesheet.css
  toc.ncx
  images/
     cover.png

EPUB 3

mimetype
META-INF/
   container.xml
EPUB/
  package.opf
  cover.xhtml
  content.xhtml
  nav.xhtml
  stylesheet.css
  toc.ncx
  images/
     cover.png

The mimetype file

This one's pretty easy: The mimetype file is required and must be named mimetype. The contents of the file are always:

application/epub+zip

Note that the mimetype file cannot contain any newlines or carriage returns.

Additionally, the mimetype file must be the first file in the ZIP archive and must not itself be compressed. You'll see how to include it using common ZIP arguments in Bundling your EPUB file as a ZIP archive. For now, just create this file and save it, making sure that it's at the root level of your EPUB project.

META-INF/container.xml

At the root level of the EPUB, there must be a META-INF directory, and it must contain a file named container.xml. EPUB reading systems will look for this file first, as it points to the location of the metadata for the digital book.

Create a directory called META-INF. Inside it, open a new file called container.xml for writing. The container file is very small, but its structural requirements are strict. Paste the code in Listing 2 into META-INF/container.xml.

Listing 2. Sample container.xml file

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

EPUB 3 example

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

The value of full-path (in bold) is the only part of this file that will ever vary. The directory path must be relative to the root of the EPUB file itself, not relative to the META-INF directory.

More about META-INF

The META-INF directory can contain a few optional files, as well. These files allow EPUB to support digital signatures, encryption, and digital rights management (DRM). These topics are not covered in this tutorial. See the OCF specification for more information.

The mimetype and container files are the only two whose location in the EPUB archive are strictly controlled. As recommended (although not required), store the remaining files in the EPUB in a sub-directory. (By convention, this is usually called OEBPS, for Open eBook Publication Structure, but can be whatever you like.)

Open Packaging Format metadata file

Although this file can be named anything, the OPF file is conventionally called content.opf. It specifies the location of all the content of the book, from its text to other media such as images. It also points to another metadata file, the Navigation Center eXtended (NCX) table of contents.

The OPF file is the most complex metadata in the EPUB specification. Create OEBPS/content.opf, and paste the contents of Listing 3 into it.

<?xml version='1.0' encoding='utf-8'?>
<package xmlns="http://www.idpf.org/2007/opf"
         xmlns:dc="http://purl.org/dc/elements/1.1/"
         unique-identifier="bookid" version="2.0">
  <metadata>
    <dc:title>Hello World: My First EPUB</dc:title>
    <dc:creator>My Name</dc:creator>
    <dc:identifier id="bookid">urn:uuid:0cc33cbd-94e2-49c1-909a-72ae16bc2658</dc:identifier>
    <dc:language>en-US</dc:language>
    <meta name="cover" content="cover-image" />
  </metadata>
  <manifest>
    <item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml"/>
    <item id="cover" href="title.html" media-type="application/xhtml+xml"/>
    <item id="content" href="content.html" media-type="application/xhtml+xml"/>
    <item id="cover-image" href="images/cover.png" media-type="image/png"/>
    <item id="css" href="stylesheet.css" media-type="text/css"/>
  </manifest>
  <spine toc="ncx">
    <itemref idref="cover" linear="no"/>
    <itemref idref="content"/>
  </spine>
  <guide>
    <reference href="title.html" type="cover" title="Cover"/>
  </guide>
</package>

Other sample

<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="uid">
   <metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
      <dc:identifier id="uid">code.google.com.epub-samples.epub30-spec</dc:identifier>
      <dc:title>EPUB 3.0 Specification</dc:title>
      <dc:creator>EPUB 3 Working Group</dc:creator>
      <dc:language>en</dc:language>
      <meta property="dcterms:modified">2012-02-27T16:38:35Z</meta>
   </metadata>
   <manifest>
      <item href="xhtml/epub30-titlepage.xhtml" id="ttl" media-type="application/xhtml+xml"/>
      <item href="xhtml/epub30-nav.xhtml" id="nav" media-type="application/xhtml+xml" properties="nav"/>
      <item href="xhtml/epub30-terminology.xhtml" id="term" media-type="application/xhtml+xml"/>
      <item href="xhtml/epub30-overview.xhtml" id="ovw" media-type="application/xhtml+xml"/>
      <item href="xhtml/epub30-publications.xhtml" id="pub" media-type="application/xhtml+xml"/>
      <item href="xhtml/epub30-contentdocs.xhtml" id="cd" media-type="application/xhtml+xml"/>
      <item href="xhtml/epub30-mediaoverlays.xhtml" id="mo" media-type="application/xhtml+xml"/>
      <item href="xhtml/epub30-ocf.xhtml" id="ocf" media-type="application/xhtml+xml"/>      
      <item href="xhtml/epub30-acknowledgements.xhtml" id="ack" media-type="application/xhtml+xml"/>
      <item href="xhtml/epub30-references.xhtml" id="ref" media-type="application/xhtml+xml"/>
      <item href="xhtml/epub30-changes.xhtml" id="cha" media-type="application/xhtml+xml"/>
      <item href="css/epub-spec.css" media-type="text/css" id="css"/>
      <item href="img/idpflogo_web_125.jpg" media-type="image/jpeg" id="logo"/>
      <item href="img/epub_logo_color.jpg" media-type="image/jpeg" id="ci" properties="cover-image"/>
   </manifest>
   <spine>
      <itemref idref="ttl"/>
      <itemref idref="nav" linear="no"/>
      <itemref idref="term" linear="no"/>
      <itemref idref="ovw"/>
      <itemref idref="pub"/>
      <itemref idref="cd"/>
      <itemref idref="mo"/>
      <itemref idref="ocf"/>
      <itemref idref="ack"/>
      <itemref idref="ref"/>
      <itemref idref="cha"/>
   </spine>
</package>

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment