@apollo-design/docx-preview 0.6.3 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +14 -125
  2. package/package.json +3 -12
package/README.md CHANGED
@@ -1,154 +1,43 @@
1
1
  # @apollo-design/docx-preview
2
2
 
3
- 将 DOCX 文档渲染为保留语义的 HTML,并附带一个可 iframe 嵌入的独立预览器。
3
+ 将 DOCX 文档渲染为保留语义的 HTML
4
4
 
5
- > Based on [docxjs](https://github.com/VolodymyrBaydalka/docxjs) by Volodymyr Baydalka (Apache License 2.0) — see [Credits](#credits).
5
+ > Based on [docxjs](https://github.com/VolodymyrBaydalka/docxjs) by Volodymyr Baydalka (Apache License 2.0).
6
6
 
7
- ## 目标
8
-
9
- 尽可能保持 HTML 语义地把 DOCX 渲染/转换为 HTML。库的能力受 HTML 本身限制(例如 Google Docs 是把文档绘制成 canvas 图像)。在此之上,本项目的重点是**公文级版式保真**:字体分 script 级联解析、按行重排分页、页眉页脚页码字段、浮动表格置底、Word 2003 兼容两端对齐等。
10
-
11
- ## 三个使用入口
12
-
13
- ### 1. npm 包(Vite / Vue / React 等工程)
7
+ ## 安装
14
8
 
15
9
  ```bash
16
10
  pnpm add @apollo-design/docx-preview
17
11
  ```
18
12
 
13
+ ## 用法
14
+
19
15
  ```ts
20
16
  import { renderAsync } from '@apollo-design/docx-preview';
21
17
 
22
18
  await renderAsync(docData, document.getElementById('container'));
23
19
  ```
24
20
 
25
- 也支持传统 script 标签方式(UMD 产物通过全局 `docx` 暴露,jszip 需自行引入为全局 `JSZip`):
21
+ 传统 script 标签方式(UMD 产物通过全局 `docx` 暴露,jszip 需自行引入为全局 `JSZip`):
26
22
 
27
23
  ```html
28
24
  <script src="https://unpkg.com/jszip/dist/jszip.min.js"></script>
29
25
  <script src="node_modules/@apollo-design/docx-preview/dist/docx-preview.min.js"></script>
30
26
  <script>
31
- docx.renderAsync(docData, document.getElementById("container"));
27
+ docx.renderAsync(docData, document.getElementById('container'));
32
28
  </script>
33
29
  ```
34
30
 
35
- ### 2. 在线预览器(GitHub Pages)
36
-
37
- ```
38
- https://apollocmh.github.io/docx-preview/
39
- ```
40
-
41
- 通过 URL 参数加载文档:
42
-
43
- ```
44
- https://apollocmh.github.io/docx-preview/?file=https://example.com/test.docx
45
- ```
46
-
47
- ### 3. 独立预览器(自行部署)
48
-
49
- 从 [GitHub Releases](https://github.com/apollocmh/docx-preview/releases) 下载:
50
-
51
- ```
52
- docx-preview-viewer-{version}.zip
53
- ```
54
-
55
- 解压后部署到任意静态服务器即可——不依赖 npm、不需要构建工具、无第三方 CDN,通过 iframe 或浏览器直接使用:
56
-
57
- ```html
58
- <iframe src="/viewer.html?file=https://example.com/document.docx"></iframe>
59
- <!-- 可选初始缩放: 适应宽度(默认) / 自然尺寸百分比 -->
60
- <iframe src="/viewer.html?file=...&scale=fit | 75 | 0.75"></iframe>
61
- <!-- 可选初始缩略图侧栏: 显示(默认) / 隐藏 -->
62
- <iframe src="/viewer.html?file=...&thumbs=1 | 0"></iframe>
63
- <!-- 可选显式文档名(下载文件名 + 标题),用于媒体库等不透明地址 -->
64
- <iframe src="/viewer.html?file=...&filename=report.docx"></iframe>
65
- ```
66
-
67
- **CORS 注意**:预览器在浏览器内通过 `fetch` 加载 DOCX URL,文件服务器必须允许跨域请求(`Access-Control-Allow-Origin`),或把预览器与文件放在同一来源下。
68
-
69
- 旧式二进制文档(老 `.doc`,以及 `.wps`——即便新版 WPS Office 默认仍保存为 OLE2 二进制)会被预先识别并给出"另存为 .docx"的明确提示;只有 OOXML 包可以渲染。
70
-
71
- 预览器会把文档重排为真实分页。字体按三级解析:.docx 内嵌字体 → viewer.html 旁的 `fonts.json` 清单 → 系统字体。清单是注入为 `@font-face` 的 webfont 定义数组:
72
-
73
- ```json
74
- [
75
- { "name": "SimSun", "src": "url(fonts/simsun.woff2) format(\"woff2\")" },
76
- { "name": "宋体", "src": "url(fonts/simsun.woff2) format(\"woff2\")" }
77
- ]
78
- ```
79
-
80
- 注意 `name` 必须与文档内部使用的字体族名一致(中文文档常直接引用中文族名,如 `宋体`/`黑体`),别名需列为单独条目。`fonts.json` 缺失或无效时静默忽略。
81
-
82
- ## API
83
-
84
- ```ts
85
- // 渲染文档到指定元素
86
- renderAsync(
87
- document: Blob | ArrayBuffer | Uint8Array, // JSZip.loadAsync 支持的任意类型
88
- bodyContainer: HTMLElement, // 渲染文档内容的元素
89
- styleContainer: HTMLElement, // 渲染样式/编号/字体的元素,为 null 时用 bodyContainer
90
- options: {
91
- className: string = "docx", // 默认样式与文档样式类的类名/前缀
92
- inWrapper: boolean = true, // 在文档内容外渲染 wrapper
93
- hideWrapperOnPrint: boolean = false, // 打印时禁用 wrapper 样式
94
- ignoreWidth: boolean = false, // 禁用页面宽度渲染
95
- ignoreHeight: boolean = false, // 禁用页面高度渲染
96
- ignoreFonts: boolean = false, // 禁用字体渲染
97
- breakPages: boolean = true, // 在分页符处分页
98
- paginate: boolean = false, // 渲染后把内容重排进页面尺寸的 section(表格按行、段落按行拆分);测量前会等待 webfont 就绪
99
- fonts: [{ name: string, src: string, weight?: string|number, style?: string }], // 渲染前注入为 @font-face 的外部 webfont
100
- ignoreLastRenderedPageBreak: boolean = true, // 禁用 lastRenderedPageBreak 元素分页
101
- experimental: boolean = false, // 启用实验特性(制表位计算)
102
- trimXmlDeclaration: boolean = true, // 解析前移除 xml 声明
103
- useBase64URL: boolean = false, // 图片/字体等转为 base64 URL,否则用 URL.createObjectURL
104
- renderChanges: false, // 实验性渲染文档修订(插入/删除)
105
- renderHeaders: true, // 渲染页眉
106
- renderFooters: true, // 渲染页脚
107
- renderFootnotes: true, // 渲染脚注
108
- renderEndnotes: true, // 渲染尾注
109
- renderComments: false, // 实验性渲染批注
110
- renderAltChunks: true, // 渲染 altChunks(html 部件)
111
- debug: boolean = false, // 额外日志
112
- h: ({ ns, tagName, className, style, children, ...props } | Node | string): Node, // 实验性 HTML 渲染钩子,默认实现见 defaultOptions.h
113
- }): Promise<WordDocument>
114
-
115
- defaultOptions: Options; // 默认选项
116
-
117
- /// ==== 实验性 / 内部 API ====
118
- // 可用于渲染前修改文档;renderAsync = parseAsync + renderDocument
119
-
120
- // 解析文档,返回内部文档对象
121
- parseAsync(
122
- document: Blob | ArrayBuffer | Uint8Array,
123
- options: Options
124
- ): Promise<WordDocument>
125
-
126
- // 渲染内部文档对象,返回节点列表
127
- renderDocument(
128
- wordDocument: WordDocument,
129
- options: Options
130
- ): Promise<Node[]>
131
- ```
132
-
133
- ## 分页说明
134
-
135
- 库在以下情况分页:
136
-
137
- - 用户手动插入分页符 `<w:br w:type="page"/>`
138
- - 编辑器应用(如 MS Word)插入的 `<w:lastRenderedPageBreak/>`(需把 `ignoreLastRenderedPageBreak` 设为 `false`)
139
- - 段落页面设置变化(如纵向改横向)
140
-
141
- `paginate: true` 则在渲染后按真实版式重排分页(预览器默认启用)。
142
-
143
- ## 稳定性
144
-
145
- 只有 **renderAsync** 是稳定 API,定义不会变更。解析与渲染的内部实现随时可能调整。
31
+ ## 选项亮点
146
32
 
147
- ## Credits
33
+ - `paginate: true` — 渲染后按真实版式重排分页(表格按行、段落按行拆分)
34
+ - `fonts: [{ name, src, weight?, style? }]` — 注入外部 webfont(公文常用方正字体可通过此注册)
35
+ - `breakPages` / `renderHeaders` / `renderFooters` / `renderFootnotes` 等完整选项见仓库 README
148
36
 
149
- This project is a heavily modified fork of [docxjs](https://github.com/VolodymyrBaydalka/docxjs) by **Volodymyr Baydalka**, licensed under the Apache License 2.0. Thanks to the original author for the excellent foundation.
37
+ ## 相关资源
150
38
 
151
- Subsequent development (pagination engine, per-script font cascade, floating-table anchoring, and the standalone viewer) by **apollocmh**.
39
+ - 在线预览器:https://apollocmh.github.io/docx-preview/
40
+ - 独立部署预览器(viewer zip)与完整文档:https://github.com/apollocmh/docx-preview
152
41
 
153
42
  ## License
154
43
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@apollo-design/docx-preview",
3
- "version": "0.6.3",
3
+ "version": "0.8.0",
4
4
  "description": "Render DOCX documents as semantic HTML, with a standalone iframe-embeddable viewer",
5
5
  "keywords": [
6
6
  "word",
@@ -13,7 +13,8 @@
13
13
  "author": "apollocmh",
14
14
  "repository": {
15
15
  "type": "git",
16
- "url": "git+https://github.com/apollocmh/docx-preview.git"
16
+ "url": "git+https://github.com/apollocmh/docx-preview.git",
17
+ "directory": "packages/docx-preview"
17
18
  },
18
19
  "homepage": "https://apollocmh.github.io/docx-preview/",
19
20
  "bugs": "https://github.com/apollocmh/docx-preview/issues",
@@ -37,7 +38,6 @@
37
38
  "engines": {
38
39
  "node": "^20.19.0 || >=22.12.0"
39
40
  },
40
- "packageManager": "pnpm@11.0.9",
41
41
  "publishConfig": {
42
42
  "access": "public",
43
43
  "registry": "https://registry.npmjs.org/"
@@ -45,17 +45,8 @@
45
45
  "dependencies": {
46
46
  "jszip": ">=3.0.0"
47
47
  },
48
- "devDependencies": {
49
- "@microsoft/api-extractor": "^7.59.0",
50
- "terser": "^5.51.2",
51
- "typescript": "^5.0.3",
52
- "vite": "^8.2.2",
53
- "vite-plugin-dts": "^5.1.0"
54
- },
55
48
  "scripts": {
56
- "dev": "vite",
57
49
  "build": "node scripts/build-lib.mjs",
58
- "build:viewer": "vite build",
59
50
  "test": "node scripts/build-lib.mjs && node scripts/check-dist.mjs",
60
51
  "watch": "node scripts/build-lib.mjs --watch"
61
52
  }