前端预览doc文件

今天让做一个登录页面的用户协议和隐私协议的预览

首先在vue项目的public文件夹的static文件夹项目放入

方法一

使用mammoth.js   会忽略复杂样式 我的版本是  "mammoth": "^1.7.2"

<template>
  <div class="word-wrap-ss">
    <div id="wordView" v-html="wordText" />
  </div>
</template>

<script>
// docx文档预览(只能转换.docx文档,转换过程中复杂样式被忽,居中、首行缩进等)
import mammoth from 'mammoth'
export default {
  name: 'Word1000',
  data() {
    return {
      wordText: '',
      wordURL: 'http://192.168.2.50:8080/static/userProtocol.docx' //文件地址
    }
  },
  created() {
    let str = window.location.href
    let arr = str.split('#')
    this.wordURL = arr[0] + 'static/userProtocol.docx'
    this.getWordText()
  },
  methods: {
    getWordText() {
      const xhr = new XMLHttpRequest()
      xhr.open('get', this.wordURL, true)
      xhr.responseType = 'arraybuffer'
      xhr.onload = () => {
        if (xhr.status == 200) {
          mammoth.convertToHtml({ arrayBuffer: new Uint8Array(xhr.response) }).then(resultObject => {
            this.$nextTick(() => {
              this.wordText = resultObject.value
            })
          })
        }
      }
      xhr.send()
    }
  }
}
</script>

<style>
.word-wrap-ss {
  padding: 20px;
  font-size: medium;
}
</style>

方法二

使用docx-preview  

这里特别需要注意版本,我使用的这两个版本才正常运行

"docx-preview": "^0.1.4",    "jszip": "^3.10.0",

window.JSZip = require('jszip')   这一步是必要的,看了原码发现docx-preview里面require('JSZip')

<template>
  <div class="word-wrap-ss">
    <div id="file" />
  </div>
</template>

<script>
const docx = require('docx-preview')
window.JSZip = require('jszip')
export default {
  name: 'Word1000',
  data() {
    return {
      wordURL: 'http://192.168.2.50:8080/static/userProtocol.docx' //文件地址
    }
  },
  created() {
    let str = window.location.href
    let arr = str.split('#')
    this.wordURL = arr[0] + 'static/userProtocol.docx'
    this.getWordText()
  },
  methods: {
    getWordText() {
      const xhr = new XMLHttpRequest()
      xhr.open('get', this.wordURL, true)
      xhr.responseType = 'blob'
      xhr.onload = () => {
        if (xhr.status == 200) {
          docx
            .renderAsync(xhr.response, document.getElementById('file'), null, {
              className: 'docx', // 默认和文档样式类的类名/前缀
              inWrapper: true, // 启用围绕文档内容渲染包装器
              ignoreWidth: false, // 禁止页面渲染宽度
              ignoreHeight: false, // 禁止页面渲染高度
              ignoreFonts: false, // 禁止字体渲染
              breakPages: false, // 在分页符上启用分页
              ignoreLastRenderedPageBreak: false, //禁用lastRenderedPageBreak元素的分页
              experimental: false, //启用实验性功能(制表符停止计算)
              trimXmlDeclaration: false, //如果为真,xml声明将在解析之前从xml文档中删除
              debug: false // 启用额外的日志记录
            })
            .then(res => {
              console.log('res---->', res)
            })
            .catch(err => {
              console.log('err---->', err)
            })
        }
      }
      xhr.send()
    }
  }
}
</script>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码老祖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值