Mammoth.js:将.docx 文件转换成HTML,从后台获取的文件流数据信息

1、图片和pdf文件可以直接在页面中预览

使用iframe src:后台返回的文件地址或者请求文件流的接口地址直接预览

2、docx文件:

2、1 后台返回的是文件流:

<template>
 <div>
   <!--文件预览的模块-->
   <div v-html="vHtml"></div>
 </div>
</template>
<script>
import mammoth from 'mammoth' // mammoth的版本是1.7.2
</script>
fetch(url,{mode:'no-cors'})
.then((response) => response.blob())
.then((res)=>{
  const reader = new FileReader()
  reader.readAsArrayBuffer(res)
  reader.onload =  function(e){
    mammoth.covertToHtml({ arrayBuffer: e.target.result },
                         { includeDefaultStyleMap : true})
                            .then((result)=>{
                               let htmlInfo = result.value
                               //html样式的转化:此处可以根据实际情况进行设置
                               if(htmlInfo){
                                 let newHtml =  htmlInfo.replace(/<img /g,'<img style="max-width:90%;margin:20px auto"')
.replace(/<table/g,'<table style="border:1px solid #ddd"')
  this.vHtml = newHtml
                               }
                             })
  }
})

3、可以参考其他的文章信息

https://blog.csdn.net/xilejie/article/details/103304391  

设置展示的文本样式信息 https://www.5axxw.com/questions/simple/9h8m7s

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值