1.安装

npm install docx-preview

npm install  alloyfinger

2.完整代码如下

获取onLoad()中options参数的文档地址,赋值值给url

<template><div class="main" ref='main'><div class="word" :style="{ transform: `scale(${w})` }"><div id="bodyContainer" ref="wordDiv":style="{ 'transform': 'translate(' + posX + 'px,' + posY + 'px) translateZ(0px) scale(' + dis + ')' }"></div></div></div>
</template><script>
import { renderAsync } from "docx-preview";
import AlloyFinger from 'alloyfinger';
var initScale = 1;
export default {data() {return {docxOptions: {className: "kaimo-docx-666", // string:默认和文档样式类的类名/前缀inWrapper: true, // boolean:启用围绕文档内容的包装器渲染ignoreWidth: false, // boolean:禁用页面的渲染宽度ignoreHeight: false, // boolean:禁止渲染页面高度ignoreFonts: false, // boolean:禁用字体渲染breakPages: true, // boolean:在分页符上启用分页ignoreLastRenderedPageBreak: true, // boolean:在 lastRenderedPageBreak 元素上禁用分页experimental: false, // boolean:启用实验功能(制表符停止计算)trimXmlDeclaration: true, // boolean:如果为true,解析前会从​​ xml 文档中移除 xml 声明useBase64URL: false, // boolean:如果为true,图片、字体等会转为base 64 URL,否则使用URL.createObjectURLuseMathMLPolyfill: false, // boolean:包括用于 chrome、edge 等的 MathML polyfill。showChanges: false, // boolean:启用文档更改的实验性渲染(插入/删除)debug: false, // boolean:启用额外的日志记录},style: {},w: 0,url: null,posX: 0,posY: 0,dis: 1,}},onLoad(options) {console.log('asd',options.url)this.url = options.url},mounted() {if (this.url) {this.getFileFromUrl(this.url, 'word').then(res => {console.log(res)this.docxRender(res)})this.setScale()window.onresize = this.Debounce(this.setScale, 1000)this.setScale2()}},methods: {setScale2() {let speed = 1.5let min = .5new AlloyFinger(this.$refs.main, {pinch: (evt) => {console.log("实现缩放");this.dis = initScale * evt.zoom;if (this.dis < 1) this.dis = 1},pressMove: (evt) => {this.posX += evt.deltaX * speed;this.posY += evt.deltaY * speed;},multipointEnd: () => {initScale = this.dis;}})},docxRender(buffer) {let bodyContainer = document.getElementById("bodyContainer");renderAsync(buffer, // Blob | ArrayBuffer | Uint8Array, 可以是 JSZip.loadAsync 支持的任何类型bodyContainer, // HTMLElement 渲染文档内容的元素,null, // HTMLElement, 用于呈现文档样式、数字、字体的元素。如果为 null,则将使用 bodyContainer。this.docxOptions // 配置).then(res => {console.log(res.data)})},getFileFromUrl(url, fileName) {return new Promise((resolve, reject) => {var blob = null;var xhr = new XMLHttpRequest();xhr.open("GET", url);xhr.setRequestHeader('Accept', 'image/png');xhr.responseType = "blob";// 加载时处理xhr.onload = () => {// 获取返回结果blob = xhr.response;let file = new File([blob], fileName, { type: 'image/png' });// 返回结果resolve(file);};xhr.onerror = (e) => {reject(e)};// 发送xhr.send();});},setScale() {setTimeout(() => {let bodyContainer = document.getElementById("bodyContainer");// this.w =window.innerWidth / bodyContainer.offsetWidththis.w = window.innerWidth < 768 ? window.innerWidth / 768 : window.innerWidth / bodyContainer.offsetWidth// this.style = {//   transform: `scale(${w})`// }}, 200)},Debounce(fn, t) {const delay = t || 500let timerreturn function () {const args = argumentsif (timer) {clearTimeout(timer)}const context = thistimer = setTimeout(() => {timer = nullconsole.log(this.w, window.innerWidth, document.getElementById("bodyContainer").offsetWidth);fn.apply(context, args)}, delay)}},}
}
</script>
<style>
uni-page-body {background-color: white !important;
}
</style>
<style scoped>
.word {transform: scale(0.6);transform-origin: top center;transition: transform 0.4s;
}
</style>

uniapp打开docx文件方法相关推荐

  1. uni-app打开docx文件

    //预览 previewDocx(){uni.downloadFile({url: '服务器链接地址',success: (res) => {if (res.statusCode === 200 ...

  2. 打开Docx文件的方法

    熟悉Word办公软件的朋友都应该知道,Word2003生成的文件格式是Doc的格式,而Word2007或Word2010生成的文件是Docx的.用Word2003是无法打开2007和2010中的Doc ...

  3. 怎样搜索计算机中docx格式的文件,教您电脑docx文件怎样打开呢?教你正确打开docx文件...

    很多朋友通过word2003软件发现无法打开docx格式的文件,其实是由于word2003文件默认格式是doc,所以docx格式的文件需要word2007以上的版本才能打开,那么下面让小编教你如何打开 ...

  4. Docx文件怎么打开,如何打开Docx文件?

    熟悉Word办公软件的朋友都应该知道,Word2003生成的文件格式是Doc的格式,而Word2007或Word2010生成的文件是Docx的.用Word2003是无法打开2007和2010中的Doc ...

  5. 如何打开.docx文件?

    今天收到一份扩展名为docx的文件,原本以为是谁不小心在doc后面加了个x的,把x去掉后准备用word打开~居然是乱码~,上网一搜,原来docx是word2007的文件~太落伍了~居然不知道~_~! ...

  6. 如何打开docx文件

    如何打开docx文件? 最直接有效的一种方法就是安装office2007,这点大家应该都知道.不过有时候手边没有   office2007怎么办? 比如你去同学或者朋友家,给他看些好玩的: 比较常见的 ...

  7. php 打开docx文件怎么打开,docx怎么打开 怎么在网页上打开docx文件

    Smarty的date_format总是提示参数不对,按照手册的index.php <?php require_once './init.inc.php'; $smarty->displa ...

  8. php 打开docx文件,通过Php提供.docx文件

    尝试使用Php提供.docx文件时遇到问题.上传文件时,我检测文件mime类型,并使用基于mime类型的正确扩展名的文件上传文件;例如下面: application/msword - doc appl ...

  9. html加密文档如何打开,如何打开docx文件 在弹出窗口中选择加密的文件

    如何打开docx的文件,我的office打不开 在电脑桌面上找到需要打开的docx文件,并右键打开菜单. 在打开的菜单界面中,找到打开方式选项,并单击选择. 在跳转的打开方式界面中,选择wps应用程序 ...

最新文章

  1. 跳槽“六要”你懂吗?[转载]
  2. Kilim实现浅析(一)
  3. linux shell 切割文件,linux shell 将文件按照行数以及顺序拆分成多个文件
  4. 数据结构课程设计------c实现散列表(二次探测再哈希)电话簿(文件存储)
  5. 不需要登陆的灵感笔记私人版正式上线,迭代优化中。
  6. php 内置mail 包,PHP使用pear自带的mail类库发邮件的方法
  7. 拉丁超立方试验设计_南水北调工程通水 六年惠及超1.2亿人
  8. ubuntu 16.04 R 安装,卸载以及Rsudio
  9. 循环体中调用[NSString stringWithFormat:@%@,Object] 方法的内存问题
  10. ACL-IJCNLP 2021|行业首个少样本NER数据集,清华联合阿里达摩院开发
  11. ecshop后台首页mysql_ecshop商城后台使用手册
  12. 五大主流浏览器及四大内核
  13. 已知ip地址和其子网掩码如何求网络号子网号主机号
  14. Spring源码编译问题--xml-apis:xml-apis引入
  15. outlook2010 html签名,Outlook2010如何设置签名
  16. 删除你的所有计算机文件的英文,删除Download和DataStore文件夹中的所有文件
  17. 餐饮行业选址如何用大数据软件了解相关要素
  18. 采购员CPPM证书含金量大吗?
  19. 【Wikipedia爬虫工具包的使用】请求超时、代理错误、SSLError
  20. 计算机一级培训 ppt,计算机基础培训ppt

热门文章

  1. 电路原理实验六:戴维南定理与诺顿定理的验证
  2. linux配置jdk环境_linux配置jdk环境变量显示不识别的标志符
  3. 聊一聊数据加密中如何进行模糊查询
  4. freemarker加载模板的三种方法
  5. 五大评价偏误 | 管理者如何做到客观评价员工?
  6. Windows网络编程 undefined reference to `__imp_WSAStartup‘ 解决办法
  7. 把灵龟八法程序改为了嵌入式芯片
  8. 你可能需要这份大厂面经
  9. Python从入门到精通的完整学习路线图(流程图)【附:全套python学习资料】
  10. 同程艺龙2020秋招笔试--Binrry(冰蕊)