html-to-word-js
Version:
一个将HTML转换为DOCX文档的TypeScript库,支持标题、段落、文本格式化、列表等常见HTML元素
224 lines (223 loc) • 5.27 kB
TypeScript
/// <reference types="node" />
/// <reference types="node" />
import { Document, Paragraph, Table } from 'docx';
export interface HtmlToDocxOptions {
/**
* 页面方向
*/
orientation?: 'portrait' | 'landscape';
/**
* 页边距 (以厘米为单位)
*/
margins?: {
top?: number;
right?: number;
bottom?: number;
left?: number;
};
/**
* 图片最大宽度 (像素)
*/
maxImageWidth?: number;
/**
* 图片最大高度 (像素)
*/
maxImageHeight?: number;
/**
* 文档属性
*/
properties?: {
/**
* 创建者
*/
creator?: string;
/**
* 描述
*/
description?: string;
/**
* 标题
*/
title?: string;
/**
* 主题
*/
subject?: string;
/**
* 关键词
*/
keywords?: string;
/**
* 修订版本
*/
revision?: number;
/**
* 最后修改者
*/
lastModifiedBy?: string;
};
/**
* 页眉设置
*/
header?: {
/**
* 页眉内容 (HTML字符串)
*/
content?: string;
/**
* 页眉样式
*/
style?: {
/**
* 字体大小 (半点)
*/
fontSize?: number;
/**
* 字体名称
*/
fontFamily?: string;
/**
* 字体颜色 (hex)
*/
color?: string;
/**
* 是否粗体
*/
bold?: boolean;
/**
* 是否斜体
*/
italic?: boolean;
/**
* 文本对齐方式
*/
alignment?: 'left' | 'center' | 'right';
};
};
/**
* 页脚设置
*/
footer?: {
/**
* 页脚内容 (HTML字符串)
*/
content?: string;
/**
* 页脚样式
*/
style?: {
/**
* 字体大小 (半点)
*/
fontSize?: number;
/**
* 字体名称
*/
fontFamily?: string;
/**
* 字体颜色 (hex)
*/
color?: string;
/**
* 是否粗体
*/
bold?: boolean;
/**
* 是否斜体
*/
italic?: boolean;
/**
* 文本对齐方式
*/
alignment?: 'left' | 'center' | 'right';
};
};
}
/**
* CSS 规则接口
*/
export interface CSSRule {
selector: string;
properties: Record<string, string>;
}
/**
* CSS 规则集合
*/
export interface CSSRuleSet {
[selector: string]: Record<string, string>;
}
export interface ConversionResult {
/**
* 生成的 DOCX 文档对象
*/
document: Document;
/**
* 转换警告信息
*/
warnings: string[];
}
/**
* 文档元素类型(段落或表格)
*/
export type DocumentElement = Paragraph | Table;
export interface ParsedBorderStyle {
width?: number;
style?: 'single' | 'double' | 'dotted' | 'dashed' | 'none';
color?: string;
}
export interface ParsedStyle {
color?: string;
fontSize?: number;
fontFamily?: string;
bold?: boolean;
italic?: boolean;
underline?: boolean;
strikethrough?: boolean;
indent?: number;
characterSpacing?: number;
textAlign?: 'left' | 'center' | 'right' | 'justify';
lineHeight?: {
line: number;
lineRule: string;
};
backgroundColor?: string;
border?: ParsedBorderStyle;
borderTop?: ParsedBorderStyle;
borderRight?: ParsedBorderStyle;
borderBottom?: ParsedBorderStyle;
borderLeft?: ParsedBorderStyle;
marginTop?: number;
marginRight?: number;
marginBottom?: number;
marginLeft?: number;
href?: string;
}
export interface ImageInfo {
data: Buffer | Uint8Array;
width: number;
height: number;
type: string;
}
/**
* 将 HTML 字符串转换为 DOCX 文档
* @param html - HTML 字符串
* @param options - 转换选项
* @returns Promise<ConversionResult>
*/
export declare function htmlToDocx(html: string, options?: HtmlToDocxOptions): Promise<ConversionResult>;
/**
* 将 DOCX 文档转换为 Buffer (Node.js) 或 Uint8Array (Browser)
* @param document - DOCX 文档对象
* @returns Promise<Buffer | Uint8Array>
*/
export declare function docxToBuffer(document: Document): Promise<Buffer | Uint8Array>;
/**
* 简便方法:直接从 HTML 生成 DOCX Buffer
* @param html - HTML 字符串
* @param options - 转换选项
* @returns Promise<{ buffer: Buffer | Uint8Array; warnings: string[] }>
*/
export declare function convertHtmlToDocxBuffer(html: string, options?: HtmlToDocxOptions): Promise<{
buffer: Buffer | Uint8Array;
warnings: string[];
}>;