-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtabula.js
70 lines (58 loc) · 1.96 KB
/
tabula.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
69
70
/**
* Trifacta Inc. Confidential
*
* Copyright 2015 Trifacta Inc.
* All Rights Reserved.
*
* Any use of this material is subject to the Trifacta Inc., Source License located
* in the file 'SOURCE_LICENSE.txt' which is part of this package. All rights to
* this material and any derivative works thereof are reserved by Trifacta Inc.
*/
var spawn = require('child_process').spawn,
path = require('path'),
util = require('util'),
events = require('events');
function Tabula() {
events.EventEmitter.call(this);
}
util.inherits(Tabula, events.EventEmitter);
/**
* name of inputFile <-- absolute path
* pageNumber is optional, defaults to 'all'
* additionalArguments are optional (arguments to tabula-extractor)
*/
Tabula.prototype.convertPdfToCsv = function convertPdfToCsv(inputFile, pageNumber, additionalArguments) {
var self = this;
var args = ['-jar', path.join(__dirname, 'lib', 'tabula-extractor.jar'), inputFile];
// if page number is given, use it
// else, digest everything
if (typeof pageNumber !== 'undefined') {
args = args.concat(['-p', pageNumber]);
} else {
args = args.concat(['-p', 'all']);
}
if (additionalArguments) {
args = args.concat(additionalArguments);
}
var childProcess = spawn('java', args);
childProcess.stdout.on('data', function(data) {
self.emit('data', data.toString());
});
childProcess.stderr.on('data', function(data) {
// filter for error messages
// when java/ruby emit error messages on stderr
// they contain some version of "error:"
var errorMsg = data.toString();
if(errorMsg.toLowerCase().indexOf('error:') !== -1) {
self.emit('error', new Error("PDF Extraction Error: " + errorMsg));
}
});
// occurs on failure of spawn initiation/message passing/termination
childProcess.on('error', function(err) {
self.emit('error', err);
});
childProcess.on('close', function(exitCode) {
self.emit('close', exitCode);
});
};
module.exports = Tabula;