fix(web-admin): resolve html-to-docx default import interop for Word export

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
zlwuandCursor committed 2026-09-05 14:52:03 +08:00
1 parent 591736f11f
commit deefdf7bb4
1 file changed
+12 -1
@@ -1,9 +1,16 @@
/** 合同打印弹窗:HTML → DOCX 预处理与生成 */
import HTMLtoDOCX from '@turbodocx/html-to-docx'
import htmlToDocxModule from '@turbodocx/html-to-docx'
const DOCX_MIME = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
/** CJS/ESM 互操作:Vite/浏览器多为函数,tsx/Node 偶发 `{ default: fn }` */
const HTMLtoDOCX = (
typeof htmlToDocxModule === 'function'
? htmlToDocxModule
: (htmlToDocxModule as { default?: unknown })?.default
) as (htmlString: string, headerHTMLString?: string | null, documentOptions?: Record<string, unknown>, footerHTMLString?: string | null) => Promise<Blob | ArrayBuffer>
/**
* 将已预处理的合同 HTML 转为 DOCX Blob(浏览器下载用)。
* 调用方须先执行 prepareContractHtmlForDocx。
@@ -12,6 +19,10 @@ export async function generateContractDocxBlob(
preparedHtml: string,
title?: string
): Promise<Blob> {
if (typeof HTMLtoDOCX !== 'function') {
throw new Error('html-to-docx 加载失败:未获得可调用的转换函数')
}
const fullHtml = `<!DOCTYPE html><html><head><meta charset="UTF-8" /></head><body>${preparedHtml}</body></html>`
const result = await HTMLtoDOCX(fullHtml, null, {
orientation: 'portrait',