当前位置:首页 > VUE

vue 实现word文档预览

2026-02-24 09:23:51VUE

Vue 实现 Word 文档预览的方法

使用 mammoth.js 库解析 .docx 文件

安装 mammoth.js

npm install mammoth

在 Vue 组件中使用:

<template>
  <div>
    <input type="file" @change="handleFileUpload" accept=".docx" />
    <div v-html="htmlContent"></div>
  </div>
</template>

<script>
import mammoth from "mammoth";

export default {
  data() {
    return {
      htmlContent: "",
    };
  },
  methods: {
    handleFileUpload(event) {
      const file = event.target.files[0];
      const reader = new FileReader();
      reader.onload = (e) => {
        mammoth
          .extractRawText({ arrayBuffer: e.target.result })
          .then((result) => {
            this.htmlContent = result.value;
          });
      };
      reader.readAsArrayBuffer(file);
    },
  },
};
</script>

使用 docx-preview 库渲染 .docx 文件

安装 docx-preview

npm install docx-preview

在 Vue 组件中使用:

<template>
  <div>
    <input type="file" @change="handleFileUpload" accept=".docx" />
    <div ref="docxContainer"></div>
  </div>
</template>

<script>
import { renderAsync } from "docx-preview";

export default {
  methods: {
    handleFileUpload(event) {
      const file = event.target.files[0];
      const container = this.$refs.docxContainer;
      renderAsync(file, container);
    },
  },
};
</script>

使用 Office Web Viewer 在线预览

直接嵌入 Office Web Viewer 的 iframe:

<template>
  <iframe
    :src="`https://view.officeapps.live.com/op/embed.aspx?src=${encodeURIComponent(
      fileUrl
    )}`"
    width="100%"
    height="600px"
    frameborder="0"
  ></iframe>
</template>

<script>
export default {
  data() {
    return {
      fileUrl: "https://example.com/document.docx",
    };
  },
};
</script>

使用 pdf-lib 转换为 PDF 后预览

安装 pdf-libdocx

npm install pdf-lib docx

转换并预览:

<template>
  <div>
    <input type="file" @change="handleFileUpload" accept=".docx" />
    <iframe v-if="pdfUrl" :src="pdfUrl" width="100%" height="600px"></iframe>
  </div>
</template>

<script>
import { Document, Packer } from "docx";
import { PDFDocument } from "pdf-lib";

export default {
  data() {
    return {
      pdfUrl: "",
    };
  },
  methods: {
    async handleFileUpload(event) {
      const file = event.target.files[0];
      const arrayBuffer = await file.arrayBuffer();
      const docx = await Document.load(arrayBuffer);
      const pdfDoc = await PDFDocument.create();
      // 转换逻辑(示例,需根据实际需求实现)
      const pdfBytes = await pdfDoc.save();
      const blob = new Blob([pdfBytes], { type: "application/pdf" });
      this.pdfUrl = URL.createObjectURL(blob);
    },
  },
};
</script>

注意事项

  • .doc 文件(旧版 Word 格式)可能需要使用 antiword 或其他工具转换,浏览器端支持有限。
  • 大文件处理建议使用后端服务转换后返回预览链接。
  • 敏感文档避免直接前端解析,可能存在安全隐患。

vue 实现word文档预览

标签: 文档vue
分享给朋友:

相关文章

用vue实现滑动输入条

用vue实现滑动输入条

使用 Vue 实现滑动输入条 使用原生 HTML5 input range 实现 在 Vue 模板中直接使用 <input type="range">,通过 v-model 绑定数据:…

vue 实现复选

vue 实现复选

Vue 实现复选功能 在 Vue 中实现复选功能可以通过多种方式完成,以下是常见的几种方法: 使用 v-model 绑定数组 通过 v-model 绑定一个数组,可以自动收集用户选择的复选框值。…

vue 动画 实现

vue 动画 实现

vue 动画实现方法 Vue 提供了多种方式实现动画效果,主要包括过渡动画和动态动画两类。 使用 <transition> 组件实现基础过渡 通过 Vue 内置的 <transit…

vue grid实现

vue grid实现

Vue Grid 实现方法 在 Vue 中实现网格布局可以通过多种方式完成,包括使用原生 CSS Grid、第三方组件库或自定义组件。以下是几种常见方法: 使用原生 CSS Grid 通过 Vue…

vue实现翻译

vue实现翻译

Vue 实现翻译功能的方法 使用 i18n 插件实现多语言切换 安装 vue-i18n 插件: npm install vue-i18n 创建语言资源文件(如 en.json 和 zh.json)…

vue实现换肤

vue实现换肤

使用 CSS 变量实现换肤 在 Vue 项目中,可以通过 CSS 变量(Custom Properties)动态切换主题色。CSS 变量可以在运行时修改,适合实现换肤功能。 定义全局 CSS 变量…