vue前端利用ofd.js实现ofd类型在线预览

1.安装:

npm i ofd.js

2.axios封装注意事项,responseType: 'arraybuffer'

//电子证件照
export function getXkzOriginal(data) {
    return req({
        url: '/bigdata/getXkzOriginal',
        method: 'POST',
        params: data,
        responseType: 'arraybuffer'
    })
}
3.vue组件使用
import {parseOfdDocument, renderOfd} from "ofd.js"

getXkzOriginal({ dataId: this.dataId })
                .then((res) => {
                    let data = res.data
                    let file = new File([data], 'demo.ofd') 
                
                    if (!!data && res.status === 200) {
                        // console.log(file.size)
                        if (file && file.size > 0) {
                            parseOfdDocument({
                                ofd: file ,
                                secret: 'kgNVVbdUZ31C6mps',
                                signaturesCallback(signatures) {
                                    // console.log(signatures)
                                    let screenWidth = 1050 //设置内容的显示框大小
                                    //将流数据渲染给接受的div盒子
                                    renderOfd(0, screenWidth).then((res) => {
                                        const divs = res
                                        let contentDiv = document.getElementById('divId') // 获取盒子元素
                                        contentDiv.innerHTML = ''
                                        for (const div of divs) {
                                            contentDiv.appendChild(div)
                                        }
                                    })
                                },
                                success(core) {
                                    console.log(core)
                                },
                                fail(error) {
                                    console.log(error)
                                },
                            })
                            this.pic = true
                            this.spinning = false
 
})
}
### 如何在 Vue 3.0 中集成和使用 ofd.js 进行 OFD 文件处理 #### 安装依赖 为了能够在 Vue 3.0 项目中使用 `ofd.js` 处理 OFD 文件,首先需要安装相应的 npm 包。考虑到不同版本对于特定功能的支持情况,建议指定安装较低版本以确保兼容性和特性支持。 ```bash npm install ofd.js@0.2.5 ``` 此命令将安装 `ofd.js` 的 0.2.5 版本[^2]。 #### 创建 OfdViewer 组件 创建一个新的 Vue 单文件组件用于展示 OFD 文档的内容: ```vue <template> <div ref="ofdContainer"></div> </template> <script setup> import { onMounted, ref } from 'vue'; import * as OFDJS from 'ofd.js'; const props = defineProps({ fileContent: { type: String, required: true } }); const ofdContainer = ref(null); onMounted(() => { const containerElement = ofdContainer.value; // 将 base64 编码的数据转换成 ArrayBuffer 对象 fetch(`data:application/octet-stream;base64,${props.fileContent}`) .then(response => response.arrayBuffer()) .then(buffer => new Uint8Array(buffer)) .then(arrayBuffer => { // 初始化并加载 OFD 解析器实例 let parser = new OFDJS.Parser(); // 加载 OFD 数据包 parser.loadPackage(arrayBuffer); // 渲染 OFD 页面至容器内 parser.render(containerElement); }); }); </script> <style scoped> /* 添加样式调整 */ .ofd-container { width: 100%; height: 100vh; /* 或者根据实际需求设置高度 */ } </style> ``` 上述代码片段展示了如何定义一个名为 `OfdViewer.vue` 的自定义组件来接收 Base64 编码形式的 OFD 文件内容作为属性,并将其解码后传递给 `ofd.js` 库进行渲染。 请注意,这里假设传入的是已经编码好的 OFD 流字符串;如果实际情况是从服务器获取二进制数据,则可能需要适当修改这部分逻辑以便正确读取响应体中的原始字节序列。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值