Rust library for creating (sync/async) EPUB files
This crate provides a high-level, ergonomic API for creating EPUB files (2.0.1). It offers both asynchronous and blocking (synchronous) implementations, with flexible builders and output options.
Add this crate to your Cargo.toml:
[dependencies]
liber = "0.1.0"[dependencies]
liber = { version = "0.1.0", features = ["async"] }use liber::epub::{
ContentBuilder, ContentReference, EpubBuilder, ImageType, MetadataBuilder, ReferenceType,
Resource,
};
use std::path::Path;
fn main() {
match create() {
Err(e) => eprintln!("{e}"),
Ok(_) => println!("ok"),
}
}
fn create() -> Result<(), Box<dyn std::error::Error>> {
let mut file = std::fs::File::create("book.epub")?;
let title = "My Book";
let contents = vec![
ContentBuilder::new(
r#"<body><h1>Chapter 2</h1></body>"#.as_bytes(),
ReferenceType::Text("Chapter 2".to_string()),
)
.build(),
ContentBuilder::new(
r#"<body><h1>Chapter 3</h1></body>"#.as_bytes(),
ReferenceType::Text("Chapter 3".to_string()),
)
.add_child(
ContentBuilder::new(
r#"<body><h1>Chapter 4</h1></body>"#.as_bytes(),
ReferenceType::TitlePage("Chapter 4".to_string()),
)
.build(),
)
.build(),
];
let epub_builder = EpubBuilder::new(MetadataBuilder::title(title).creator("author").build())
.stylesheet("body {}".as_bytes())
.cover_image(Path::new("/path/to/img.jpg"), ImageType::Jpg)
.add_resource(Resource::Font(Path::new("/path/to/some_font.otf")))
.add_content(
ContentBuilder::new(
r#"<body><h1>Chapter 1</h1><h2 id="id01">Section 1.1</h2><h2 id="id02">Section 1.1.1</h2><h2 id="id03">Section 1.2</h2></body>"#.as_bytes(),
ReferenceType::Text("Chapter 1".to_string()),
)
.add_content_reference(ContentReference::new("Section 1.1").add_child(ContentReference::new("Section 1.1.1")))
.add_content_reference(ContentReference::new("Section 1.2"))
.add_children(contents)
.build(),
);
epub_builder.create(&mut file)?;
Ok(())
}- Here are more examples
- Every content is a xhtml. Only the body needs to be added as a content (see examples)
- Content (Ex: Chapter) and ContentReference (Ex: Chapter#ref1) could be named with filename and id methods respectively. If none is set, Content will be sequencial cNN.xhtml (c01.xhtml, c02.xhtml...) and ContentReferences will be idNN (id01, id02...) corresponding to the Content.
- Default blocking creation. Async available too (using tokio and async_zip crates)
- Multi section creation (contents, subcontents, references and subreferences)
- Supporting file content and raw content (bytes) creation
Find all the configuration options in the full documentation.