vue使用html-docx基于TinyMCE 导出Word 文档

背景

最近有个需求基于TinyMCE富文本插件导出word,导出并保持富文本样式不变,

前端实现word导出的方式主要有两种:

  • docx:通过 API 构建 Word 文档(控制精细,但代码量大)。
  • html-docx:直接把 HTML 转换成Word(简单快捷,适合文章导出)。
    基于快速开发,于是选择了html-docx

实现思路

1.从 TinyMCE 获取 HTML 内容(如果是 Markdown,可以先转成 HTML)。

2.使用 html-docx 把 HTML 转换为 Word 的 Blob 对象。

3.动态创建 a标签,触发浏览器下载。

注意事项

直接在项目中通过 import htmlDocx from 'html-docx-js' 可能会报错(尤其是在 Vue / Vite / TypeScript 环境下),所以我直接将 html-docx.js 放在项目的 public 目录下,然后在代码中通过全局对象引用:

javascript 复制代码
// 假设你把文件放在 public/lib/html-docx.js
// 在 main.ts 或组件中直接使用 window.htmlDocx
const blob = window.htmlDocx.asBlob(html, { orientation: 'portrait' });

实现代码

javascript 复制代码
const exportToDocx = async () => {
  if (!queryParams.value.wTitle) {
    return proxy?.$modal.msgError('导出失败:请输入文章标题');
  }

  // 将 Markdown 转换为 HTML(如果 TinyMCE 直接返回 HTML,就不需要这一步)
  const textHtml = marked(docContent.value);

  // 拼接完整 HTML,定义页面样式
  const html = `
    <html>
      <head>
        <meta charset="UTF-8">
        <style>
          @page {
            size: A4;
            margin-top: 3.7cm;
            margin-bottom: 3.5cm;
            margin-left: 2.8cm;
            margin-right: 2.6cm;
          }
          body {
            font-size: 16pt;
            line-height: 28pt;       /* 固定 28 磅行距 */
            font-family: FangSong, serif;
            text-indent: 2em;        /* 首行缩进 2 字 */
          }
          p {
            margin: 0;               /* 避免额外段间距 */
            padding: 0;
          }
        </style>
      </head>
      <body>${textHtml}</body>
    </html>`;

  // 使用 html-docx 转换成 Word Blob
  const blob = window.htmlDocx.asBlob(html, { orientation: 'portrait' });

  // 触发浏览器下载
  const link = document.createElement('a');
  link.href = URL.createObjectURL(blob);
  link.download = `${queryParams.value.wTitle}.docx`;
  document.body.appendChild(link);
  link.click();
  document.body.removeChild(link);
};

附件:html-docx.js源码下载

html-docx.js

相关推荐
假如让我当三天老蒯10 小时前
Options API(选项式 API) 和 Composition API(组合式 API)
前端·vue.js·面试
anOnion20 小时前
构建无障碍组件之Menu Button pattern
前端·html·交互设计
米丘1 天前
微前端之 Web Components 完全指南
微服务·html
秃头网友小李3 天前
前端难点:keep-alive 缓存什么?RouterView 的 key 为什么要带 scopeId?
前端·vue.js
徐小夕3 天前
JitWord 3.0 正式发布,高精度Word异构解析+复杂组件兼容,打造web端协同Word编辑器
前端·vue.js·算法
奋斗吧程序媛4 天前
补充一个小知识点:有关@click.native
前端·vue.js
英勇无比的消炎药4 天前
一行命令背后:TinyRobot CLI 如何重构 AI 对话接入的效率范式
vue.js·aigc
Metaphor6924 天前
使用 Python 将 PDF 转换为 HTML
python·pdf·html
jay神4 天前
基于 FastAPI + Vue 的宠物领养管理系统
前端·vue.js·python·毕业设计·fastapi·宠物
一杯奶茶¥4 天前
水果销售网站 CRM客户信息管理系统 超市管理系 酒店管理系统 健身房管理系统 在线音乐网站 校园招聘系统
java·vue.js·spring boot·mysql·spring·java项目