Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/Plugins/Drawio-input-plugin.ts
Original file line number Diff line number Diff line change
@@ -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<PluginOutput> {
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 = `<div>${JSON.stringify(drawioJson)}</div>`;

// 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');
}
}
}
30 changes: 30 additions & 0 deletions src/Plugins/Mermaid.input.plugin.ts
Original file line number Diff line number Diff line change
@@ -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<PluginOutput> {
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 = `<div class="mermaid">${mermaidContent}</div>`;
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');
}
}
}
78 changes: 78 additions & 0 deletions src/Plugins/docsx-input-plugin.ts
Original file line number Diff line number Diff line change
@@ -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<PluginOutput> {
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<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();
}
public async convertDocxToImages(
docxFilePath: string,
): Promise<PluginOutput> {
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<void> {
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();
}
}
37 changes: 37 additions & 0 deletions src/Plugins/docsx-output-plugin.ts
Original file line number Diff line number Diff line change
@@ -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<PluginOutput> {
const docFilePath = path.join('./generatedDocxDocument.docx');

return new Promise<PluginOutput>((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);
});
});
}
}
25 changes: 25 additions & 0 deletions src/Plugins/docx-plugin.service.ts
Original file line number Diff line number Diff line change
@@ -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<PluginOutput> {
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');
}
}
}
33 changes: 33 additions & 0 deletions src/Plugins/excalidraw.input-plugin.ts
Original file line number Diff line number Diff line change
@@ -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<PluginOutput> {
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');
}
}
}
33 changes: 33 additions & 0 deletions src/Plugins/image-input-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as fs from 'fs';
import PDFDocument from 'pdfkit';

export class ImageInputPlugin {
async convertImageToPdf(
imageFilePath: string,
pdfFilePath: string,
): Promise<void> {
const doc = new PDFDocument();
const output = fs.createWriteStream(pdfFilePath);

return new Promise<void>((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'));
}
});
}
}
45 changes: 45 additions & 0 deletions src/Plugins/image-output-plugin.ts
Original file line number Diff line number Diff line change
@@ -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<PluginOutput> {
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<PluginOutput>((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');
}
}
}
4 changes: 4 additions & 0 deletions src/Plugins/officegen.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module 'officegen' {
const officegen: any;
export default officegen;
} //
34 changes: 34 additions & 0 deletions src/Plugins/pdf-input-plugin.ts
Original file line number Diff line number Diff line change
@@ -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<PluginOutput> {
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');
}
}
//
}
Loading