diff --git a/src/Plugins /docsx-output-plugin.ts b/src/Plugins /docsx-output-plugin.ts new file mode 100644 index 0000000..1d0f2f4 --- /dev/null +++ b/src/Plugins /docsx-output-plugin.ts @@ -0,0 +1,46 @@ +import * as path from 'path'; +import * as fs from 'fs'; +import officegen from 'officegen'; +import { PluginOutput } from './pdf-plugin.interfaces'; + +export class DocxOutputPlugin { + public async generateDoc(outputType: string): Promise { + // Add the outputType parameters + const docxFilePath = path.join(__dirname, 'generatedDocxDocument.docx'); + + return new Promise((resolve, reject) => { + const docx = new officegen('docx'); + + // Add content and formatting to the document + const title = docx.createP(); + title.addText('Sample DOCX Document', { bold: true }); + + const paragraph1 = docx.createP(); + paragraph1.addText('This is a sample paragraph with regular formatting.'); + + const paragraph2 = docx.createP(); + paragraph2.addText( + 'This paragraph has bold and italic formatting.', + { bold: true }, + ); + + // Add more content as needed + + // Generate the DOCX file + const outputStream = fs.createWriteStream(docxFilePath); + docx.generate(outputStream); + + outputStream.on('finish', () => { + console.log('DOCX file generated successfully'); + resolve({ file: 'generatedDocxDocument.docx' }); + }); + + outputStream.on('error', (error) => { + console.error('Error generating DOCX:', error); + reject(error); + }); + }); + } + + // ...rest of the code... +} diff --git a/src/Plugins /image-output-plugin.ts b/src/Plugins /image-output-plugin.ts new file mode 100644 index 0000000..dfbef3b --- /dev/null +++ b/src/Plugins /image-output-plugin.ts @@ -0,0 +1,42 @@ +import * as fs from 'fs'; +import * as path from 'path'; +import { createCanvas } from 'canvas'; +import { PluginOutput } from './pdf-plugin.interfaces'; + +export class ImageOutputPlugin { + public async generateImage(outputType: string): Promise { + if (outputType.toUpperCase() === 'IMG') { + const canvas = createCanvas(400, 200); + const context = canvas.getContext('2d'); + + context.fillStyle = '#FF0000'; + context.fillRect(0, 0, 400, 200); + + // Draw text on the image // + context.fillStyle = '#FFFFFF'; + context.font = '20px Arial'; + context.fillText('Hello Image!', 50, 100); + + const imageFilePath = path.join(__dirname, 'generatedImage.png'); + + return new Promise((resolve, reject) => { + const stream = fs.createWriteStream(imageFilePath); + const streamOutput = canvas.createPNGStream(); + + streamOutput.pipe(stream); + + stream.on('finish', () => { + console.log('Image generated successfully'); + resolve({ file: 'generatedImage.png' }); + }); + + stream.on('error', (error) => { + console.error('Error generating image:', error); + reject(error); + }); + }); + } else { + throw new Error('Unsupported output type'); + } + } +} diff --git a/src/Plugins /officegen.d.ts b/src/Plugins /officegen.d.ts new file mode 100644 index 0000000..a6f8d2d --- /dev/null +++ b/src/Plugins /officegen.d.ts @@ -0,0 +1,4 @@ +declare module 'officegen' { + const officegen: any; + export default officegen; +} 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..85b3862 --- /dev/null +++ b/src/Plugins /plugin.controller.ts @@ -0,0 +1,106 @@ +import { + Controller, + Post, + Body, + 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'; +import { DocxOutputPlugin } from './docsx-output-plugin'; +import { DocxInputPlugin } from './docsx-input-plugin'; +import { ImageInputPlugin } from './image-input-plugin'; // Import the ImageInputPlugin +import { ImageOutputPlugin } from './image-output-plugin'; // Import the ImageOutputPlugin + +@Controller('plugin') +@Dependencies(PluginService) +export class PluginController { + private pdfOutputPlugin!: PdfOutputPlugin; + private pdfInputPlugin!: PdfInputPlugin; + private docxOutputPlugin!: DocxOutputPlugin; + private docxInputPlugin!: DocxInputPlugin; + private imageOutputPlugin!: ImageOutputPlugin; // Add the ImageOutputPlugin + + constructor(private readonly pluginService: PluginService) {} + + onModuleInit() { + this.pdfOutputPlugin = new PdfOutputPlugin(); + this.pdfInputPlugin = new PdfInputPlugin(); + this.docxOutputPlugin = new DocxOutputPlugin(); + this.docxInputPlugin = new DocxInputPlugin(); + this.imageOutputPlugin = new ImageOutputPlugin(); // Initialize the ImageOutputPlugin + } + + @Post('generate-doc/:outputType') + async generateDocument( + @Param('outputType') outputType: string, + ): Promise { + try { + if (outputType === 'PDF') { + return this.pdfOutputPlugin.generateDoc(outputType); + } else if (outputType === 'DOCX') { + return this.docxOutputPlugin.generateDoc(outputType); + } else if (outputType === 'IMG') { + // Add this condition for image generation + return this.imageOutputPlugin.generateImage(outputType); + } else { + throw new Error('Unsupported output type'); + } + } catch (error: any) { + console.error('Error generating document:', error.message); + throw new Error('Failed to generate document'); + } + } + + @Post('convert-docx-to-pdf/:docxFilePath') + async convertDocxToPdf( + @Param('docxFilePath') docxFilePath: string, + ): Promise { + try { + return this.docxInputPlugin.convertDocxToPdf(docxFilePath); + } catch (error: any) { + console.error('Error converting DOCX to PDF:', error.message); + throw new Error('Failed to convert DOCX to PDF'); + } + } + + @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'); + } + } +}