-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathdocsx-input-plugin.ts
34 lines (29 loc) · 1.17 KB
/
docsx-input-plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import * as path from 'path';
import * as mammoth from 'mammoth';
import * as puppeteer from 'puppeteer';
import { PluginOutput } from './pdf-plugin.interfaces';
export class DocxInputPlugin {
public async convertDocxToPdf(docxFilePath: string): Promise<PluginOutput> {
try {
const outputPdfPath = path.join(__dirname, './generatedPdfDocument.pdf');
const htmlContent = await this.convertDocxToHtml(docxFilePath);
await this.htmlToPdf(htmlContent, outputPdfPath);
console.log('PDF file generated successfully');
return { file: outputPdfPath };
} catch (error) {
console.error('Error generating PDF:', error);
throw new Error('Failed to convert DOCX to PDF');
}
}
private async convertDocxToHtml(docxFilePath: string): Promise<string> {
const result = await mammoth.convertToHtml({ path: docxFilePath });
return result.value;
}
private async htmlToPdf(htmlContent: string, pdfPath: string): Promise<void> {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent(htmlContent);
await page.pdf({ path: pdfPath, format: 'A4' });
await browser.close();
}
}