-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
64 lines (54 loc) · 1.64 KB
/
index.js
File metadata and controls
64 lines (54 loc) · 1.64 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const fs = require('fs');
const path = require('path');
const pdfUtil = require('pdf-to-text');
const SOURCE = './source';
const OUTPUT = './output';
Array.prototype.delayedForEach = function(callback, timeout, thisArg){
var i = 0,
l = this.length,
self = this,
caller = function(){
callback.call(thisArg || self, self[i], i, self);
(++i < l) && setTimeout(caller, timeout);
};
caller();
};
fs.readdir(SOURCE, (err, list)=>{
if(err){
return console.log(err);
}
console.log(list);
let processed = 0;
list.delayedForEach((file)=>{
const fileToConvertPath = path.join(SOURCE, file);
pdfUtil.pdfToText(fileToConvertPath, null, function(err, data){
if(err){
console.log(err);
} else {
const name = file.split('.pdf')[0];
const newName = `${name}.txt`;
const txtPath = path.join(OUTPUT, newName);
fs.writeFile(txtPath, data, (err)=>{
if (err){
console.log(err);
}
processed ++;
console.log(`convertiti ${processed} files`);
});
}
})
}, 50);
})
/*
var pdfUtil = require('pdf-to-text');
var pdf_path = "absolute_path/to/pdf_file.pdf";
pdfUtil.pdfToText(upload.path, null, function(err, data) {
if (err) throw(err);
console.log(data); //print text
});
//Omit option to extract all text from the pdf file
pdfUtil.pdfToText(upload.path, function(err, data) {
if (err) throw(err);
console.log(data); //print all text
});
*/