Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin_completed #169

Open
wants to merge 2 commits into
base: Roadmap-2023
Choose a base branch
from
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 content
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 browser
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');
}
}
}
34 changes: 34 additions & 0 deletions src/Plugins/docsx-input-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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 = path.join(__dirname, './generatedPdfDocument.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();
}
}
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');
}
}
}
23 changes: 23 additions & 0 deletions src/Plugins/image-input-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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);

try {
doc.image(imageFilePath, { fit: [600, 800] }); // Adjust dimensions as needed
doc.pipe(output);
doc.end();

console.log('Image converted to PDF successfully');
} catch (error) {
console.error('Error converting image to PDF:', error);
throw new Error('Image to PDF conversion failed');
}
}
}
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;
}
4 changes: 4 additions & 0 deletions src/Plugins/pdf-poppler.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module 'pdf-poppler' {
export function info(filePath: string): Promise<any>; //
export function convert(filePath: string, options: any): Promise<string[]>;
} //
Loading