前端OFD文件预览(vue案例cafe-ofd)

0、提示

下面只有vue的使用示例demo ,官文档参考 cafe-ofd - npm

其他平台可以参考 ofd - npm

官方线上demo: ofd

1、安装包

bash 复制代码
npm install cafe-ofd --save

2、引入

html 复制代码
import cafeOfd from 'cafe-ofd'
import 'cafe-ofd/package/index.css'
Vue.use(cafeOfd)

3、使用案例(借助了element的组件可以替换其他)

html 复制代码
<template>

  <div style="padding: 20px 100px; font-size: 20px">
    <h1>ofd文件预览</h1>
    <h1>https://www.npmjs.com/package/cafe-ofd</h1>
    <h1>npm install cafe-ofd --save</h1>
    <h1>
      import cafeOfd from 'cafe-ofd' <br>
      import 'cafe-ofd/package/index.css'<br>
      Vue.use(cafeOfd)
    </h1>
    <el-upload
        class="upload-demo"
        ref="upload"
        action=""
        :on-preview="handlePreview"
        :file-list="fileList"
        :auto-upload="false">
      <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
    </el-upload>
    <div style="width: 100%; height: 60vh; border: 1px solid red; background-color:#ccc;" id="easyofd">
      <cafe-ofd ref="ofd" :filePath="file" @on-success="load" @on-scroll="scroll">
        <div slot="header">
          <input type="file" ref="file" class="hidden" placeholder="选择文件" accept=".ofd" @change="fileChanged">
        </div>
        <div slot="footer" class="footer">
          <el-button @click="plus">放大</el-button>
          <el-button @click="minus">缩小</el-button>
          <el-button @click="pre" :disabled="currentNum <= 1">上一页</el-button>
          <el-input type="number" :min="1" :max="pageNum" v-model.number="currentNum" @change="pageChange(currentNum)"></el-input>
          <el-button @click="next" :disabled="currentNum >= pageNum">下一页</el-button>
          <el-button @click="print">打印</el-button>
        </div>
      </cafe-ofd>
    </div>
  </div>

</template>

<script>
export default {
  data(){
    return {
      fileList: [],
      currentNum: null,
      file: null,
      pageNum: null
    }
  },

  mounted() {
  },
  methods: {
    handlePreview(e){
      this.file = e.raw
    },
    load(val) {
      this.pageNum = val;
    },
    fileChanged() {
      this.file = this.$refs.file.files[0];
    },
    plus() {
      this.$refs.ofd.scale(1.1);
    },
    minus() {
      this.$refs.ofd.scale(-0.9);
    },
    next() {
      this.$refs.ofd.nextPage();
    },
    pre() {
      this.$refs.ofd.prePage();
    },
    pageChange(val) {
      this.$refs.ofd.goToPage(val);
    },
    print() {
      this.$refs.ofd.printFile();
    },
    scroll(val) {
      this.currentNum = val;
    }
  }
}
</script>

<style scoped>
.footer {
  padding: 0 20px;
  display: flex;
  justify-content: space-between;
  gap: 20px;
}
@media print {
  html,
  body,
  #app {
    height: auto;
    overflow: auto;
  }
}
</style>

4、案例-图示

相关推荐
岁月宁静23 分钟前
在富文本编辑器中封装实用的 AI 写作助手功能
前端·vue.js·人工智能
金士顿28 分钟前
为什么MainWindow.xaml绑定的datacontext,EtherCATSuiteCtrl.xaml直接用了?
前端
533_32 分钟前
[css] flex布局中的英文字母不换行问题
前端·css
浮游本尊1 小时前
React 18.x 学习计划 - 第四天:React Hooks深入
前端·学习·react.js
future_studio1 小时前
聊聊 Unity(小白专享、C# 小程序 之 日历、小闹钟)
前端·html
Yeats_Liao2 小时前
Go Web 编程快速入门 · 04 - 请求对象 Request:头、体与查询参数
前端·golang·iphone
祈祷苍天赐我java之术2 小时前
Redis 数据类型与使用场景
java·开发语言·前端·redis·分布式·spring·bootstrap
草莓熊Lotso3 小时前
C++ 方向 Web 自动化测试入门指南:从概念到 Selenium 实战
前端·c++·python·selenium
JS.Huang3 小时前
【JavaScript】原生函数
开发语言·javascript·ecmascript
Olrookie3 小时前
若依前后端分离版学习笔记(二十)——实现滑块验证码(vue3)
java·前端·笔记·后端·学习·vue·ruoyi