当前位置:首页 > VUE

vue实现word文档预览

2026-02-21 08:46:32VUE

Vue 实现 Word 文档预览

使用 mammoth.js 转换 Word 为 HTML

mammoth.js 是一个将 .docx 文件转换为 HTML 的库,适合在 Vue 中实现轻量级预览。

安装依赖:

npm install mammoth.js

示例代码:

<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-preview 提供更专业的 Word 文档渲染效果,支持样式保留。

安装依赖:

npm install docx-preview

示例代码:

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

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

export default {
  methods: {
    renderDocx(event) {
      const file = event.target.files[0];
      const container = this.$refs.documentContainer;
      renderAsync(file, container).catch((err) => {
        console.error("渲染失败:", err);
      });
    },
  },
};
</script>

通过后端服务转换

若需支持复杂格式或旧版 .doc 文件,可通过后端(如 LibreOffice 或 Python 库)转换为 PDF/HTML 后返回前端显示。

前端示例(假设接口返回 HTML):

<template>
  <div>
    <input type="file" @change="uploadFile" />
    <iframe v-if="previewUrl" :src="previewUrl" width="100%" height="500px"></iframe>
  </div>
</template>

<script>
export default {
  data() {
    return {
      previewUrl: "",
    };
  },
  methods: {
    async uploadFile(event) {
      const formData = new FormData();
      formData.append("file", event.target.files[0]);
      const response = await fetch("/api/convert-to-html", {
        method: "POST",
        body: formData,
      });
      this.previewUrl = await response.text();
    },
  },
};
</script>

注意事项

  • 样式兼容性:转换后的 HTML 可能需要额外 CSS 调整。
  • 文件大小限制:大文件需分块处理或使用进度提示。
  • 安全防护:直接渲染 HTML 时需防范 XSS 攻击,可配合 DOMPurify 过滤内容。

根据需求选择方案:纯前端方案适合简单预览,复杂场景建议结合后端处理。

vue实现word文档预览

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

相关文章

vue实现边框

vue实现边框

Vue 实现边框的方法 在 Vue 中实现边框效果可以通过多种方式完成,包括内联样式、CSS 类绑定、动态样式以及使用第三方 UI 库。以下是几种常见的实现方法。 内联样式绑定 使用 Vue 的…

vue实现旋转

vue实现旋转

Vue 实现旋转效果的方法 在 Vue 中实现旋转效果可以通过多种方式完成,以下是几种常见的方法: 使用 CSS 动画 通过 Vue 的 :class 或 :style 绑定 CSS 动画实现旋转效…

vue实现缩放

vue实现缩放

Vue 实现缩放的方法 在 Vue 中实现缩放功能可以通过多种方式完成,以下是一些常见的方法: 使用 CSS transform 属性 通过 CSS 的 transform: scale() 属性…

vue SSG实现

vue SSG实现

Vue SSG 实现方法 Vue 的静态站点生成(SSG)可以通过多种工具和框架实现,以下是几种常见的方法: 使用 VuePress VuePress 是一个基于 Vue 的静态站点生成器,适合文档…

vue实现全屏

vue实现全屏

Vue 实现全屏功能的方法 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 来实现。以下是几种常见的实现方式: 使用原生 Fullscreen API 通过调用 doc…

vue实现addclass

vue实现addclass

Vue 实现动态添加 class 的方法 在 Vue 中动态添加 class 可以通过多种方式实现,以下是常见的几种方法: 使用对象语法 通过绑定一个对象到 :class,可以动态切换 class…