当前位置:   article > 正文

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

cafe-ofd

0、提示

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

其他平台可以参考 ofd - npm

官方线上demo: ofd

1、安装包

npm install cafe-ofd --save

2、引入

  1. import cafeOfd from 'cafe-ofd'
  2. import 'cafe-ofd/package/index.css'
  3. Vue.use(cafeOfd)

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

  1. <template>
  2. <div style="padding: 20px 100px; font-size: 20px">
  3. <h1>ofd文件预览</h1>
  4. <h1>https://www.npmjs.com/package/cafe-ofd</h1>
  5. <h1>npm install cafe-ofd --save</h1>
  6. <h1>
  7. import cafeOfd from 'cafe-ofd' <br>
  8. import 'cafe-ofd/package/index.css'<br>
  9. Vue.use(cafeOfd)
  10. </h1>
  11. <el-upload
  12. class="upload-demo"
  13. ref="upload"
  14. action=""
  15. :on-preview="handlePreview"
  16. :file-list="fileList"
  17. :auto-upload="false">
  18. <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
  19. </el-upload>
  20. <div style="width: 100%; height: 60vh; border: 1px solid red; background-color:#ccc;" id="easyofd">
  21. <cafe-ofd ref="ofd" :filePath="file" @on-success="load" @on-scroll="scroll">
  22. <div slot="header">
  23. <input type="file" ref="file" class="hidden" placeholder="选择文件" accept=".ofd" @change="fileChanged">
  24. </div>
  25. <div slot="footer" class="footer">
  26. <el-button @click="plus">放大</el-button>
  27. <el-button @click="minus">缩小</el-button>
  28. <el-button @click="pre" :disabled="currentNum <= 1">上一页</el-button>
  29. <el-input type="number" :min="1" :max="pageNum" v-model.number="currentNum" @change="pageChange(currentNum)"></el-input>
  30. <el-button @click="next" :disabled="currentNum >= pageNum">下一页</el-button>
  31. <el-button @click="print">打印</el-button>
  32. </div>
  33. </cafe-ofd>
  34. </div>
  35. </div>
  36. </template>
  37. <script>
  38. export default {
  39. data(){
  40. return {
  41. fileList: [],
  42. currentNum: null,
  43. file: null,
  44. pageNum: null
  45. }
  46. },
  47. mounted() {
  48. },
  49. methods: {
  50. handlePreview(e){
  51. this.file = e.raw
  52. },
  53. load(val) {
  54. this.pageNum = val;
  55. },
  56. fileChanged() {
  57. this.file = this.$refs.file.files[0];
  58. },
  59. plus() {
  60. this.$refs.ofd.scale(1.1);
  61. },
  62. minus() {
  63. this.$refs.ofd.scale(-0.9);
  64. },
  65. next() {
  66. this.$refs.ofd.nextPage();
  67. },
  68. pre() {
  69. this.$refs.ofd.prePage();
  70. },
  71. pageChange(val) {
  72. this.$refs.ofd.goToPage(val);
  73. },
  74. print() {
  75. this.$refs.ofd.printFile();
  76. },
  77. scroll(val) {
  78. this.currentNum = val;
  79. }
  80. }
  81. }
  82. </script>
  83. <style scoped>
  84. .footer {
  85. padding: 0 20px;
  86. display: flex;
  87. justify-content: space-between;
  88. gap: 20px;
  89. }
  90. @media print {
  91. html,
  92. body,
  93. #app {
  94. height: auto;
  95. overflow: auto;
  96. }
  97. }
  98. </style>

4、案例-图示

5、封装的组件(我在项目中使用的、有使用el组件,只需传入File格式的OFD文件)

  1. <template>
  2. <div class="previewOFDBox">
  3. <cafe-ofd
  4. v-show="file"
  5. ref="ofd"
  6. :width="500"
  7. :filePath="file"
  8. @on-success="load"
  9. @on-scroll="scroll"
  10. :class="['previewOFD', fullScreen ? 'ofd_full_screen' : '']"
  11. >
  12. <div slot="footer" class="footer" style="position: absolute; right: 20px; bottom: 10px; z-index: 999">
  13. <el-button size="mini" circle icon="el-icon-plus" @click="plus"></el-button>
  14. <el-button size="mini" circle icon="el-icon-minus" @click="minus"></el-button>
  15. <el-tooltip v-if="fullScreen" effect="light" content="打印(如果打印预览异常,请将文件视图缩放至合适大小)" placement="top">
  16. <el-button size="mini" circle icon="el-icon-printer" @click="print"></el-button>
  17. </el-tooltip>
  18. <el-button size="mini" circle :icon="fullScreen ? 'el-icon-files' : 'el-icon-full-screen'" @click="full"></el-button>
  19. </div>
  20. </cafe-ofd>
  21. <div v-if="!file" style="position: absolute; top: 50%; left: 48%">
  22. 暂无数据
  23. </div>
  24. </div>
  25. </template>
  26. <script>
  27. export default {
  28. name: "previewOFD",
  29. props: {
  30. file: {
  31. type: File,
  32. }
  33. },
  34. data() {
  35. return {
  36. pageNum: 1,
  37. currentNum: null,
  38. fullScreen: false,
  39. }
  40. },
  41. methods: {
  42. // 加载成功
  43. load(val) {
  44. this.pageNum = val;
  45. this.$emit('success')
  46. },
  47. fileChanged(file) {
  48. this.file = file
  49. },
  50. plus() {
  51. this.$refs.ofd.scale(1.1);
  52. },
  53. minus() {
  54. this.$refs.ofd.scale(-0.9);
  55. },
  56. next() {
  57. this.$refs.ofd.nextPage();
  58. },
  59. pre() {
  60. this.$refs.ofd.prePage();
  61. },
  62. pageChange(val) {
  63. this.$refs.ofd.goToPage(val);
  64. },
  65. print() {
  66. this.$refs.ofd.printFile();
  67. },
  68. scroll(val) {
  69. this.currentNum = val;
  70. },
  71. full(){
  72. this.fullScreen = !this.fullScreen
  73. }
  74. }
  75. }
  76. </script>
  77. <style scoped>
  78. .previewOFD {
  79. transition: all .5s;
  80. }
  81. .previewOFDBox {
  82. position: relative;
  83. width: 100%;
  84. height: 100%;
  85. }
  86. .ofd_full_screen {
  87. z-index: 9999;
  88. position: fixed;
  89. left: 0;
  90. top: 0;
  91. width: 100vw;
  92. height: 100vh;
  93. padding: 10px;
  94. box-shadow: 0 0 4px #000;
  95. background: rgba(0, 0, 0, 0.8);
  96. }
  97. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/686127
推荐阅读
相关标签
  

闽ICP备14008679号