UNPKG

@neosjs/html-to-pdf

Version:

将html转换为pdf,支持中文,支持图片,支持表格,支持分页,支持水印,支持自定义页眉页脚,支持页码. 解决分页截断问题.

501 lines 12.5 kB
//#region src/constants/index.d.ts /** * 项目常量定义 * 包含默认配置、纸张尺寸、限制条件、样式设置等 */ /** 默认中文字体 */ declare const DEFAULT_FONT = "FZLTXIHJW"; /** 水印重复次数 */ declare const WATERMARK_REPEATS = 6; /** * 默认配置选项 * 用户未指定时使用的默认值 */ declare const DEFAULT_OPTIONS: { /** 目标元素ID(必填) */ readonly elementId: ""; /** 不可分页元素的CSS类名(必填) */ readonly wholeNodeClass: ""; /** 需要忽略的元素的CSS类名 */ readonly ignoreElementsClass: ""; /** 页面方向,默认为纵向 */ readonly orientation: "p"; /** 纸张格式,默认为A4 */ readonly format: "a4"; /** 元素间距(像素),默认为5 */ readonly intervalHeight: 5; /** 是否自动打开PDF,默认为false */ readonly open: false; /** 是否启用性能监控,默认为false */ readonly enablePerformanceMonitor: false; /** 页边距配置(毫米) */ readonly pageMargin: { readonly top: 10; readonly bottom: 10; readonly left: 10; readonly right: 10; }; }; /** * 标准纸张尺寸(毫米) * 包含A系列、Letter、Legal等常用纸张格式 */ declare const PAPER_SIZES: { readonly a0: { readonly width: 841; readonly height: 1189; }; readonly a1: { readonly width: 594; readonly height: 841; }; readonly a2: { readonly width: 420; readonly height: 594; }; readonly a3: { readonly width: 297; readonly height: 420; }; readonly a4: { readonly width: 210; readonly height: 297; }; readonly a5: { readonly width: 148; readonly height: 210; }; readonly a6: { readonly width: 105; readonly height: 148; }; readonly a7: { readonly width: 74; readonly height: 105; }; readonly a8: { readonly width: 52; readonly height: 74; }; readonly letter: { readonly width: 215.9; readonly height: 279.4; }; readonly legal: { readonly width: 215.9; readonly height: 355.6; }; readonly tabloid: { readonly width: 279.4; readonly height: 431.8; }; readonly ledger: { readonly width: 431.8; readonly height: 279.4; }; }; /** * 系统限制条件 * 用于防止内存溢出和性能问题 */ declare const LIMITS: { /** 最小页面高度(像素) */ readonly MIN_PAGE_HEIGHT: 50; /** 最大Canvas尺寸(像素) */ readonly MAX_CANVAS_SIZE: 16384; /** 最大内存使用量(MB) */ readonly MAX_MEMORY_USAGE: 512; /** 最大PDF文件大小(MB) */ readonly MAX_PDF_SIZE: 100; }; /** * 默认样式设置 * 用于确保PDF输出的视觉效果一致性 */ declare const STYLES: { /** 默认字体族 */ readonly DEFAULT_FONT_FAMILY: "Arial, sans-serif"; /** 默认字体大小 */ readonly DEFAULT_FONT_SIZE: "14px"; /** 默认行高 */ readonly DEFAULT_LINE_HEIGHT: "1.5"; /** 默认背景颜色 */ readonly DEFAULT_BACKGROUND_COLOR: "#ffffff"; /** 默认文本颜色 */ readonly DEFAULT_TEXT_COLOR: "#000000"; /** 默认页眉字体大小 */ readonly DEFAULT_HEADER_FONT_SIZE: 10; /** 默认页眉颜色 */ readonly DEFAULT_HEADER_COLOR: "#ddd"; }; /** * 错误代码定义 * 用于统一错误处理和用户友好的错误提示 */ declare const ERROR_CODES: { /** 元素未找到 */ readonly ELEMENT_NOT_FOUND: "ELEMENT_NOT_FOUND"; /** 元素为空 */ readonly EMPTY_ELEMENT: "EMPTY_ELEMENT"; /** Canvas生成失败 */ readonly CANVAS_GENERATION_FAILED: "CANVAS_GENERATION_FAILED"; /** PDF创建失败 */ readonly PDF_CREATION_FAILED: "PDF_CREATION_FAILED"; /** 内存错误 */ readonly MEMORY_ERROR: "MEMORY_ERROR"; /** 配置选项无效 */ readonly INVALID_OPTIONS: "INVALID_OPTIONS"; }; /** * 性能监控阈值 * 用于识别潜在的性能问题 */ declare const PERFORMANCE_THRESHOLDS: { /** 慢操作阈值(毫秒) */ readonly SLOW_OPERATION: 5000; /** 内存警告阈值(MB) */ readonly MEMORY_WARNING: 100; /** Canvas尺寸警告阈值(像素) */ readonly CANVAS_SIZE_WARNING: 8192; }; //#endregion //#region src/types/index.d.ts /** * 纸张格式类型 */ type PaperFormat = 'a0' | 'a1' | 'a2' | 'a3' | 'a4' | 'a5' | 'a6' | 'a7' | 'a8' | 'letter' | 'legal' | 'tabloid' | 'ledger'; /** * 页面方向类型 */ type Orientation = 'p' | 'l' | 'portrait' | 'landscape'; /** * 文本对齐方式 */ type Alignment = 'left' | 'center' | 'right'; /** * 纸张尺寸接口 */ interface PaperSize { /** 宽度(毫米) */ width: number; /** 高度(毫米) */ height: number; } /** * 页眉页脚配置接口 */ interface HeaderFooter { /** 内容文本,支持数组形式的多行文本 */ content: string | string[]; /** 对齐方式,默认为 center */ align?: Alignment; /** 文本颜色,默认为 #333 */ color?: string; /** 字体大小,默认为 10 */ fontSize?: number; /** 字体粗细,默认为 400 */ fontWeight?: number; /** 是否显示边框,默认为 false */ border?: boolean; } /** * 页边距配置接口 */ interface PageMargin { /** 上边距(毫米),默认为 10 */ top?: number; /** 下边距(毫米),默认为 10 */ bottom?: number; /** 左边距(毫米),默认为 10 */ left?: number; /** 右边距(毫米),默认为 10 */ right?: number; } /** * 水印配置接口 */ interface Watermark { /** 水印内容文本 */ content?: string; /** 水印颜色,默认为 #ddd */ color?: string; /** 水印字体大小,默认为 14 */ size?: number; /** 水印旋转角度,默认为 45 */ angle?: number; /** 水印透明度,默认为 0.2 */ opacity?: number; } /** * html2canvas配置选项 */ interface Html2CanvasOptions { /** 缩放比例,默认为 1.5 */ scale?: number; /** 图片质量(0-1),默认为 0.75 */ quality?: number; /** 是否使用CORS,默认为 true */ useCORS?: boolean; /** 是否允许跨域图片,默认为 true */ allowTaint?: boolean; /** 背景颜色,默认为 #fff */ backgroundColor?: string; /** 是否开启日志,默认为 false */ logging?: boolean; } /** * PDF文档创建配置 */ interface PdfDocumentConfig { /** Canvas元素 */ canvas: HTMLCanvasElement; /** 内容宽度(毫米) */ contentWidth: number; /** 内容高度(毫米) */ contentHeight: number; /** 页边距配置 */ margins: { left: number; top: number; bottom: number; right: number; }; /** PDF基本配置 */ pdf: { orientation: Orientation; format: PaperFormat; quality: number; }; /** 水印配置 */ watermark?: Watermark; /** 页眉页脚配置 */ headerFooter?: { header?: HeaderFooter; footer?: HeaderFooter; }; /** 文件操作配置 */ file?: { fileName?: string; open?: boolean; }; } /** * 不可分页元素处理配置 */ interface NonBreakableElementsConfig { /** 临时容器元素 */ tempContainer: HTMLElement; /** 页面高度(像素) */ pageHeight: number; /** 不可分页元素的CSS类名 */ wholeNodeClass: string; /** 元素间距(像素),默认为 5 */ intervalHeight?: number; } /** * 主要配置选项接口 */ interface Options { /** 需要转换的DOM元素ID或元素本身 */ elementId: string | HTMLElement; /** 不可分页元素的CSS类名 */ wholeNodeClass: string; /** 需要忽略的元素的CSS类名 */ ignoreElementsClass?: string; /** 元素间距(像素),默认为 5 */ intervalHeight?: number; /** 是否启用性能监控,默认为 false */ enablePerformanceMonitor?: boolean; /** 输出文件名(不含扩展名),默认为 "导出" */ fileName?: string; /** 纸张格式,默认为 a4 */ format?: PaperFormat; /** 页面方向,默认为 portrait */ orientation?: Orientation; /** html2canvas配置选项 */ html2canvas?: Html2CanvasOptions; /** 页眉配置 */ header?: HeaderFooter; /** 页脚配置 */ footer?: HeaderFooter; /** 页边距配置 */ pageMargin?: PageMargin; /** 水印配置 */ watermark?: Watermark; /** 是否自动打开PDF文件,默认为 false */ open?: boolean; } /** * Canvas转换结果 */ interface CanvasResult { /** Canvas宽度(像素) */ width: number; /** Canvas高度(像素) */ height: number; /** Canvas数据URL */ canvasData: string; /** Canvas元素 */ canvas: HTMLCanvasElement; } /** * 页面信息 */ interface PageInfo { /** 当前页码 */ currentPage: number; /** 总页数 */ totalPages: number; /** 当前位置 */ position: number; /** HTML内容高度 */ htmlHeight: number; } /** * 页面尺寸信息 */ interface PageDimensions { /** 上边距(毫米) */ marginTop: number; /** 下边距(毫米) */ marginBottom: number; /** 左边距(毫米) */ marginLeft: number; /** 右边距(毫米) */ marginRight: number; /** 内容宽度(毫米) */ contentWidth: number; /** 内容高度(毫米) */ contentHeight: number; /** 页面宽度(像素) */ pageWidth: number; /** 页面高度(像素) */ pageHeight: number; } /** * 性能监控报告 */ interface PerformanceReport { /** 总耗时(毫秒) */ totalTime: number; /** 各步骤详情 */ steps: Array<{ step: string; duration: number; percentage: number; memory?: number; }>; /** 平均耗时(毫秒) */ averageTime: number; /** 最慢步骤名称 */ slowestStep: string; /** 最快步骤名称 */ fastestStep: string; } /** * 转换错误类型 */ interface ConversionError extends Error { /** 错误代码 */ code: 'ELEMENT_NOT_FOUND' | 'EMPTY_ELEMENT' | 'CANVAS_GENERATION_FAILED' | 'PDF_CREATION_FAILED' | 'MEMORY_ERROR' | 'INVALID_OPTIONS'; /** 错误详情 */ details?: any; } //#endregion //#region src/core/converter.d.ts /** * HTML转PDF转换器 * 支持中文、图片、表格、分页、水印、自定义页眉页脚、页码等功能 * 解决分页截断问题,提供高性能的PDF生成能力 */ declare class HtmlToPdf { /** 用户配置选项 */ private readonly options; /** 计算后的纸张尺寸 */ private readonly paperSize; /** * 构造函数 * @param options 用户配置选项 */ constructor(options: Options); /** * 合并用户配置和默认配置 * @param userOptions 用户配置 * @returns 合并后的配置 */ private mergeOptions; /** * 根据方向和格式计算纸张尺寸 * @returns 计算后的纸张尺寸 */ private calculatePaperSize; /** * 计算页面尺寸和边距信息 * @param domElement DOM元素 * @returns 页面尺寸信息 */ private calculatePageDimensions; /** * 获取指定格式的纸张尺寸 * @param format 纸张格式 * @returns 纸张尺寸 */ getPaperSize: (format: PaperFormat) => PaperSize; /** * 生成PDF文档 * @returns Promise<Blob> PDF文件的Blob对象 */ generatePDF: () => Promise<Blob>; /** * 开始计时 * @param step 步骤名称 */ startTimer: (step: string) => void; /** * 结束计时 * @param step 步骤名称 * @param isFinish 是否为最终步骤 */ endTimer: (step: string, isFinish?: boolean) => number; /** * 获取性能报告 * @returns 性能报告对象 */ getReport: () => { totalTime: number; steps: Array<{ step: string; duration: number; percentage: number; memory?: number; }>; averageTime: number; slowestStep: string; fastestStep: string; }; /** * 打印性能摘要 */ printSummary: () => void; /** * 获取性能日志 * @returns 性能日志数组 */ getLogs: () => { step: string; duration: number; timestamp: number; memory?: number; }[]; /** * 导出性能数据 * @returns JSON格式的性能数据 */ exportData: () => string; /** * 重置性能监控器 */ reset: () => void; } //#endregion export { Alignment, CanvasResult, ConversionError, DEFAULT_FONT, DEFAULT_OPTIONS, ERROR_CODES, HeaderFooter, Html2CanvasOptions, LIMITS, NonBreakableElementsConfig, Options, Orientation, PAPER_SIZES, PERFORMANCE_THRESHOLDS, PageDimensions, PageInfo, PageMargin, PaperFormat, PaperSize, PdfDocumentConfig, PerformanceReport, STYLES, WATERMARK_REPEATS, Watermark, HtmlToPdf as default };