A Rust library for generating EPUB files

20 releases

0.5.0 Jan 26, 2022
0.4.8 Sep 29, 2020
0.4.7 Jun 21, 2020
0.4.5 Feb 11, 2020
0.2.2 Mar 20, 2017

#315 in Text processing

Download history 231/week @ 2022-12-07 117/week @ 2022-12-14 144/week @ 2022-12-21 161/week @ 2022-12-28 68/week @ 2023-01-04 111/week @ 2023-01-11 103/week @ 2023-01-18 122/week @ 2023-01-25 198/week @ 2023-02-01 94/week @ 2023-02-08 183/week @ 2023-02-15 107/week @ 2023-02-22 84/week @ 2023-03-01 132/week @ 2023-03-08 182/week @ 2023-03-15 91/week @ 2023-03-22

499 downloads per month
Used in 6 crates (5 directly)

MPL-2.0 license


See the full library documentation on Docs.rs.

A library to generate EPUB files.

The purpose for this library is to make it easier to generate EPUB files: it should take care of most of the boilerplate for you, leaving you only with the task of filling the actual content.


Add this in your Cargo.toml file:

epub-builder = "0.4"


extern crate epub_builder;

use epub_builder::EpubBuilder;
use epub_builder::Result;
use epub_builder::ZipLibrary;
use epub_builder::EpubContent;
use epub_builder::ReferenceType;
use epub_builder::TocElement;

use std::io;
use std::io::Write;

// Try to print Zip file to stdout
fn run() -> Result<()> {
    // Some dummy content to fill our books
    let dummy_content = r#"<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops">
<p>Text of the page</p>
    let dummy_image = "Not really a PNG image";
    let dummy_css = "body { background-color: pink }";

    // Create a new EpubBuilder using the zip library
    // Set some metadata
        .metadata("author", "Joan Doe")?
        .metadata("title", "Dummy Book")?
    // Set the stylesheet (create a "stylesheet.css" file in EPUB that is used by some generated files)
    // Add a image cover file
        .add_cover_image("cover.png", dummy_image.as_bytes(), "image/png")?
    // Add a resource that is not part of the linear document structure
        .add_resource("some_image.png", dummy_image.as_bytes(), "image/png")?
    // Add a cover page
        .add_content(EpubContent::new("cover.xhtml", dummy_content.as_bytes())
    // Add a title page
        .add_content(EpubContent::new("title.xhtml", dummy_content.as_bytes())
    // Add a chapter, mark it as beginning of the "real content"
        .add_content(EpubContent::new("chapter_1.xhtml", dummy_content.as_bytes())
                     .title("Chapter 1")
    // Add a second chapter; this one has more toc information about its internal structure
        .add_content(EpubContent::new("chapter_2.xhtml", dummy_content.as_bytes())
                     .title("Chapter 2")
                     .child(TocElement::new("chapter_2.xhtml#1", "Chapter 2, section 1")))?
    // Add a section. Since its level is set to 2, it will be attached to the previous chapter.
        .add_content(EpubContent::new("section.xhtml", dummy_content.as_bytes())
                     .title("Chapter 2, section 2")
    // Add a chapter without a title, which will thus not appear in the TOC.
        .add_content(EpubContent::new("notes.xhtml", dummy_content.as_bytes()))?
    // Generate a toc inside of the document, that will be part of the linear structure.
    // Finally, write the EPUB file to stdout
        .generate(&mut io::stdout())?;

fn main() {
    match run() {
        Ok(_) => {
            writeln!(&mut io::stderr(),
                     "Successfully wrote epub document to stdout!")
        Err(err) => writeln!(&mut io::stderr(), "Error: {}", err).unwrap(),


epub-builder's aim is to make EPUB generation simpler. It takes care of zipping the files and generate the following ones:

  • mimetype
  • toc.ncx
  • nav.xhtml
  • manifest.xml
  • content.opf
  • com.apple.ibooks.display-options.xml.

It also tries to make it easier to have a correct table of contents, and optionally generate an inline one in the document.

Supported EPUB versions:

  • 2.0.1 (default)
  • 3.0.1

Missing features

There are various EPUB features that epub-builder doesn't handle. Particularly, there are some metadata that could be better handled (e.g. support multiple authors, multiple languages in the document and so on).

There are also various things that aren't in the scope of this library: it doesn't provide a default CSS, templates for your XHTML content and so on. This is left to libraries or applications using it.

Conditional compilation

EPUB files are Zip files, so we need to zip. By default, this library provides wrappers around both the Rust zip library and calls to the zip command that may (or may not) be installed on your system.

It is possible to disable the compilation (and the dependencies) of either of these wrappers, using no-default-features. (If you don't enable at least one of them this library will be pretty useless).


This is free software, published under the Mozilla Public License, version 2.0.


See the ChangeLog file.


~163K SLoC