当前位置:首页 > 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

vue 实现word文档预览

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:

vue 实现word文档预览

<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
分享给朋友:

相关文章

vue 实现菜单

vue 实现菜单

Vue 实现菜单的方法 使用 Vue 实现菜单可以通过多种方式,以下是几种常见的方法: 使用 Vue Router 实现动态路由菜单 通过 Vue Router 可以动态生成菜单,根据路由配置自动渲…

vue实现bootstrap

vue实现bootstrap

Vue 中集成 Bootstrap 的方法 在 Vue 项目中集成 Bootstrap 可以通过以下方式实现,涵盖样式、组件和交互功能。 安装 Bootstrap 依赖 通过 npm 或 yarn…

vue拼图实现

vue拼图实现

实现 Vue 拼图游戏的方法 使用 Vue 组件和动态数据绑定 创建一个 Vue 组件来管理拼图的状态和逻辑。通过 v-for 动态渲染拼图块,利用 v-bind 绑定样式和位置。拼图块的数据可以存储…

vue实现CRUD

vue实现CRUD

Vue 实现 CRUD 操作 Vue 提供了响应式数据绑定和组件化开发能力,结合后端 API 可以轻松实现 CRUD(增删改查)功能。以下是基于 Vue 3 和 Axios 的完整实现方案。 安装依…

vue实现模糊

vue实现模糊

Vue实现模糊搜索的方法 在Vue中实现模糊搜索功能通常需要结合输入框和列表渲染,通过监听输入内容动态过滤数据。以下是几种常见实现方式: 使用计算属性实现 计算属性适合处理需要响应式更新的搜索逻辑:…

vue实现折叠

vue实现折叠

Vue 实现折叠功能 在 Vue 中实现折叠功能可以通过多种方式完成,以下介绍几种常见的方法。 使用 v-show 或 v-if 控制显示隐藏 通过 Vue 的指令 v-show 或 v-if 可以…