| Crates.io | iepub |
| lib.rs | iepub |
| version | 1.1.2 |
| created_at | 2024-07-18 02:56:55.117217+00 |
| updated_at | 2025-09-11 02:19:37.015017+00 |
| description | epub、mobi电子书读写 |
| homepage | |
| repository | https://github.com/inkroom/iepub |
| max_upload_size | |
| id | 1306866 |
| size | 2,390,982 |
支持从文件和内存读取和生成EPUB电子书
EpubBook结构体手动生成epubEpubBuilder快速生成use iepub::EpubHtml;
use iepub::EpubBuilder;
EpubBuilder::default()
.with_title("书名")
.with_creator("作者")
.with_date("2024-03-14")
.with_description("一本好书")
.with_identifier("isbn")
.with_publisher("行星出版社")
.add_chapter(
EpubHtml::default()
.with_file_name("0.xml")
.with_data("<p>锻炼</p>".to_string().as_bytes().to_vec()),
)
.add_assets("1.css", "p{color:red}".to_string().as_bytes().to_vec())
.metadata("s", "d")
.metadata("h", "m")
.file("target/build.epub")
.unwrap();
use iepub::prelude::{reader::read_from_vec, reader::read_from_file, EpubHtml};
let mut data = Vec::new();// epub的二进制数据
let mut book = read_from_vec(data);
// 从文件读取
let mut bbook = read_from_file("epub格式文件绝对路径");
iepub使用EpubHtml来存储章节内容,但是EpubHtml#data实际只会存储 html>body 节点内的内容,并且不包括body节点的属性(attribute),其他比如样式表将会存放在其他属性中
不同的阅读器对于文件名的兼容性不同,这里建议文件最好使用.xhtml后缀,例如EpubHtml::default().with_file_name("1.xhtml")
custome_nav(true),然后调用add_nav()添加目录自动生成黑底白字,写着书籍名的封面图
需要启用feature cover,然后调用auto_gen_cover(true),同时需要调用with_font(font)设置字体文件位置。
use iepub::prelude::*;
let path = std::env::current_dir().unwrap().join("1.mobi");
let mut mobi = MobiReader::new(std::fs::File::open(path.to_str().unwrap()).unwrap()).unwrap();
let book = mobi.load().unwrap();
使用builder
let v = MobiBuilder::default()
.with_title("书名")
.with_creator("作者")
.with_date("2024-03-14")
.with_description("一本好书")
.with_identifier("isbn")
.with_publisher("行星出版社")
.append_title(true)
.custome_nav(true)
.add_chapter(MobiHtml::new(1).with_title("标题").with_data("<p>锻炼</p>".as_bytes().to_vec()))
// .file("builder.mobi")
.mem()
.unwrap();
custome_nav(true),然后调用add_nav()添加目录MobiNav#set_chap_id()指明指向的章节;如果是类似卷首目录,指向最接近的章节即可mobi格式中图片是不存在文件路径的,如果需要添加图片,首先在章节中使用 img 标签的src属性,随便给个文件名,只要不重复就行,然后添加图片的时候也指向同一个文件名,最后写入就会添加图片了
由于mobi设计原因,如果某个图片未被引用,最终仍然会被写入到文件,但是不可索引,不可查看,只能白白占用空间
另外封面需要调用cover()设置
默认情况下会在章节的html片段前面加一段标题xml,如果章节内容里本身就有可阅读的标题,设置append_title(false)
自动生成黑底白字,写着书籍名的封面图
需要启用feature cover,然后调用auto_gen_cover(true),同时需要调用with_font(font)设置字体文件位置。
use iepub::prelude::*;
let mut book = std::fs::File::open(std::path::PathBuf::from("example.mobi"))
.map_err(|e| IError::Io(e))
.and_then(|f| MobiReader::new(f))
.and_then(|mut f| f.load())
.unwrap();
let mut epub = mobi_to_epub(&mut book).unwrap();
epub.write("convert.epub").unwrap();
use iepub::prelude::*;
let mut epub = EpubBook::default();
let mobi = epub_to_mobi(&mut epub).unwrap();
let mut v = std::io::Cursor::new(Vec::new());
MobiWriter::new(&mut v)
.unwrap()
.with_append_title(false)
.write(&mobi)
.unwrap();
lib/src/cli目录为命令行工具,支持mobi和epub格式,但是不同格式支持的命令不尽相同
目前支持
可通过-h获取使用方法说明
可以使用cargo install iepub安装
启用cache feature可以缓存到文件,适用于爬虫场景重试