-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf-to-txt.js
68 lines (61 loc) · 1.92 KB
/
pdf-to-txt.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const fs = require('fs');
const pdf = require('pdf-parse');
const path = require('path');
let getDirName = require('path').dirname;
const moveFrom = __dirname + '/pdfs/itamarfranco/';
const moveTo = __dirname + '/pdf-txt/';
function cb(err) {
console.error(err ? 'deu ruim' : 'deu bom');
}
function writeFile(path, contents, cb) {
fs.mkdir(
getDirName(path),
{
recursive: true,
},
function (err) {
if (err) return cb(err);
fs.writeFile(path, contents, cb);
}
);
}
(async () => {
try {
const presidentesFolder = await fs.promises.readdir(moveFrom);
for (const presidentes of presidentesFolder) {
const yearsFolder = await fs.promises.readdir(moveFrom + `${presidentes}/`);
for (const year of yearsFolder) {
const data = await fs.promises.readdir(moveFrom + `${presidentes}/${year}/`);
for (const file of data) {
const fromPath = path.join(moveFrom + `${presidentes}/${year}/`, file);
const toPath = path.join(moveTo + `${presidentes}/${year}/`, file);
let dataBuffer = fs.readFileSync(fromPath);
pdf(dataBuffer).then(function (data) {
writeFile(toPath, data.text, cb);
});
}
}
}
} catch (e) {
console.error(e);
}
})();
// have selected president name on 'moveFrom'
// (async () => {
// try {
// const yearsFolder = await fs.promises.readdir(moveFrom);
// for (const year of yearsFolder) {
// const data = await fs.promises.readdir(moveFrom + `${year}/`);
// for (const file of data) {
// const fromPath = path.join(moveFrom + `${year}/`, file);
// const toPath = path.join(moveTo + `${year}/`, file);
// let dataBuffer = fs.readFileSync(fromPath);
// pdf(dataBuffer).then(function (data) {
// writeFile(toPath, data.text, cb);
// });
// }
// }
// } catch (e) {
// console.error(e);
// }
// })();