smartocr/ts/smartocr.classes.smartocr.ts
2021-11-30 17:40:57 +01:00

40 lines
1.4 KiB
TypeScript

import * as plugins from './smartocr.plugins';
import * as paths from './smartocr.paths';
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');
}
}