diff --git a/src/Plugins/pdf-input-plugin.ts b/src/Plugins/pdf-input-plugin.ts new file mode 100644 index 0000000..6e4888a --- /dev/null +++ b/src/Plugins/pdf-input-plugin.ts @@ -0,0 +1,33 @@ +import * as path from 'path'; +import { PluginOutput } from './pdf-plugin.interfaces'; +import * as pdfPoppler from 'pdf-poppler'; + +export class PdfInputPlugin { + public async transformPdfToImage(pdfFilePath: string): Promise { + const outDir = path.dirname(pdfFilePath); + const outPrefix = path.basename(pdfFilePath, path.extname(pdfFilePath)); + + const pdfImageOptions = { + format: 'jpeg', + out_dir: outDir, + out_prefix: outPrefix, + page: null, + }; + + try { + const result = await pdfPoppler.convert(pdfFilePath, pdfImageOptions); + + console.log('Successfully converted'); + console.log(result); + + const images = Array.isArray(result) + ? result.map((imagePath: string) => ({ url: imagePath })) + : [{ url: result }]; + + return { images: images }; + } catch (error) { + console.error('Error converting PDF to image:', error); + throw new Error('PDF to image conversion failed'); + } + } +} diff --git a/src/Plugins/pdf-output-plugin.ts b/src/Plugins/pdf-output-plugin.ts new file mode 100644 index 0000000..551db79 --- /dev/null +++ b/src/Plugins/pdf-output-plugin.ts @@ -0,0 +1,67 @@ +import { TDocumentDefinitions } from 'pdfmake/interfaces'; +import * as pdfMake from 'pdfmake/build/pdfmake'; +import * as pdfFonts from 'pdfmake/build/vfs_fonts'; +import * as fs from 'fs'; +import * as path from 'path'; +import axios from 'axios'; +import { PluginOutput } from './pdf-plugin.interfaces'; + +(pdfMake as any).vfs = pdfFonts.pdfMake.vfs; + +export class PdfOutputPlugin { + public async generateDoc(outputType: string): Promise { + if (outputType.toUpperCase() === 'PDF') { + const pdfContent: TDocumentDefinitions = { + content: [ + { + text: 'Hello PDF generated successfully!', + fontSize: 16, + alignment: 'center', + margin: [0, 100, 0, 0], + }, + ], + }; + + const pdfDoc = pdfMake.createPdf(pdfContent); + const pdfFilePath = path.join('.Plugins/generatedDocument.pdf'); + + return new Promise((resolve, reject) => { + pdfDoc.getBuffer((buffer) => { + fs.writeFileSync(pdfFilePath, buffer); + console.log('PDF generated successfully'); + resolve({ file: 'generatedDocument.pdf' }); + }); + }); + } else { + throw new Error('Unsupported output type'); + } + } + + public async createDefaultPdf(): Promise { + const pdfContent: TDocumentDefinitions = { + content: [ + { + text: 'Hello, this is a default PDF document!', + fontSize: 16, + alignment: 'center', + margin: [0, 100, 0, 0], + }, + ], + }; + + const pdfDoc = pdfMake.createPdf(pdfContent); + const pdfFilePath = './defaultDocument.pdf'; + + return new Promise((resolve, reject) => { + pdfDoc.getBuffer((buffer) => { + fs.writeFileSync(pdfFilePath, buffer); + console.log('Default PDF generated successfully'); + + resolve({ + file: 'defaultDocument.pdf', + url: 'http://your-domain.com/defaultDocument.pdf', // Replace with the actual PDF URL + }); + }); + }); + } +} diff --git a/src/Plugins/pdf-plugin.interfaces.ts b/src/Plugins/pdf-plugin.interfaces.ts new file mode 100644 index 0000000..843ecfa --- /dev/null +++ b/src/Plugins/pdf-plugin.interfaces.ts @@ -0,0 +1,25 @@ +import { TDocumentDefinitions } from 'pdfmake/interfaces'; + +export interface PluginOutput { + file?: string; + url?: string; + images?: { url: string }[]; +} + +export interface Plugin { + generateDoc(outputType: string): Promise; + validateTemplate(template: string): boolean; + getName(): string; + getSupportedOutputs(): string[]; + getSupportedInputs(): string[]; + transformToPDFMake( + inputType: string, + outputType: string, + ): Promise; + transformPdfToDocsx(pdfFilePath: string): Promise; + transformPdfToImage(pdfFilePath: string): Promise; + transformPdfToExcalidraw(inputFile: string): PluginOutput; + transformPdfToDrawio(inputFile: string): PluginOutput; + transformPdfMakeToMermaid(pdfMakeContent: TDocumentDefinitions): string; + isSupportedConversion(inputType: string, outputType: string): boolean; +} diff --git a/src/Plugins/pdf-plugin.service.ts b/src/Plugins/pdf-plugin.service.ts new file mode 100644 index 0000000..1d807aa --- /dev/null +++ b/src/Plugins/pdf-plugin.service.ts @@ -0,0 +1,88 @@ +import { Injectable } from '@nestjs/common'; +import { Plugin, PluginOutput } from './pdf-plugin.interfaces'; +import * as path from 'path'; +import * as fs from 'fs'; +import { TDocumentDefinitions } from 'pdfmake/interfaces'; +import { PdfInputPlugin } from './pdf-input-plugin'; // Import PdfInputPlugin + +@Injectable() +export class PluginService implements Plugin { + constructor(private readonly pdfInputPlugin: PdfInputPlugin) {} // Use PdfInputPlugin here + + validateTemplate(template: string): boolean { + if (template === 'PDF') { + return true; + } else { + return false; + } + } + + getName(): string { + return 'PDF Plugin'; + } + + getSupportedOutputs(): string[] { + return ['PDF', 'Image']; + } + + getSupportedInputs(): string[] { + return ['PDF']; + } + + public async transformPdfToImage(pdfFilePath: string): Promise { + const outDir = path.dirname(pdfFilePath); + const outPrefix = path.basename(pdfFilePath, path.extname(pdfFilePath)); + + const pdfImageOptions = { + format: 'jpeg', + out_dir: outDir, + out_prefix: outPrefix, + page: null, + }; + + try { + await this.pdfInputPlugin.transformPdfToImage(pdfFilePath); + + console.log('Successfully converted'); + + const images = fs.readdirSync(outDir).map((fileName) => ({ + url: path.join(outDir, fileName), + })); + + return { images: images }; + } catch (error) { + console.error('Error converting PDF to image:', error); + throw new Error('PDF to image conversion failed'); + } + } + + transformToPDFMake( + inputType: string, + outputType: string, + ): Promise { + throw new Error('Method not implemented.'); + } + + transformPdfToDocsx(pdfFilePath: string): Promise { + throw new Error('Method not implemented.'); + } + + transformPdfToExcalidraw(inputFile: string): PluginOutput { + throw new Error('Method not implemented.'); + } + + transformPdfToDrawio(inputFile: string): PluginOutput { + throw new Error('Method not implemented.'); + } + + transformPdfMakeToMermaid(pdfMakeContent: TDocumentDefinitions): string { + throw new Error('Method not implemented.'); + } + + isSupportedConversion(inputType: string, outputType: string): boolean { + return true; + } + generateDoc(outputType: string): Promise { + throw new Error('Method not implemented.'); + } +} diff --git a/src/Plugins/pdf-poppler.d.ts b/src/Plugins/pdf-poppler.d.ts new file mode 100644 index 0000000..8ebaaf9 --- /dev/null +++ b/src/Plugins/pdf-poppler.d.ts @@ -0,0 +1,4 @@ +declare module 'pdf-poppler' { + export function info(filePath: string): Promise; + export function convert(filePath: string, options: any): Promise; +} diff --git a/src/Plugins/plugin.controller.ts b/src/Plugins/plugin.controller.ts new file mode 100644 index 0000000..ba2f509 --- /dev/null +++ b/src/Plugins/plugin.controller.ts @@ -0,0 +1,68 @@ +import { Controller, Post, Get, Param, Dependencies } from '@nestjs/common'; +import { PluginService } from './pdf-plugin.service'; +import { PluginOutput } from './pdf-plugin.interfaces'; +import { PdfOutputPlugin } from './pdf-output-plugin'; +import { PdfInputPlugin } from './pdf-input-plugin'; + +@Controller('plugin') +@Dependencies(PluginService) +export class PluginController { + private pdfOutputPlugin!: PdfOutputPlugin; + private pdfInputPlugin!: PdfInputPlugin; + + constructor(private readonly pluginService: PluginService) {} + + onModuleInit() { + this.pdfOutputPlugin = new PdfOutputPlugin(); + this.pdfInputPlugin = new PdfInputPlugin(); + } + + @Post('generate-doc/:outputType') + async generateDocument( + @Param('outputType') outputType: string, + ): Promise { + try { + return this.pdfOutputPlugin.generateDoc(outputType); + } catch (error: any) { + console.error('Error generating document:', error.message); + throw new Error('Failed to generate document'); + } + } + + @Get() + getPluginStatus(): string { + return 'Plugin is running!'; + } + + @Get('/pdf-to-image') + async convertPdfToImage(): Promise<{ images?: { url: string }[] }> { + const pdfFilePath = './generatedDocument.pdf'; + try { + const pluginOutput = await this.pdfInputPlugin.transformPdfToImage( + pdfFilePath, + ); + + if (pluginOutput.images) { + const images = pluginOutput.images; + images.forEach((image: { url: string }) => { + console.log('Image URL:', image.url); + }); + } + + return { images: pluginOutput.images }; + } catch (error) { + console.error('Error converting PDF to image:', error); + throw new Error('PDF to image conversion failed'); + } + } + + @Post('create-default-pdf') + async createDefaultPdf(): Promise { + try { + return this.pdfOutputPlugin.createDefaultPdf(); + } catch (error: any) { + console.error('Error creating default PDF:', error.message); + throw new Error('Failed to create default PDF'); + } + } +} diff --git a/src/Plugins/plugin.module.ts b/src/Plugins/plugin.module.ts new file mode 100644 index 0000000..d2969f8 --- /dev/null +++ b/src/Plugins/plugin.module.ts @@ -0,0 +1,10 @@ +import { Module } from '@nestjs/common'; +import { PluginController } from './plugin.controller'; +import { PluginService } from './pdf-plugin.service'; +import { PdfInputPlugin } from './pdf-input-plugin'; + +@Module({ + controllers: [PluginController], + providers: [PluginService, PdfInputPlugin], +}) +export class PluginModule {}