|
|
@@ -7,7 +7,7 @@ import * as path from 'path';
|
|
|
import * as os from 'os';
|
|
|
import PizZip from 'pizzip';
|
|
|
import Docxtemplater from 'docxtemplater';
|
|
|
-import * as JSZip from 'jszip';
|
|
|
+// import * as JSZip from 'jszip';
|
|
|
import { MinioService } from '@/server/modules/files/minio.service';
|
|
|
|
|
|
export interface DocumentConversionOptions {
|
|
|
@@ -20,7 +20,7 @@ export class DocumentService {
|
|
|
private minioService: MinioService;
|
|
|
private minioAvailable: boolean = false;
|
|
|
|
|
|
- constructor(private dataSource: DataSource) {
|
|
|
+ constructor() {
|
|
|
this.tempDir = path.join(os.tmpdir(), 'document-processing');
|
|
|
this.minioService = new MinioService();
|
|
|
this.initializeMinio();
|
|
|
@@ -62,7 +62,7 @@ export class DocumentService {
|
|
|
// 方法1: 使用mammoth将Word转HTML,然后HTML转PDF
|
|
|
const tempDir = await this.ensureTempDir();
|
|
|
const tempHtmlPath = path.join(tempDir, `${filename}.html`);
|
|
|
- const tempPdfPath = path.join(tempDir, `${filename}.pdf`);
|
|
|
+ // const tempPdfPath = path.join(tempDir, `${filename}.pdf`);
|
|
|
|
|
|
// 使用mammoth转换Word到HTML
|
|
|
const result = await mammoth.convertToHtml({ buffer: wordBuffer });
|
|
|
@@ -104,7 +104,7 @@ export class DocumentService {
|
|
|
|
|
|
} catch (error) {
|
|
|
console.error('Word转PDF失败:', error);
|
|
|
- throw new Error(`Word文档转换失败: ${error.message}`);
|
|
|
+ throw new Error(`Word文档转换失败: ${error instanceof Error ? error.message : '未知错误'}`);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -113,7 +113,7 @@ export class DocumentService {
|
|
|
*/
|
|
|
async mergePdfs(pdfBuffers: Buffer[]): Promise<Buffer> {
|
|
|
try {
|
|
|
- const merger = new PDFMerger.default();
|
|
|
+ const merger = new PDFMerger();
|
|
|
|
|
|
for (let i = 0; i < pdfBuffers.length; i++) {
|
|
|
await merger.add(pdfBuffers[i]);
|
|
|
@@ -123,7 +123,7 @@ export class DocumentService {
|
|
|
return Buffer.from(mergedPdf);
|
|
|
} catch (error) {
|
|
|
console.error('PDF合并失败:', error);
|
|
|
- throw new Error(`PDF文档合并失败: ${error.message}`);
|
|
|
+ throw new Error(`PDF文档合并失败: ${error instanceof Error ? error.message : '未知错误'}`);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -138,7 +138,7 @@ export class DocumentService {
|
|
|
|
|
|
const tempDir = await this.ensureTempDir();
|
|
|
const tempPdfPath = path.join(tempDir, `${filename}.pdf`);
|
|
|
-
|
|
|
+
|
|
|
// 写入PDF文件
|
|
|
await fs.writeFile(tempPdfPath, pdfBuffer);
|
|
|
|
|
|
@@ -147,7 +147,7 @@ export class DocumentService {
|
|
|
const { convert } = await import('libreoffice-convert');
|
|
|
const extend = '.docx';
|
|
|
|
|
|
- return new Promise((resolve, reject) => {
|
|
|
+ return new Promise((resolve) => {
|
|
|
convert(pdfBuffer, extend, undefined, (err: Error | null, done: Buffer) => {
|
|
|
if (err) {
|
|
|
console.warn('libreoffice-convert转换失败:', err);
|
|
|
@@ -168,7 +168,7 @@ export class DocumentService {
|
|
|
|
|
|
} catch (error) {
|
|
|
console.error('PDF转Word失败:', error);
|
|
|
- throw new Error(`PDF转Word失败: ${error.message}`);
|
|
|
+ throw new Error(`PDF转Word失败: ${error instanceof Error ? error.message : '未知错误'}`);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -268,7 +268,7 @@ export class DocumentService {
|
|
|
}
|
|
|
} catch (error) {
|
|
|
console.error('Word文档合并失败:', error);
|
|
|
- throw new Error(`文档合并失败: ${error.message}`);
|
|
|
+ throw new Error(`文档合并失败: ${error instanceof Error ? error.message : '未知错误'}`);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -317,7 +317,7 @@ export class DocumentService {
|
|
|
return mergedBuffer;
|
|
|
} catch (error) {
|
|
|
console.error('docxtemplater合并错误:', error);
|
|
|
- throw new Error(`文档合并处理失败: ${error.message}`);
|
|
|
+ throw new Error(`文档合并处理失败: ${error instanceof Error ? error.message : '未知错误'}`);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -371,13 +371,14 @@ export class DocumentService {
|
|
|
try {
|
|
|
// 使用mammoth将DOCX转HTML,然后HTML转PDF
|
|
|
const result = await mammoth.convertToHtml({ buffer: docxBuffer });
|
|
|
- const html = result.value;
|
|
|
+ // const html = result.value;
|
|
|
|
|
|
// 使用pdf-lib创建PDF
|
|
|
const pdfDoc = await PDFDocument.create();
|
|
|
const page = pdfDoc.addPage([595, 842]); // A4尺寸
|
|
|
-
|
|
|
+
|
|
|
// 简单文本渲染(实际项目中可能需要更复杂的HTML到PDF转换)
|
|
|
+ // 这里应该使用html内容,但简化实现
|
|
|
page.drawText('合并后的文档内容', {
|
|
|
x: 50,
|
|
|
y: 700,
|
|
|
@@ -388,7 +389,7 @@ export class DocumentService {
|
|
|
return Buffer.from(pdfBytes);
|
|
|
} catch (error) {
|
|
|
console.error('DOCX转PDF失败:', error);
|
|
|
- throw new Error(`DOCX转PDF失败: ${error.message}`);
|
|
|
+ throw new Error(`DOCX转PDF失败: ${error instanceof Error ? error.message : '未知错误'}`);
|
|
|
}
|
|
|
}
|
|
|
}
|