36 lines
1.1 KiB
TypeScript
36 lines
1.1 KiB
TypeScript
import HTMLtoDOCX from '@turbodocx/html-to-docx';
|
|
|
|
export interface DOCXOptions {
|
|
title?: string;
|
|
creator?: string;
|
|
font?: string;
|
|
margins?: { top: number; right: number; bottom: number; left: number };
|
|
lang?: string;
|
|
}
|
|
|
|
export class DOCXConverterService {
|
|
async convert(html: string, options: DOCXOptions = {}): Promise<Buffer> {
|
|
const opts = {
|
|
title: options.title || 'Document',
|
|
creator: options.creator || 'iistwin CRM',
|
|
font: options.font || 'Noto Sans',
|
|
margins: options.margins || { top: 1440, right: 1440, bottom: 1440, left: 1440 },
|
|
lang: options.lang || 'ru-RU',
|
|
};
|
|
|
|
const convertFn = typeof HTMLtoDOCX === 'function' ? HTMLtoDOCX : (HTMLtoDOCX as any).default;
|
|
if (typeof convertFn !== 'function') {
|
|
throw new Error('HTMLtoDOCX converter is not available');
|
|
}
|
|
const result = await convertFn(html, null, opts);
|
|
if (result instanceof ArrayBuffer) {
|
|
return Buffer.from(result);
|
|
}
|
|
if (Buffer.isBuffer(result)) {
|
|
return result;
|
|
}
|
|
// Fallback: некоторые версии возвращают Blob/Buffer
|
|
return Buffer.from(result as any);
|
|
}
|
|
}
|