【前端】每天一个简单库的使用-vue-office

在前端实现PDF、Execl、Word预览。暂时遇到的问题就是Word渲染时分页支持的不是很好。 当然还有其他的方案,比如onlyoffice不过这个需要单独部署服务。

实例
npm 复制代码
npm install @vue-office/docx vue-demi #excel文档预览组件 
npm install @vue-office/excel vue-demi #pdf文档预览组件 
npm install @vue-office/pdf vue-demi
// 如果是vue2.6版本或以下还需要额外安装 @vue/composition-api
npm install @vue/composition-api
组件定义
js 复制代码
<template>
  <div class="file-preview-wrapper">
    <vue-office-docx
      v-if="docType === 'docx'"
      :src="docContent"
      class="file-body"
      :options="docOptions"
      @rendered="renderedHandler"
      @error="errorHandler"
    />
    <vue-office-excel
      v-else-if="docType === 'xlsx'"
      :src="docContent"
      class="file-body"
      @rendered="renderedHandler"
      @error="errorHandler"
    />
    <vue-office-pdf
      v-else-if="docType === 'pdf'"
      :src="docContent"
      class="file-body"
      @rendered="renderedHandler"
      @error="errorHandler"
    />
    <div v-else class="file-body">
      <el-empty description="暂无内容" />
    </div>
  </div>
</template>
<script>
import VueOfficeDocx from '@vue-office/docx';
import '@vue-office/docx/lib/index.css';
import VueOfficeExcel from '@vue-office/excel';
import '@vue-office/excel/lib/index.css';
import VueOfficePdf from '@vue-office/pdf';
export default {
  props: {
    docType: {
      required: true,
      type: String,
      validator(value) {
        return ['docx', 'xlsx', 'pdf'].includes(value);
      }
    },
    docContent: {
      required: true,
      type: String
    }
  },
  components: {
    VueOfficeDocx,
    VueOfficeExcel,
    VueOfficePdf
  },
  data() {
    return {
      docOptions: {
        inWrapper: true, //enables rendering of wrapper around document content
        ignoreWidth: true, //disables rendering width of page
        ignoreHeight:true, //disables rendering height of page
        ignoreFonts: true, //disables fonts rendering
        breakPages: true, //enables page breaking on page breaks
        ignoreLastRenderedPageBreak: false, //disables page breaking on lastRenderedPageBreak elements
        experimental: true, //enables experimental features (tab stops calculation)
        trimXmlDeclaration:true, //if true, xml declaration will be removed from xml documents before parsing
        useBase64URL: true, //if true, images, fonts, etc. will be converted to base 64 URL, otherwise URL.createObjectURL is used
        useMathMLPolyfill: true, //includes MathML polyfills for chrome, edge, etc.
        showChanges: false, //enables experimental rendering of document changes (inserions/deletions)
        debug: true //enables additional logging
      }
    };
  },
  methods: {
    renderedHandler() {
      console.log('渲染完成');
    },
    errorHandler() {
      console.log('渲染失败');
    }
  }
};
</script>

<style lang="scss" scoped>
.file-preview-wrapper {
  height: 100%;
  width: 100%;
}
.file-body {
  height: 100%;
}
</style>
组件使用
js 复制代码
<template>
    <FilePreview  :docType="docType" :docContent="docContent" />
</template>
<script>
    export default {
        data(){
            return {
                docType:'docx',
                docContent:'https://501351981.github.io/vue-office/examples/dist/#/docx'
            }
        },
        created(){
            //或者网络请求返回Arraybuffer
            getXXXX().then(res=>{
                 let docData = new Blob([res], {
                  type: 'application/octet-stream'
                });
                const url = URL.createObjectURL(docData);
                this.docContent = url;
            })
        }
    }
</script>
相关推荐
林恒smileZAZ2 小时前
Vue<前端页面版本检测>
前端·javascript·vue.js
我是Superman丶7 小时前
Element UI 表格某行突出悬浮效果
前端·javascript·vue.js
Cobyte11 小时前
3.响应式系统基础:从发布订阅模式的角度理解 Vue2 的数据响应式原理
前端·javascript·vue.js
军军君0111 小时前
Three.js基础功能学习十八:智能黑板实现实例五
前端·javascript·vue.js·3d·typescript·前端框架·threejs
禅思院11 小时前
前端架构演进:基于AST的常量模块自动化迁移实践
前端·vue.js·前端框架
许杰小刀11 小时前
FastAPI + Vue 前后端分离实战:我的项目结构“避坑指南”
前端·vue.js·fastapi
walking95711 小时前
Vue3 日历组件选型指南:五大主流方案深度解析
前端·vue.js·面试
英俊潇洒美少年11 小时前
Vue、React.lazy、React 19 异步组件核心区别
javascript·vue.js·react.js
快乐小土豆~~12 小时前
echarts柱状图的X轴label过长被重叠覆盖
前端·javascript·vue.js·echarts
儒雅的烤地瓜12 小时前
Vue | Vue3中<script setup>用法详解
vue.js·vue3·选项式api·组合式 api·setup方法·<script setup>