From 114177b0061a4d3e9b61777dec4dad20d07e4a0a Mon Sep 17 00:00:00 2001 From: kartiktongaria Date: Sun, 13 Aug 2023 01:02:23 +0530 Subject: [PATCH 1/3] Added Create Docx File --- src/Plugins /docsx-output-plugin.ts | 46 +++++++++++++++ src/Plugins /officegen.d.ts | 4 ++ src/Plugins /pdf-poppler.d.ts | 4 ++ src/Plugins /plugin.controller.ts | 91 +++++++++++++++++++++++++++++ 4 files changed, 145 insertions(+) create mode 100644 src/Plugins /docsx-output-plugin.ts create mode 100644 src/Plugins /officegen.d.ts create mode 100644 src/Plugins /pdf-poppler.d.ts create mode 100644 src/Plugins /plugin.controller.ts 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 /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..fd9117f --- /dev/null +++ b/src/Plugins /plugin.controller.ts @@ -0,0 +1,91 @@ +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'; +import { DocxOutputPlugin } from './docsx-output-plugin'; // Import the DocxOutputPlugin +import { DocxInputPlugin } from './docsx-input-plugin'; +import * as path from 'path'; + +@Controller('plugin') +@Dependencies(PluginService) +export class PluginController { + private pdfOutputPlugin!: PdfOutputPlugin; + private pdfInputPlugin!: PdfInputPlugin; + private docxOutputPlugin!: DocxOutputPlugin; + private docxInputPlugin!: DocxInputPlugin; + + constructor(private readonly pluginService: PluginService) {} + + onModuleInit() { + this.pdfOutputPlugin = new PdfOutputPlugin(); + this.pdfInputPlugin = new PdfInputPlugin(); + this.docxOutputPlugin = new DocxOutputPlugin(); + this.docxInputPlugin = new DocxInputPlugin(); + } + + @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 { + 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') // Adjust the route + async convertDocxToPdf(): Promise { + try { + return this.docxInputPlugin.convertDocxToPdf(); // Call the conversion function + } 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'); + } + } +} From e73a9a297669b3424a6d13508fc825304ad1856f Mon Sep 17 00:00:00 2001 From: kartiktongaria Date: Tue, 15 Aug 2023 00:06:25 +0530 Subject: [PATCH 2/3] Added Create Image File --- src/Plugins /image-output-plugin.ts | 42 +++++++++++++++++++++++++++++ src/Plugins /officegen.d.ts | 2 +- src/Plugins /pdf-poppler.d.ts | 2 +- src/Plugins /plugin.controller.ts | 25 +++++++++++++---- 4 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 src/Plugins /image-output-plugin.ts diff --git a/src/Plugins /image-output-plugin.ts b/src/Plugins /image-output-plugin.ts new file mode 100644 index 0000000..b24934e --- /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 index a6f8d2d..6ca22fe 100644 --- a/src/Plugins /officegen.d.ts +++ b/src/Plugins /officegen.d.ts @@ -1,4 +1,4 @@ declare module 'officegen' { const officegen: any; - export default officegen; + export default officegen; //j } diff --git a/src/Plugins /pdf-poppler.d.ts b/src/Plugins /pdf-poppler.d.ts index 8ebaaf9..8bc32ff 100644 --- a/src/Plugins /pdf-poppler.d.ts +++ b/src/Plugins /pdf-poppler.d.ts @@ -1,4 +1,4 @@ declare module 'pdf-poppler' { - export function info(filePath: string): Promise; + 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 index fd9117f..c678d2d 100644 --- a/src/Plugins /plugin.controller.ts +++ b/src/Plugins /plugin.controller.ts @@ -1,11 +1,19 @@ -import { Controller, Post, Get, Param, Dependencies } from '@nestjs/common'; +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 the DocxOutputPlugin import { DocxInputPlugin } from './docsx-input-plugin'; -import * as path from 'path'; +import { ImageInputPlugin } from './image-input-plugin'; // Import the ImageInputPlugin +import { ImageOutputPlugin } from './image-output-plugin'; // Import the ImageOutputPlugin @Controller('plugin') @Dependencies(PluginService) @@ -14,6 +22,7 @@ export class PluginController { private pdfInputPlugin!: PdfInputPlugin; private docxOutputPlugin!: DocxOutputPlugin; private docxInputPlugin!: DocxInputPlugin; + private imageOutputPlugin!: ImageOutputPlugin; // Add the ImageOutputPlugin constructor(private readonly pluginService: PluginService) {} @@ -22,6 +31,7 @@ export class PluginController { this.pdfInputPlugin = new PdfInputPlugin(); this.docxOutputPlugin = new DocxOutputPlugin(); this.docxInputPlugin = new DocxInputPlugin(); + this.imageOutputPlugin = new ImageOutputPlugin(); // Initialize the ImageOutputPlugin } @Post('generate-doc/:outputType') @@ -33,6 +43,9 @@ export class PluginController { 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'); } @@ -42,10 +55,12 @@ export class PluginController { } } - @Post('convert-docx-to-pdf') // Adjust the route - async convertDocxToPdf(): Promise { + @Post('convert-docx-to-pdf/:docxFilePath') + async convertDocxToPdf( + @Param('docxFilePath') docxFilePath: string, + ): Promise { try { - return this.docxInputPlugin.convertDocxToPdf(); // Call the conversion function + 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'); From 47053506e22aed52e613af1fa66a044fd42f1bb2 Mon Sep 17 00:00:00 2001 From: kartiktongaria Date: Tue, 15 Aug 2023 00:12:34 +0530 Subject: [PATCH 3/3] Added Create Image File --- src/Plugins /image-output-plugin.ts | 2 +- src/Plugins /officegen.d.ts | 2 +- src/Plugins /pdf-poppler.d.ts | 2 +- src/Plugins /plugin.controller.ts | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Plugins /image-output-plugin.ts b/src/Plugins /image-output-plugin.ts index b24934e..dfbef3b 100644 --- a/src/Plugins /image-output-plugin.ts +++ b/src/Plugins /image-output-plugin.ts @@ -12,7 +12,7 @@ export class ImageOutputPlugin { context.fillStyle = '#FF0000'; context.fillRect(0, 0, 400, 200); - // Draw text on the image + // Draw text on the image // context.fillStyle = '#FFFFFF'; context.font = '20px Arial'; context.fillText('Hello Image!', 50, 100); diff --git a/src/Plugins /officegen.d.ts b/src/Plugins /officegen.d.ts index 6ca22fe..a6f8d2d 100644 --- a/src/Plugins /officegen.d.ts +++ b/src/Plugins /officegen.d.ts @@ -1,4 +1,4 @@ declare module 'officegen' { const officegen: any; - export default officegen; //j + export default officegen; } diff --git a/src/Plugins /pdf-poppler.d.ts b/src/Plugins /pdf-poppler.d.ts index 8bc32ff..8ebaaf9 100644 --- a/src/Plugins /pdf-poppler.d.ts +++ b/src/Plugins /pdf-poppler.d.ts @@ -1,4 +1,4 @@ declare module 'pdf-poppler' { - export function info(filePath: string): Promise; // + 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 index c678d2d..85b3862 100644 --- a/src/Plugins /plugin.controller.ts +++ b/src/Plugins /plugin.controller.ts @@ -10,7 +10,7 @@ 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 the DocxOutputPlugin +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