【前端】每天一个简单库的使用-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>
相关推荐
m0_740043732 小时前
3、Vuex-Axios-Element UI
前端·javascript·vue.js
鹏北海2 小时前
微信扫码登录 iframe 方案中的状态拦截陷阱
前端·javascript·vue.js
狗哥哥2 小时前
Vite 插件实战 v2:让 keep-alive 的“组件名”自动长出来
前端·vue.js·架构
小黑的铁粉2 小时前
Vue2 vs Vue3
vue.js
AAA阿giao2 小时前
代码宇宙的精密蓝图:深入探索 Vue 3 + Vite 项目的灵魂结构
前端·javascript·vue.js
半桶水专家2 小时前
vue中的props详解
前端·javascript·vue.js
前端不太难2 小时前
RN 遇到复杂手势(缩放、拖拽、旋转)时怎么设计架构
javascript·vue.js·架构
白兰地空瓶2 小时前
一行 npm init vite,前端工程化的世界就此展开
前端·vue.js·vite
码力巨能编2 小时前
Markdown 作为 Vue 组件导入
前端·javascript·vue.js
仰望.3 小时前
vue 甘特图 vxe-gantt table 拖拽任务调整开始日期和结束日期的使用,拖拽任务调整日期
vue.js·甘特图·vxe-ui