smartocr/ts/smartocr.classes.smartocr.ts
2024-04-18 16:43:32 +02:00

40 lines
1.4 KiB
TypeScript

import * as plugins from './smartocr.plugins.js';
import * as paths from './smartocr.paths.js';
export class SmartOcr {
// STATIC
public static async createAndInit() {
const smartocrInstance = new SmartOcr();
await smartocrInstance.init();
return smartocrInstance;
}
// INSTANCE
public readyDeferred = plugins.smartpromise.defer();
public smartshellInstance: plugins.smartshell.Smartshell;
public async processPdfBuffer (pdfBufferArg: Buffer): Promise<Buffer> {
const uniqueString = plugins.smartunique.uni('doc_');
const originalPath = plugins.path.join(paths.noGitDir, `${uniqueString}.pdf`);
const processedPath = plugins.path.join(paths.noGitDir, `${uniqueString}_processed.pdf`);
const originalSmartfile = await plugins.smartfile.SmartFile.fromBuffer(originalPath, pdfBufferArg);
await originalSmartfile.write();
await this.smartshellInstance.exec(`ocrmypdf --rotate-pages ${originalPath} ${processedPath}`);
const processedSmartfile = await plugins.smartfile.SmartFile.fromFilePath(processedPath);
await originalSmartfile.delete();
await processedSmartfile.delete();
return processedSmartfile.contentBuffer;
}
constructor() {
this.smartshellInstance = new plugins.smartshell.Smartshell({
executor: 'bash'
});
}
public async init() {
await plugins.smartfile.fs.ensureDir(paths.noGitDir);
const result = await plugins.smartshell.which('ocrmypdf');
}
}