EPUB是目前最廣泛使用的電子書格式,即便是有自己格式的kindle目前也支援epub。而且說實在,epub嚴格上來說,是個非常簡單的檔案組合格式,他基本上就是zip檔,再加上各種符合xhtml的文字檔案,再加上固定的目錄,全部zip變成一個檔案。實務上來說他甚至比大家更常用的.doc (word檔案)簡單很多很多。
如果你有mac要手搓epub檔案,相當簡單,就是用純文字編輯器,加上固定的目錄,加上zip指令,把所有相關的東西全部zip打包在一起就好了。
所有相關規範都是透明的,可以參考https://www.w3.org/publishing/epub3/ (或者叫你的AI agent去讀這個規範做出電子書:)
假設你要做一本電子書內容如下:
* 書名 "This is my first epub"* 內容:"Hello world, this is my first epub book. It is very easy to build."
* 有一個封面圖片、有個簡單的導覽目錄(Table of Contents)
第一步是要先準備好檔案目錄結構,最簡單的目錄結構如下圖:
具體的說 你先建立目錄my-first-epub 然後在這個目錄下面建立 兩個目錄:META-INF 以及 EPUB(這下面還有其他目錄),然後還有一個純文字檔案mimetype。
可以直接參考這些建立目錄的指令
mkdir -p ~/my-first-epub/META-INFmkdir -p ~/my-first-epub/EPUB/css
mkdir -p ~/my-first-epub/EPUB/images
mkdir -p ~/my-first-epub/EPUB/text
第二步: mimetype檔案。
mimetype檔案是用來告知電子書閱讀器該如何處理此檔案。
關鍵在於,內容必須嚴格為 `application/epub+zip`,不可有任何結尾換行符號或空格,且zip打包成 zip 時絕對不能壓縮。
在shell裡面執行這個指令 確保沒有換行符號跟其他內容
echo -n "application/epub+zip" > mimetype
第三步:建立 `META-INF/container.xml`
<?xml version="1.0" encoding="UTF-8"?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="EPUB/package.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
第四步:建立 CSS 樣式表 (`EPUB/css/style.css`)
建立 CSS 檔案來設定標題、內文與封面圖片的樣式: 用文字編輯器 編輯EPUB/css/style.css 內容如下:(如果你對html有了解 哪這個跟html/css是一樣的)
body {
font-family: serif;
margin: 5%;
line-height: 1.6;
color: #222222;
}
h1 {
text-align: center;
color: #111111;
margin-top: 2em;
margin-bottom: 1em;
}
p {
text-indent: 1.5em;
margin-bottom: 1em;
}
.cover-container {
text-align: center;
margin-top: 10%;
}
.cover-image {
max-width: 100%;
height: auto;
}
第五步:新增封面圖片 (`EPUB/images/cover.jpg`)
這步驟就是你隨便(AI)產生一個cover.jpg 放在這個目錄固定地方即可
這步驟就是你隨便(AI)產生一個cover.jpg 放在這個目錄固定地方即可
第六步:建立內容頁面 (XHTML)
所有實際書籍內容檔案,基本上就是 XHTML5 格式
(1) 用文字編輯器編輯 EPUB/text/cover.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta charset="UTF-8"/>
<title>Cover</title>
<link rel="stylesheet" type="text/css" href="../css/style.css"/>
</head>
<body>
<div class="cover-container">
<img class="cover-image" src="../images/cover.jpg" alt="Book Cover"/>
</div>
</body>
</html>
(2) 用文字編輯器編輯 EPUB/text/ch01.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta charset="UTF-8"/>
<title>This is my first epub</title>
<link rel="stylesheet" type="text/css" href="../css/style.css"/>
</head>
<body>
<h1>This is my first epub</h1>
<p>Hello world, this is my first epub book. It is very easy to build.</p>
</body>
</html>
其實到這裡你應該也非常容易理解,這些根本就都是網頁/html啊
第七步:建立導覽文件 (`EPUB/nav.xhtml`)
這是和普通網站略有不同的地方epub規定要有導覽文件 這個檔案是必要的 大部分的時候這檔案會涵蓋所有的章節目錄
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="en" lang="en">
<head>
<meta charset="UTF-8"/>
<title>Table of Contents</title>
<link rel="stylesheet" type="text/css" href="css/style.css"/>
</head>
<body>
<nav epub:type="toc" id="toc">
<h1>Table of Contents</h1>
<ol>
<li><a href="text/cover.xhtml">Cover</a></li>
<li><a href="text/ch01.xhtml">This is my first epub</a></li>
</ol>
</nav>
</body>
</html>
第八步:建立封裝文件 (`EPUB/package.opf`) 這個檔案是用來指定如何封裝所有東西 更重要的是他可以指定閱讀順序!閱讀順序跟檔案內容順序是可以不一樣的
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" unique-identifier="pub-id">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:identifier id="pub-id">urn:uuid:12345678-1234-5678-1234-567812345678</dc:identifier>
<dc:title>This is my first epub</dc:title>
<dc:language>en</dc:language>
<meta property="dcterms:modified">2026-07-27T00:00:00Z</meta>
</metadata>
<manifest>
<item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/>
<item id="cover-image" href="images/cover.jpg" media-type="image/jpeg" properties="cover-image"/>
<item id="cover-page" href="text/cover.xhtml" media-type="application/xhtml+xml"/>
<item id="ch01" href="text/ch01.xhtml" media-type="application/xhtml+xml"/>
<item id="style" href="css/style.css" media-type="text/css"/>
</manifest>
<spine>
<itemref idref="cover-page"/>
<itemref idref="ch01"/>
</spine>
</package>
第九步: 使用zip工具打包成 `.epub` 這裡的zip範例是用mac的zip
根據 EPUB 官方規範:
1. `mimetype` 必須是 ZIP 壓縮包中的第一個檔案,且必須不加壓縮(`-0`)且不含額外屬性(`-X`)。
2. 其餘所有檔案則透過遞迴方式壓縮加入。
1. 以不壓縮方式新增 mimetype 檔案
#zip -X0 my_first_book.epub mimetype
2. 以壓縮方式新增 META-INF 與 EPUB 資料夾
#zip -rDX9 my_first_book.epub META-INF EPUB
