ofd_parser_dev.js
Version:
145 lines (102 loc) • 3.38 kB
Markdown
# ofd.js
 [](./LICENSE)
目前方案采用wasm实现ofd解析、渲染、验签,不支持IE
examples已增加原生js、reactjs的demo
效果:

[体验地址](https://51shouzu.xyz/ofd/)
## Usage with npm
```
npm i ofd.js
```
## Usage with CDN
目前可以通过 [unpkg.com/ofd.js](https://unpkg.com/ofd.js) 获取到最新版本的资源,在页面上引入 js 文件即可开始使用。
```
html引用js文件
<script type="text/javascript" src="./OfdParser.js"></script>
<script type="text/javascript" src="./FreetypeKit.js"></script>
<script src="https://unpkg.com/ofd.js"></script>
<script type="text/javascript">
ofd.onWebassemblyRuntimeInitialized(()=>{
console.log(ofd)
})
</script>
```
## 相关接口说明
#### webassembly加载回调
```
onWebassemblyRuntimeInitialized(()=>{
//do something
})
```
#### 解析OFD文件
```
ofdFile为OFD文件。支持File或者ArrayBuffer
parseOfdDocument({
ofd: ofdFile,
success(res) {
console.log(res)
}
})
```
#### 获取OFD文档页数
```
ofdData为解析ofd文件接口返回的OFD对象
documentIndex为ofd文档中document的索引,默认从0开始
getOFDPageCount(ofdData, documentIndex)
```
#### 获取OFD文档对应页的页宽
```
ofdData为解析ofd文件接口返回的OFD对象
documentIndex为ofd文档中document的索引,默认从0开始
pageIndex为ofd文档中页码,默认从0开始
pageWidth(ofdData, documentIndex, pageIndex)
```
#### 获取OFD文档对应页的页高
```
ofdData为解析ofd文件接口返回的OFD对象
documentIndex为ofd文档中document的索引,默认从0开始
pageIndex为ofd文档中页码,默认从0开始
pageHeight(ofdData, documentIndex, pageIndex)
```
#### 设置渲染的DPI
```
dpi---对应的dpi值,默认是96
setPageDPI(dpi)
```
#### 一次性渲染OFD对应文档的所有页,适合页数少
```
ofdData为解析ofd文件接口返回的OFD对象
documentIndex为ofd文档中document的索引,默认从0开始
renderOfd(ofdData, documentIndex)
```
#### 渲染OFD对应文档的对应页
```
pageDiv为html中div容器
ofdData为解析ofd文件接口返回的OFD对象
documentIndex为ofd文档中document的索引,默认从0开始
pageIndex为ofd文档中页码,默认从0开始
renderOfdByIndex(pageDiv, ofdData, documentIndex, pageIndex)
```
#### 打开内置ofdview。适合打开大于100页的文档,具体看examples中的html例子
html需要添加一个id为OfdView的div容器,其中overflow必须设置为auto,height必须有值,否则不会自动渲染后面的页面
```
<div id="OfdView" style="overflow:auto;height:1000px"></div>
```
js方法
```
ofdFile为OFD文件。支持File或者ArrayBuffer
openOfdView(ofdFile)
```
### 获取帮助
如需获取帮助可以扫码加微信或者知识星球


### 微信小程序体验OFD

## ofd推荐项目
[OFD Reader & Writer](https://github.com/Trisia/ofdrw)
[ofd.android](https://github.com/DLTech21/ofd.android)
### 项目关注度
> 项目获得 Star曲线
[](https://starchart.cc/DLTech21/ofd.js)