forked from p2r3/convert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdfparse.ts
More file actions
45 lines (34 loc) · 1.1 KB
/
Copy pathpdfparse.ts
File metadata and controls
45 lines (34 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// file: pdfparse.ts
import type { FileData, FileFormat, FormatHandler } from "../FormatHandler.ts";
import CommonFormats from "src/CommonFormats.ts";
import { PDFParse } from 'pdf-parse';
class pdfparseHandler implements FormatHandler {
public name: string = "pdfparse";
public supportedFormats?: FileFormat[] = [
CommonFormats.PDF.builder("pdf").allowFrom(),
CommonFormats.TEXT.builder("txt").allowTo(),
];
public ready: boolean = false;
async init () {
PDFParse.setWorker('/convert/js/pdf.worker.mjs');
this.ready = true;
}
async doConvert (
inputFiles: FileData[],
inputFormat: FileFormat,
outputFormat: FileFormat
): Promise<FileData[]> {
const outputFiles: FileData[] = [];
for (const inputFile of inputFiles) {
const parser = new PDFParse({ data: inputFile.bytes });
const text = await parser.getText();
await parser.destroy();
outputFiles.push({
bytes: new TextEncoder().encode(text.text),
name: inputFile.name.replace(/\.pdf$/i, ".txt"),
});
}
return outputFiles;
}
}
export default pdfparseHandler;