word预览方式---插件,vue-office-docx、docx-preview、mammoth.js

提示:word预览方式—插件

前言

word预览

在这里插入图片描述

一、vue-office-docx

npm install vue-office-docx -S-D

officeDocx.vue

<template>
    <div class="preview_box">
      <VueOfficeDocx :src="htmlContent"></VueOfficeDocx>
    </div>
  </template>
  
  <script>
  import axios from 'axios'
  import VueOfficeDocx from '@vue-office/docx'
  //引入相关样式
  import '@vue-office/docx/lib/index.css';

  
  export default {
    name: 'preview',
    components:{VueOfficeDocx},
    data () {
      return {
        src:`.docx文件rul`,
        htmlContent:''
      }
    },
    mounted(){
      this.docToHtml();
    },
    methods: {
      docToHtml(){
        axios.get(this.src,{ responseType: 'arraybuffer' }).then((res)=>{
          this.htmlContent = res.data;
        })
      }
    }
  }
  </script>
  <style scoped></style>
  

在这里插入图片描述

样式还原度一般,间距太大,分页也有问题

二、docx-preview

npm install docx-preview -S-D

docxPreview.vue

<template>
    <div class="preview_box">
       <div ref="docxPreviewRef"></div>
    </div>
  </template>
  
  <script>
  import axios from 'axios'
  import { renderAsync }  from 'docx-preview'

  
  export default {
    name: 'preview',
    components:{},
    data () {
      return {
        src:`.docx文件rul`,
      }
    },
    mounted(){
      this.docToHtml();
    },
    methods: {
      docToHtml(){
        axios.get(this.src,{ responseType: 'arraybuffer' }).then((res)=>{
          renderAsync(res.data, this.$refs.docxPreviewRef);
        })
      }
    }
  }
  </script>
  <style scoped></style>
  

在这里插入图片描述
样式还原度一般,无分页

三、mammoth.js

npm install mammoth.js -S-D

docxMammoth.vue

<template>
    <div class="preview_box">
       <div ref="docxPreviewRef" v-html="htmlContent"></div>
    </div>
  </template>
  
  <script>
  import axios from 'axios'
  import mammoth  from 'mammoth'
  export default {
    name: 'preview',
    components:{},
    data () {
      return {
        src:`.docx文件rul`,
        htmlContent:''
      }
    },
    mounted(){
      this.docToHtml();
    },
    methods: {
      docToHtml(){
        axios.get(this.src,{ responseType: 'arraybuffer' }).then((res)=>{
          mammoth.convertToHtml({ arrayBuffer: new Uint8Array(res.data) }).then((html)=>{
            this.htmlContent = html.value;
          })
        })
      }
    }
  }
  </script>
  <style scoped></style>
  

在这里插入图片描述
样式有问题,表格都没了

总结

踩坑路漫漫长@~@

如果您想在 Vue 内网项目中使用 `docx-preview`、`mammoth` 和 `MathJax` 来浏览带有公式的 DOCX 文件,可以按照以下步骤进行操作: 1. 首先,安装所需的库: ```bash npm install docx-preview mammoth mathjax --save ``` 2. 在您的 Vue 组件中导入所需的库和组件: ```vue <template> <div> <DocxPreview :src="docxFilePath" /> </div> </template> <script> import DocxPreview from 'docx-preview' import * as mammoth from 'mammoth' import MathJax from 'mathjax' export default { components: { DocxPreview }, data() { return { docxFilePath: '/path/to/your/docx/file.docx' } }, mounted() { this.renderDocx() }, methods: { renderDocx() { mammoth.extractRawText({ path: this.docxFilePath }) .then((result) => { const html = result.value MathJax.typesetPromise() .then(() => { this.$refs.docxPreview.setContent(html) }) }) .done() } } } </script> ``` 在上述示例中,我们首先导入了 `docx-preview` 组件、`mammoth` 库和 `MathJax` 库。然后,在 Vue 组件的 `mounted` 钩子中,我们调用 `renderDocx` 方法来加载和渲染 DOCX 文件。 `renderDocx` 方法使用 `mammoth` 库的 `extractRawText` 方法来提取 DOCX 文件的原始文本,并生成对应的 HTML。然后,我们使用 `MathJax.typesetPromise()` 方法来渲染 HTML 中的公式。最后,我们使用 `setContent` 方法将渲染后的 HTML 内容传递给 `docx-preview` 组件。 请确保将路径 `/path/to/your/docx/file.docx` 替换为实际的 DOCX 文件路径。 这样,您就可以在 Vue 内网项目中使用 `docx-preview`、`mammoth` 和 `MathJax` 来浏览带有公式的 DOCX 文件了。 希望这个解决方案对您有所帮助。如果您有其他问题,请随时提问。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值