-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.js
212 lines (185 loc) · 5.79 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
var path = require('path')
var spawn = require('child_process').spawn
function pdfTextExtract (filePath, options, pdfToTextCommand, cb) {
if (!cb) {
cb = pdfToTextCommand
}
if (!pdfToTextCommand) {
cb = options
}
// options is optional
if (typeof (options) === 'function') {
cb = options
options = {}
}
if (typeof (pdfToTextCommand) === 'function') {
cb = pdfToTextCommand
pdfToTextCommand = 'pdftotext'
}
if (!pdfToTextCommand) {
pdfToTextCommand = 'pdftotext'
}
filePath = path.resolve(filePath)
// [feat-promise] if cb is not a function, then it's probably a promise-typed call
if (typeof (cb) !== 'function') {
cb = null
}
// [feat-promise] options have to be not null
if (!options) {
options = {}
}
// default options
options.encoding = options.encoding || 'UTF-8'
options.layout = options.layout || 'layout'
options.splitPages = (options.splitPages !== false)
// Build args based on options
var args = []
// First and last page to convert
if (options.firstPage) { args.push('-f'); args.push(options.firstPage) }
if (options.lastPage) { args.push('-l'); args.push(options.lastPage) }
// Resolution, in dpi. (null is pdftotext default = 72)
if (options.resolution) { args.push('-r'); args.push(options.resolution) }
// If defined, should be an object { x:x, y:y, w:w, h:h }
if (typeof (options.crop) === 'object') {
if (options.crop.x) { args.push('-x'); args.push(options.crop.x) }
if (options.crop.y) { args.push('-y'); args.push(options.crop.y) }
if (options.crop.w) { args.push('-W'); args.push(options.crop.w) }
if (options.crop.h) { args.push('-H'); args.push(options.crop.h) }
}
// One of either 'layout', 'raw' or 'htmlmeta'
if (options.layout === 'layout') { args.push('-layout') }
if (options.layout === 'raw') { args.push('-raw') }
if (options.layout === 'htmlmeta') { args.push('-htmlmeta') }
// Output text encoding (UCS-2, ASCII7, Latin1, UTF-8, ZapfDingbats or Symbol)
if (options.encoding) { args.push('-enc'); args.push(options.encoding) }
// Output end of line convention (unix, dos or mac)
if (options.eol) { args.push('-eol'); args.push(options.eol) }
// Owner and User password (for encrypted files)
if (options.ownerPassword) { args.push('-opw'); args.push(options.ownerPassword) }
if (options.userPassword) { args.push('-upw'); args.push(options.userPassword) }
// finish up arguments
args.push(filePath)
args.push('-')
function splitPages (err, content) {
if (err) {
return cb(err)
}
var pages = content.split(/\f/)
if (!pages) {
return cb({
message: 'pdf-text-extract failed',
error: 'no text returned from the pdftotext command',
filePath: filePath,
stack: new Error().stack
})
}
// sometimes there can be an extract blank page on the end
var lastPage = pages[pages.length - 1]
if (!lastPage) {
pages.pop()
}
cb(null, pages)
}
// [feat-promise]
// if cb is not defined, then it's probably a promise-typed call
// in order to use promise, instantiation is required
if (!cb) {
this.pdfToTextCommand = pdfToTextCommand
this.args = args
this.options = options
this.splitPages = splitPages
this.filePath = filePath
} else {
streamResults(pdfToTextCommand, args, options, options.splitPages ? splitPages : cb)
}
}
/**
* spawns pdftotext and returns its output
*/
function streamResults (command, args, options, cb) {
var output = ''
var stderr = ''
var child = spawn(command, args, options)
child.stdout.setEncoding('utf8')
child.stderr.setEncoding('utf8')
child.stdout.on('data', stdoutHandler)
child.stderr.on('data', stderrHandler)
child.on('close', closeHandler)
function stdoutHandler (data) {
output += data
}
function stderrHandler (data) {
stderr += data
}
function closeHandler (code) {
if (code !== 0) {
return cb(new Error('pdf-text-extract command failed: ' + stderr))
}
cb(null, output)
}
}
/**
* [feat-promise]
* Promise support
*
* @param {Function} resolve
* @param {Function} [reject]
* @return {Request}
*/
pdfTextExtract.prototype.then = function (resolve, reject) {
if (!this._fullfilledPromise) {
var self = this
this._fullfilledPromise = new Promise(function (innerResolve, innerReject) {
streamResultsPromise(self.pdfToTextCommand, self.args, self.options, self.options.splitPages ? splitPagesPromise : resolve)
})
}
/**
* Duplicated from function splitPages of pdfTextExtract
*/
function splitPagesPromise (content) {
var pages = content.split(/\f/)
if (!pages) {
var ex = {
message: 'pdf-text-extract failed',
error: 'no text returned from the pdftotext command',
filePath: this.filePath,
stack: new Error().stack
}
throw ex
}
// sometimes there can be an extract blank page on the end
var lastPage = pages[pages.length - 1]
if (!lastPage) {
pages.pop()
}
resolve(pages)
}
/**
* Duplicated from function splitPages of streamResults
*/
function streamResultsPromise (command, args, options, cb) {
var output = ''
var stderr = ''
var child = spawn(command, args, options)
child.stdout.setEncoding('utf8')
child.stderr.setEncoding('utf8')
child.stdout.on('data', stdoutHandler)
child.stderr.on('data', stderrHandler)
child.on('close', closeHandler)
function stdoutHandler (data) {
output += data
}
function stderrHandler (data) {
stderr += data
}
function closeHandler (code) {
if (code !== 0) {
var ex = new Error('pdf-text-extract command failed: ' + stderr)
throw ex
}
cb(output)
}
}
return this._fullfilledPromise.then(resolve, reject)
}
module.exports = pdfTextExtract