epublib
Introduction
Epublib is an ETS library used to read, write, or operate EPUB files.
Effect

How to Install
ohpm install @ohos/epublib
For details about the OpenHarmony ohpm environment configuration, see OpenHarmony HAR.
How to Use
Sample Code
- Import GlobalContext to entryAbility.
import { GlobalContext } from '@ohos/epublib'
onWindowStageCreate(windowStage: window.WindowStage) {
// The main window is created. Set a main page for this ability.
GlobalContext.getContext().setValue('filePath',this.context.filesDir)
}
- Import epublib to the page.
import {DOMParser,EpubReader,EpubWriter, Book,Author,EpubResource,MediaType,Metadata,MediatypeService} from "@ohos/epublib"
- Use the project.
funcStart(input: string) {
console.log("-----funcStart----filePath-----------------" + input)
let result = EpubReader.readEpub(input);
if (result != undefined) {
result.then((book) => {
this.text = book.getResources()
.getResourceMap()
.get("chapter_446465249.xhtml")
.getStrData()
.toString();
console.error("----index-result-------" + this.text)
this.testEpubWriter(book)
});
}
}
Available APIs
- Obtains a book instance.
readEpub(inPath:string, encoding?:string, lazyLoadedTypes?: Array<MediaType>):Book - Reads the eBook through resources.
readEpubToBook(resources:Resources, result?:Book):Book - Reads and parses the EPUB ebook from EPUB files in a lazy manner.
readEpubLazy(inPath: string, encoding?: string, lazyLoadedTypes?: Array<MediaType>): Book - Obtains the collection of all images, chapters, sections, XHTML files, and style sheets of the book.
getResources(): Resources - Obtains the book content.
getStrData() - Writes the book content.
write(book: Book, fileName: string) - Obtains the content sequence of the EPUB eBook.
getSpine() - Obtains the metadata of the EPUB eBook.
getMetadata() - Adds a resource to the EPUB eBook.
addResource(resource: EpubResource): EpubResource - Obtains the location of the EPUB file in the folder.
getHref() - Loads resources through compressed files.
loadResources(ZipFile zipFile, String defaultHtmlEncoding, List<MediaType> lazyLoadedTypes) - Captures the output directory.
outFile(inZipPath: string): string - Sets the author.
addAuthor(author: Author) - Adds the title.
addTitle(title: string): string - Sets the language.
setLanguage(language: string)
About obfuscation
- Code obfuscation, please seeCode Obfuscation
- If you want the library not to be obfuscated during code obfuscation, you need to add corresponding exclusion rules in the obfuscation rule configuration file obfuscation-rules.txt:
-keep
./oh_modules/@ohos/epublib
Constraints
This project has been verified in the following version:
-
DevEco Studio: 5.0.2 Beta1 5.0.7.41, SDK 5.0.2.120(API14)
-
DevEco Studio: NEXT Beta1-5.0.3.806, SDK: API12 Release(5.0.0.66)
-
DevEco Studio: 4.1 Canary (4.1.3.521), OpenHarmony SDK: API 11 (4.1.0.65)
Directory Structure
|---- epublib
| |---- entry # Sample code
| |---- epublib # eBook library
| |---- index.ets External APIs
| |---- src
| |---- main
| |---- ets
| |---- components
| |---- domain # Book data models
| |---- epub # Parsing and processing
| |---- service # Supported media types
| |---- util # Common method library
| |---- Constants.ets # Constant definition
| |---- README.md # Readme
How to Contribute
If you find any problem when using the project, submit an issue or a PR.
License
This project is licensed under LGPL License 3.0.