Writer/Reader for Mobi format.
Note: All testing were done on Kindle Previewer
(Windows) and Kindle Paperwhite (6th Gen)
- This is more or less WIP. Use at your own risk.
- This package was written for a specific task, thus there are certain limitations, such as:
img
tags are ignored and not embedded.- TOC depth does not go beyond 1. Meaning for now you can only have chapters and sub-chapters. But sub-chaper can not have it's own sub-chapters.
- HTML formatting is supported, but rendering is dependant on your eBook reader. (For Kindle see Supported HTML Tags in Book Content)
- Cover images should be in JPG (I have not tested GIF, which sould be supported).
- IMPORTANT: Images resized using
image/jpeg
package will not display (in Kindle) because JFIF APP0 marker segment is not generated byimage/jpeg
package.
- IMPORTANT: Images resized using
- Table of Content is automaticaly generated.
- Can only read metadata; content is not accessible.
w, err := mobi.NewWriter("/tmp/example.mobi")
if err != nil {
panic(err)
}
defer w.Close()
w.Title("Book Title")
w.Compression(mobi.CompressionNone) // LZ77 compression is also possible using mobi.CompressionPalmDoc
// Meta data
w.NewExthRecord(mobi.EXTH_DOCTYPE, "EBOK")
w.NewExthRecord(mobi.EXTH_AUTHOR, "Book Author Name")
// See exth.go for additional EXTH record IDs
// Add chapters and subchapters
ch1 := w.NewChapter("Chapter 1", []byte("Some text here"))
ch1.AddSubChapter("Chapter 1-1", []byte("Some text here"))
ch1.AddSubChapter("Chapter 1-2", []byte("Some text here"))
w.NewChapter("Chapter 2", []byte("Some text here")).AddSubChapter("Chapter 2-1", []byte("Some text here")).AddSubChapter("Chapter 2-2", []byte("Some text here"))
w.NewChapter("Chapter 3", []byte("Some text here")).AddSubChapter("Chapter 3-1", []byte("Some text here"))
w.NewChapter("Chapter 4", []byte("Some text here")).AddSubChapter("Chapter 4-1", []byte("Some text here"))
// Output MOBI File
w.Write()
r, err := mobi.NewReader("/tmp/example.mobi")
if err != nil {
panic(err)
}
defer r.Close()
fmt.Printf("Document type: %s\n", r.DocType())
fmt.Printf("Author(s): %s\n", strings.Join(r.Authors(), ", "))
fmt.Printf("Title: %s\n", r.BestTitle())
// See reader.go for additional record types
if r.HasCover() {
img, _ := r.Cover()
f, _ := os.Create("/tmp/mobi_example.jpg")
jpeg.Encode(f, img, nil)
f.Close()
}