-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
54 lines (46 loc) · 1.36 KB
/
index.js
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
46
47
48
49
50
51
52
53
54
const pdfUtil = require("pdf-to-text");
const libre = require("libreoffice-convert");
const path = require("path");
const fs = require("fs");
function readPdf(pdfPath, callback) {
pdfUtil.info(pdfPath, async (err, { pages }) => {
const readeLine = (opts) => {
return new Promise((resolve, reject) => {
pdfUtil.pdfToText(pdfPath, opts, (err, lines) => {
if (err) return reject(err);
resolve(
lines
.trim()
.split("\n")
.map((text) => text.trim())
.filter((text) => text.length)
);
});
});
};
const lines = [];
for (let idx = 1; idx < pages + 1; idx++) {
let line = await readeLine({ from: idx, to: idx });
lines.push(line);
}
callback(lines);
});
}
exports.readPath = (input) => {
return new Promise((resolve, reject) => {
const enterPath = path.resolve(input);
const file = fs.readFileSync(enterPath);
const extend = ".pdf";
const outputPath = path.join(`/tmp/pdf-to-json${extend}`);
libre.convert(file, extend, undefined, (err, done) => {
if (err) {
return reject(new Error(`Error converting file: ${err}`));
}
fs.writeFileSync(outputPath, done);
readPdf(outputPath, (pairs) => {
resolve(pairs);
fs.rmSync(outputPath);
});
});
});
};