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
33 changes: 33 additions & 0 deletions src/Plugins/pdf-input-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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');
}
}
}
67 changes: 67 additions & 0 deletions src/Plugins/pdf-output-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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 axios from 'axios';
import { PluginOutput } from './pdf-plugin.interfaces';

(pdfMake as any).vfs = pdfFonts.pdfMake.vfs;

export class PdfOutputPlugin {
public async generateDoc(outputType: string): Promise<PluginOutput> {
if (outputType.toUpperCase() === 'PDF') {
const pdfContent: TDocumentDefinitions = {
content: [
{
text: 'Hello PDF generated successfully!',
fontSize: 16,
alignment: 'center',
margin: [0, 100, 0, 0],
},
],
};

const pdfDoc = pdfMake.createPdf(pdfContent);
const pdfFilePath = path.join('.Plugins/generatedDocument.pdf');

return new Promise<PluginOutput>((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<PluginOutput> {
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<PluginOutput>((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
});
});
});
}
}
25 changes: 25 additions & 0 deletions src/Plugins/pdf-plugin.interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { TDocumentDefinitions } from 'pdfmake/interfaces';

export interface PluginOutput {
file?: string;
url?: string;
images?: { url: string }[];
}

export interface Plugin {
generateDoc(outputType: string): Promise<PluginOutput>;
validateTemplate(template: string): boolean;
getName(): string;
getSupportedOutputs(): string[];
getSupportedInputs(): string[];
transformToPDFMake(
inputType: string,
outputType: string,
): Promise<PluginOutput>;
transformPdfToDocsx(pdfFilePath: string): Promise<PluginOutput>;
transformPdfToImage(pdfFilePath: string): Promise<PluginOutput>;
transformPdfToExcalidraw(inputFile: string): PluginOutput;
transformPdfToDrawio(inputFile: string): PluginOutput;
transformPdfMakeToMermaid(pdfMakeContent: TDocumentDefinitions): string;
isSupportedConversion(inputType: string, outputType: string): boolean;
}
88 changes: 88 additions & 0 deletions src/Plugins/pdf-plugin.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
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<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 {
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<PluginOutput> {
throw new Error('Method not implemented.');
}

transformPdfToDocsx(pdfFilePath: string): Promise<PluginOutput> {
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<PluginOutput> {
throw new Error('Method not implemented.');
}
}
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[]>;
}
68 changes: 68 additions & 0 deletions src/Plugins/plugin.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
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';

@Controller('plugin')
@Dependencies(PluginService)
export class PluginController {
private pdfOutputPlugin!: PdfOutputPlugin;
private pdfInputPlugin!: PdfInputPlugin;

constructor(private readonly pluginService: PluginService) {}

onModuleInit() {
this.pdfOutputPlugin = new PdfOutputPlugin();
this.pdfInputPlugin = new PdfInputPlugin();
}

@Post('generate-doc/:outputType')
async generateDocument(
@Param('outputType') outputType: string,
): Promise<PluginOutput> {
try {
return this.pdfOutputPlugin.generateDoc(outputType);
} catch (error: any) {
console.error('Error generating document:', error.message);
throw new Error('Failed to generate document');
}
}

@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<PluginOutput> {
try {
return this.pdfOutputPlugin.createDefaultPdf();
} catch (error: any) {
console.error('Error creating default PDF:', error.message);
throw new Error('Failed to create default PDF');
}
}
}
10 changes: 10 additions & 0 deletions src/Plugins/plugin.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { PluginController } from './plugin.controller';
import { PluginService } from './pdf-plugin.service';
import { PdfInputPlugin } from './pdf-input-plugin';

@Module({
controllers: [PluginController],
providers: [PluginService, PdfInputPlugin],
})
export class PluginModule {}