diff --git a/src/Plugins/Drawio-input-plugin.ts b/src/Plugins/Drawio-input-plugin.ts new file mode 100644 index 0000000..2d3560d --- /dev/null +++ b/src/Plugins/Drawio-input-plugin.ts @@ -0,0 +1,51 @@ +import * as fs from 'fs'; +import * as path from 'path'; +import { PluginOutput } from './pdf-plugin.interfaces'; +import puppeteer from 'puppeteer'; +import { parseString } from 'xml2js'; + +export class DrawioInputPlugin { + public async convertDrawioFileToPdf( + drawioFilePath: string, + ): Promise { + try { + // Read Draw.io file conten + const drawioXml = fs.readFileSync(drawioFilePath, 'utf-8'); + + // Parse Draw.io XML to JavaScript object// + let drawioJson; + parseString(drawioXml, (err, result) => { + if (err) { + throw err; + } + drawioJson = result; + }); + + // Convert Draw.io JSON object to HTML (manually or using a templating engine) + const drawioHtml = `
${JSON.stringify(drawioJson)}
`; + + // Launch a headless browser instance + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + + // Set the content of the page to the Draw.io HTML + await page.setContent(drawioHtml); + + // Generate a PDF from the rendered HTML content + const pdfBuffer = await page.pdf(); + + // Close the browsers + await browser.close(); + + const pdfFilePath = path.join(__dirname, 'generatedDrawio.pdf'); // Adjust the path accordingly + fs.writeFileSync(pdfFilePath, pdfBuffer); + + console.log('Draw.io PDF generated successfully'); + + return { file: 'generatedDrawio.pdf' }; + } catch (error) { + console.error('Error converting Draw.io file to PDF:', error); + throw new Error('Failed to convert Draw.io file to PDF'); + } + } +} diff --git a/src/Plugins/Mermaid.input.plugin.ts b/src/Plugins/Mermaid.input.plugin.ts new file mode 100644 index 0000000..13de642 --- /dev/null +++ b/src/Plugins/Mermaid.input.plugin.ts @@ -0,0 +1,30 @@ +import { PluginOutput } from './pdf-plugin.interfaces'; // Make sure you import the correct interface +import puppeteer from 'puppeteer'; + +export class MermaidInputPlugin { + public async convertMermaidToPdf(mermaidContent: any): Promise { + try { + const pdfFilePath = `./generatedMermaid.pdf`; // Adjust the path + + // Launch a headless browser + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + + // Set the content of the page to the Mermaid diagram HTML// + const htmlContent = `
${mermaidContent}
`; + await page.setContent(htmlContent); + + // Generate a PDF from the Mermaid diagram HTML + await page.pdf({ path: pdfFilePath, format: 'A4' }); + + await browser.close(); + + console.log('Mermaid PDF generated successfully'); + + return { file: 'generatedMermaid.pdf' }; + } catch (error) { + console.error('Error generating Mermaid PDF:', error); + throw new Error('Failed to convert Mermaid to PDF'); + } + } +} diff --git a/src/Plugins/docsx-input-plugin.ts b/src/Plugins/docsx-input-plugin.ts new file mode 100644 index 0000000..568214a --- /dev/null +++ b/src/Plugins/docsx-input-plugin.ts @@ -0,0 +1,78 @@ +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 { + try { + const outputPdfPath = `./DocxToPdf.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 { + const result = await mammoth.convertToHtml({ path: docxFilePath }); + return result.value; + } + + private async htmlToPdf(htmlContent: string, pdfPath: string): Promise { + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + await page.setContent(htmlContent); + await page.pdf({ path: pdfPath, format: 'A4' }); + + await browser.close(); + } + public async convertDocxToImages( + docxFilePath: string, + ): Promise { + try { + const imagesFolderPath = path.join(__dirname, './generatedImages'); + const htmlContent = await this.convertDocxToHtml(docxFilePath); + await this.htmlToImages(htmlContent, imagesFolderPath); + + console.log('Images generated successfully'); + return { folder: imagesFolderPath }; + } catch (error) { + console.error('Error generating images:', error); + throw new Error('Failed to convert DOCX to images'); + } + } + + private async htmlToImages( + htmlContent: string, + imagesFolderPath: string, + ): Promise { + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + await page.setContent(htmlContent); + + const elements = await page.$$('img'); // Get all img elements in the HTML + for (let i = 0; i < elements.length; i++) { + const imgElement = elements[i]; + const imgSrc = await imgElement.evaluate((node) => + node.getAttribute('src'), + ); + const imgName = `image_${i}.png`; + const imgPath = path.join(imagesFolderPath, imgName); + + // Wait for images to load + await page.waitForFunction(() => { + const images = document.getElementsByTagName('img'); + return Array.from(images).every((img) => img.complete); + }); + + await imgElement.screenshot({ path: imgPath }); + } + + await browser.close(); + } +} diff --git a/src/Plugins/docsx-output-plugin.ts b/src/Plugins/docsx-output-plugin.ts new file mode 100644 index 0000000..b07a41a --- /dev/null +++ b/src/Plugins/docsx-output-plugin.ts @@ -0,0 +1,37 @@ +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, + userInput: string[], + ): Promise { + const docFilePath = path.join('./generatedDocxDocument.docx'); + + return new Promise((resolve, reject) => { + const docx = new officegen('docx'); + + // Iterate through user input and add paragraphs to the document + userInput.forEach((input) => { + const paragraph = docx.createP(); + paragraph.addText(input); + }); + + // Generate the DOCX file + const outputStream = fs.createWriteStream(docFilePath); + 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); + }); + }); + } +} diff --git a/src/Plugins/docx-plugin.service.ts b/src/Plugins/docx-plugin.service.ts new file mode 100644 index 0000000..f62de1c --- /dev/null +++ b/src/Plugins/docx-plugin.service.ts @@ -0,0 +1,25 @@ +import { Injectable } from '@nestjs/common'; +import { PluginOutput } from './pdf-plugin.interfaces'; +import { promisify } from 'util'; +import { exec } from 'child_process'; // + +@Injectable() +export class DocxInputService { + async convertToPdf(docxFilePath: string): Promise { + const pdfFilePath = docxFilePath.replace('.docx', '.pdf'); + + const execAsync = promisify(exec); + + try { + const command = `pandoc ${docxFilePath} -o ${pdfFilePath}`; + await execAsync(command); + + console.log('PDF file generated successfully'); + return { file: pdfFilePath }; + } catch (error: any) { + // Cast error to 'any' type + console.error('Error converting to PDF:', error.message); + throw new Error('Failed to convert DOCX to PDF'); + } + } +} diff --git a/src/Plugins/excalidraw.input-plugin.ts b/src/Plugins/excalidraw.input-plugin.ts new file mode 100644 index 0000000..1d514f2 --- /dev/null +++ b/src/Plugins/excalidraw.input-plugin.ts @@ -0,0 +1,33 @@ +import * as fs from 'fs'; +import * as path from 'path'; +import { PluginOutput } from './pdf-plugin.interfaces'; // Make sure you import the correct interface +import puppeteer from 'puppeteer'; + +export class ExcalidrawInputPlugin { + public async convertExcalidrawToPdf( + excalidrawContent: string, + ): Promise { + try { + const pdfFilePath = path.join(__dirname, 'generatedExcalidraw.pdf'); // Adjust the path + + // Launch a headless browser + const browser = await puppeteer.launch(); + const page = await browser.newPage(); + + // Set the content of the page to the Excalidraw SVG + await page.setContent(excalidrawContent); + + // Generate a PDF from the SVG // + await page.pdf({ path: pdfFilePath, format: 'A4' }); + + await browser.close(); + + console.log('Excalidraw PDF generated successfully'); + + return { file: pdfFilePath }; + } catch (error) { + console.error('Error generating Excalidraw PDF:', error); + throw new Error('Failed to convert Excalidraw to PDF'); + } + } +} diff --git a/src/Plugins/image-input-plugin.ts b/src/Plugins/image-input-plugin.ts new file mode 100644 index 0000000..45cdbff --- /dev/null +++ b/src/Plugins/image-input-plugin.ts @@ -0,0 +1,33 @@ +import * as fs from 'fs'; +import PDFDocument from 'pdfkit'; + +export class ImageInputPlugin { + async convertImageToPdf( + imageFilePath: string, + pdfFilePath: string, + ): Promise { + const doc = new PDFDocument(); + const output = fs.createWriteStream(pdfFilePath); + + return new Promise((resolve, reject) => { + try { + doc.image(imageFilePath, { fit: [600, 800] }); // Adjust dimensions as needed// + doc.pipe(output); + doc.end(); + + output.on('finish', () => { + console.log('Image converted to PDF successfully'); + resolve(); + }); + + output.on('error', (error) => { + console.error('Error converting image to PDF:', error); + reject(new Error('Image to PDF conversion failed')); + }); + } catch (error) { + console.error('Error converting image to PDF:', error); + reject(new Error('Image to PDF conversion failed')); + } + }); + } +} diff --git a/src/Plugins/image-output-plugin.ts b/src/Plugins/image-output-plugin.ts new file mode 100644 index 0000000..2f95072 --- /dev/null +++ b/src/Plugins/image-output-plugin.ts @@ -0,0 +1,45 @@ +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, + userInput: 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 user input text on the images// + context.fillStyle = '#FFFFFF'; + context.font = '20px Arial'; + context.fillText(userInput, 50, 100); + + const imageFilePath = path.join('./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..934350b --- /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-input-plugin.ts b/src/Plugins/pdf-input-plugin.ts new file mode 100644 index 0000000..5f128cc --- /dev/null +++ b/src/Plugins/pdf-input-plugin.ts @@ -0,0 +1,34 @@ +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..13fa3a8 --- /dev/null +++ b/src/Plugins/pdf-output-plugin.ts @@ -0,0 +1,72 @@ +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 { PluginOutput } from './pdf-plugin.interfaces'; + +(pdfMake as any).vfs = pdfFonts.pdfMake.vfs; + +export class PdfOutputPlugin { + public async generateDoc( + outputType: string, + userInput: string[], + ): Promise { + if (outputType.toUpperCase() === 'PDF') { + const pdfContent: TDocumentDefinitions = { + content: [ + { + text: '', + fontSize: 16, + alignment: 'center', + margin: [0, 10, 0, 0], + }, + { + ul: userInput, // Assuming userInput is an array of string + }, + ], + }; + + const pdfDoc = pdfMake.createPdf(pdfContent); + const pdfFilePath = path.join('./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..8823c4f --- /dev/null +++ b/src/Plugins/pdf-plugin.interfaces.ts @@ -0,0 +1,19 @@ +export interface PluginOutput { + file?: string; + url?: string; + pdfPath?: string; + folder?: string; // Add this line to include the 'folder' properties + 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; +} diff --git a/src/Plugins/pdf-plugin.service.ts b/src/Plugins/pdf-plugin.service.ts new file mode 100644 index 0000000..8be7008 --- /dev/null +++ b/src/Plugins/pdf-plugin.service.ts @@ -0,0 +1,89 @@ +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..9dc569a --- /dev/null +++ b/src/Plugins/plugin.controller.ts @@ -0,0 +1,252 @@ +import { + Controller, + Post, + Query, + UseInterceptors, + Res, + Get, + Body, + Param, + Dependencies, +} from '@nestjs/common'; +import * as fs from 'fs'; +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 { ImageOutputPlugin } from './image-output-plugin'; // Import the ImageOutputPlugin +import { ImageInputPlugin } from './image-input-plugin'; +import { DrawioInputPlugin } from './Drawio-input-plugin'; +import * as path from 'path'; +import { parseString } from 'xml2js'; // Import parseString from xml2js +import puppeteer from 'puppeteer'; +import { ExcalidrawInputPlugin } from './excalidraw.input-plugin'; // Adjust the import path +import { MermaidInputPlugin } from './Mermaid.input.plugin'; // Adjust the import path + +@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 + private imageInputPlugin!: ImageInputPlugin; + private ExcalidrawInputPlugin!: ExcalidrawInputPlugin; + private MermaidInputPlugin!: MermaidInputPlugin; + private DrawioInputPlugin!: DrawioInputPlugin; + 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 + this.imageInputPlugin = new ImageInputPlugin(); + this.ExcalidrawInputPlugin = new ExcalidrawInputPlugin(); + this.DrawioInputPlugin = new DrawioInputPlugin(); + this.MermaidInputPlugin = new MermaidInputPlugin(); + } + + @Get() + getPluginStatus(): string { + return 'Plugin is running!'; + } + + @Post('generate-pdf') + async generatePdf(@Body() userInput: string[]): Promise { + try { + const result = await this.pdfOutputPlugin.generateDoc('pdf', userInput); + return result; + } catch (error: any) { + console.error('Error generating PDF:', error.message); + throw new Error('Failed to generate PDF'); + } + } + + @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'); + } + } + @Post('generate-docx') + async generateDocx(@Body() userInput: string[]): Promise { + try { + const result = await this.docxOutputPlugin.generateDoc('docx', userInput); + return result; + } catch (error: any) { + console.error('Error generating DOCX:', error.message); + throw new Error('Failed to generate DOCX'); + } + } + @Post('generate-image') + async generateImage(@Body() userInput: string): Promise { + try { + return await this.imageOutputPlugin.generateImage('img', userInput); + } catch (error: any) { + console.error('Error generating image:', error.message); + throw new Error('Failed to generate image'); + } + } + + @Get('convert-pdf-to-images') + async convertPdfToImages( + @Query('pdfPath') pdfPath: string, + ): Promise { + try { + if (!pdfPath) { + console.error('PDF file path not provided.'); + throw new Error('PDF file path not provided.'); + } + + // Define the output folder where the images will be saved + const outputFolder = './generatedImages'; // Change this path as needed + + // Call a function to convert the PDF to images (you should implement this function) + const pluginOutput = await this.pdfInputPlugin.transformPdfToImage( + pdfPath, + ); + + // Check if the plugin output contains the list of image URLs + if (pluginOutput.images) { + const images = pluginOutput.images; + images.forEach((image: { url: string }) => { + console.log('Image', image.url); + }); + } + + return { images: pluginOutput.images }; + } catch (error: any) { + console.error('Error converting PDF to images:', error.message); + throw new Error('PDF to images conversion failed'); + } + } + + @Get('convert-image-to-pdf') + async convertImageToPdf( + @Query('imagePath') imagePath: string, + ): Promise { + try { + if (!imagePath) { + console.error('Image file path not provided.'); + throw new Error('Image file path not provided.'); + } + + const pdfFilePath = `./generatedImage.pdf`; // Output PDF file path + + await this.imageInputPlugin.convertImageToPdf(imagePath, pdfFilePath); + + return { pdfPath: pdfFilePath }; + } catch (error: any) { + console.error('Error converting image to PDF:', error.message); + throw new Error('Failed to convert image to PDF'); + } + } + + @Get('convert-docx-to-pdf') + async convertDocxToPdf( + @Query('docxPath') docxPath: string, + ): Promise { + try { + if (!docxPath) { + console.error('DOCX file path not provided.'); + throw new Error('DOCX file path not provided.'); + } + + const pluginOutput = await this.docxInputPlugin.convertDocxToPdf( + docxPath, + ); + + if (!pluginOutput.file) { + throw new Error('Generated PDF file not found.'); + } + + return { file: 'generatedPdfDocument.pdf' }; + } catch (error: any) { + console.error('Error converting DOCX to PDF:', error.message); + throw new Error('Failed to convert DOCX to PDF'); + } + } + + @Get('convert-docx-to-images') + async convertDocxToImages( + @Query('docxPath') docxPath: string, + ): Promise { + try { + if (!docxPath) { + console.error('DOCX file path not provided.'); + throw new Error('DOCX file path not provided.'); + } + + const pluginOutput = await this.docxInputPlugin.convertDocxToImages( + docxPath, + ); + + if (!pluginOutput.folder) { + throw new Error('Generated images folder not found.'); + } + + return { folder: pluginOutput.folder }; + } catch (error: any) { + console.error('Error converting DOCX to images:', error.message); + throw new Error('Failed to convert DOCX to images'); + } + } + + @Get('convert-excalidraw-to-pdf') + async convertExcalidrawToPdf( + @Query('excalidrawpath') excalidrawContent: string, + ): Promise { + try { + if (!excalidrawContent) { + console.error('Excalidraw content not provided.'); + throw new Error('Excalidraw content not provided.'); + } + + const pluginOutput = + await this.ExcalidrawInputPlugin.convertExcalidrawToPdf( + excalidrawContent, + ); + + if (!pluginOutput.file) { + throw new Error('Generated PDF file not found.'); + } + + return { file: pluginOutput.file }; // Use the correct property from the pluginOutput// + } catch (error: any) { + console.error('Error converting Excalidraw to PDF:', error.message); + throw new Error('Failed to convert Excalidraw to PDF'); + } + } + @Get('convert-drawio-to-pdf') + async convertDrawioToPdf( + @Query('drawioFilePath') drawioFilePath: string, + ): Promise { + try { + if (!drawioFilePath) { + console.error('Draw.io file path not provided.'); + throw new Error('Draw.io file path not provided.'); + } + + const pluginOutput = await this.DrawioInputPlugin.convertDrawioFileToPdf( + drawioFilePath, + ); + + if (!pluginOutput.file) { + throw new Error('Generated PDF file not found.'); + } + + return { file: pluginOutput.file }; + } catch (error: any) { + console.error('Error converting Draw.io to PDF:', error.message); + throw new Error('Failed to convert Draw.io to PDF'); + } + } +} diff --git a/src/Plugins/plugin.module.ts b/src/Plugins/plugin.module.ts new file mode 100644 index 0000000..61d3b3e --- /dev/null +++ b/src/Plugins/plugin.module.ts @@ -0,0 +1,16 @@ +import { Module } from '@nestjs/common'; +import { MulterModule } from '@nestjs/platform-express'; // Import MulterModule +import { PluginController } from './plugin.controller'; +import { PluginService } from './pdf-plugin.service'; +import { PdfInputPlugin } from './pdf-input-plugin'; + +@Module({ + imports: [ + MulterModule.register({ + dest: './uploads', // Specify the directory to store uploaded file + }), + ], + controllers: [PluginController], + providers: [PluginService, PdfInputPlugin], +}) +export class PluginModule {} diff --git a/src/Plugins/puppeteer-html-to-pdf.d.ts b/src/Plugins/puppeteer-html-to-pdf.d.ts new file mode 100644 index 0000000..2fc607c --- /dev/null +++ b/src/Plugins/puppeteer-html-to-pdf.d.ts @@ -0,0 +1,16 @@ +declare module 'puppeteer-html-to-pdf' { + interface Page { + goto: (url: string) => Promise; + waitForSelector: (selector: string) => Promise; + evaluate: (fn: (xml: string) => void, xml: string) => Promise; + } + + interface Browser { + newPage: () => Promise; + close: () => Promise; + } + + function initBrowser(): Promise; + + function generatePdfBuffer(page: Page): Promise; +} //