How to Create an EPUB File From HTML and XML

Create an EPUB file from HTML and XML in a few easy steps

What to Know

  • Build HTML > create a MIME file > cover image > title page and table of contents > container XML file > contents list.
  • Test your book to check for issues.

This article explains how to create an EPUB file from HTML and XML.

Woman Reading Tablet
Photo © Letizia Le Fur / Getty Images

How to Create an EPUB File From HTML and XML

An EPUB file is the other type of ebook file that is popular. If you are planning on writing or publishing an ebook, you should save your HTML as a Mobipocket file, and also as an EPUB. In some ways, an epub file is a lot easier to build than a Mobi file. Since EPUB is based on XML, you simply need to create your XML files, collect them together, and call it an epub.

These are the steps you should take to create an epub file:

  1. Build your HTML. Your book is written in HTML, with CSS for styling. But, it's not just HTML, it's XHTML. So, if you don't normally write in XHTML (closing your elements, using quotes around all attributes, and so on) you will need to convert your HTML to XHTML. You can use one or more XHTML files for your books. Most people separate the chapters into separate XHTML files. Once you have all the XHTML files, put them in a folder all together.
  2. Create a MIME Type File. In your text editor, open a new document and type:
    application/epub+zip 
    Save the file as “mimetype” without any extension. Place that file in the folder with your XHTML files.
  3. Add your style sheets. You should create two style sheets for your book one for the pages called
    page_styles.css
    :
    @page {
  4. margin-bottom: 5pt;
  5. margin-top: 5pt
  6. }
  7. Create one for the book styles called
    stylesheet.css
    . You can give them other names, you'll just need to remember what they are. Save these files in the same directory with your XHTML and mimetype files.
  8. Add your cover image. Your cover image should be a JPG file no more than 64KB. The smaller you can make it the better, but keep it good looking. Small images can be very hard to read, and the cover is where you do your marketing of your book.
  9. Build your title page. You don't have to use the cover image as your title page, but most people do. To add your title page, create an XHTML file called
    titlepage.xhtml
    Here is an example of a title page using SVG for the image. Change the highlighted part to point to your cover image:
  10. Cover
  11. Build your "Table of Contents." Create a file called
    toc.ncx
    in your text editor. This is an XML file, and it should point to all your HTML files in your book. Here is a sample with two elements in the table of contents. Change the highlighted parts to your book, and add additional
    navPoint
    elements for additional sections:
  12. How to Build a Website
  13. Hosting
  14. Do You Need a Domain Name?
  15. Add a container XML file. In your text editor, create a file called
    container.xml
    and save it in a sub-directory below your HTML files. The file should read:
  16. Create the contents list (
    content.opf
    ).
    This is the file that explains what your epub book is. It includes metadata about the book (like the author, publish date, and genre). Here is a sample, you should change the parts in yellow to reflect your book:
  17. en
  18. How to Build a Website
  19. Jennifer Kyrnin
  20. 0101-01-01T00:00:00+00:00
  21. 0c159d12-f5fe-4323-8194-f5c652b89f5c
  22. That's all the files you need, they should all be in a directory together (except for
    container.xml
    , which goes in a sub-directory
    META-INF
    ). We like to then go to the container directory and make sure it has a name that reflects the title and author names.
  23. Once you have the directory of files named how you want it you should use a zip file archive program to zip the directory. My sample directory ends up as a zip file named “How to Build a Website — Jennifer Kyrnin.zip”
  24. Finally, change the file name extension from
    .zip
    to
    .epub
    . Your operating system may protest, but go ahead with it. You want this to have an epub extension.
  25. Lastly, test your book. It's hard to get the epub format correct on the first try, so you should always test your file. Open it in an epub reader like Calibre. And if it doesn't display correctly, you can use Calibre to correct problems.
Format
mla apa chicago
Your Citation
Kyrnin, Jennifer. "How to Create an EPUB File From HTML and XML." ThoughtCo, Sep. 30, 2021, thoughtco.com/create-epub-file-from-html-and-xml-3467282. Kyrnin, Jennifer. (2021, September 30). How to Create an EPUB File From HTML and XML. Retrieved from https://www.thoughtco.com/create-epub-file-from-html-and-xml-3467282 Kyrnin, Jennifer. "How to Create an EPUB File From HTML and XML." ThoughtCo. https://www.thoughtco.com/create-epub-file-from-html-and-xml-3467282 (accessed March 29, 2024).