forked from tcr/scissors
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscissors.js
458 lines (412 loc) · 12.4 KB
/
scissors.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
var fs = require('fs');
var spawn = require('child_process').spawn;
var path = require('path');
var Stream = require('stream').Stream;
var BufferStream = require('bufferstream');
var temp = require('temp').track();
var async = require('async');
var PDFTKCMD = 'pdftk';
// Calls functions once a promise has been delivered.
// Queue functions by using promise(yourCallback); Deliver the promise using promise.deliver().
// Once the promise has been delivered, promise(yourCallback) immediately calls.
function promise () {
var queue = [], args = null;
var promise = function (fn) {
if (promise.delivered) {
process.nextTick(function () {
fn.apply(null, args);
});
} else {
queue.push(fn);
}
}
promise.deliver = function () {
args = arguments, promise.delivered = true;
queue.splice(0, queue.length).forEach(function (fn) {
process.nextTick(function () {
fn.apply(null, args);
});
});
}
return promise;
}
function proxyStream (a, b) {
if (a && b) {
a
.on('data', b.emit.bind(b, 'data'))
.on('end', b.emit.bind(b, 'end'))
.on('error', b.emit.bind(b, 'error'));
}
}
// scissors
function Command (input, ready) {
this.input = input;
this.commands = [];
this.onready = promise();
if (ready !== false) {
this.onready.deliver();
}
}
Command.prototype._copy = function () {
var cmd = new Command();
cmd.input = this.input;
cmd.commands = this.commands.slice();
cmd.onready = this.onready;
cmd.userPass = this.userPass;
cmd.ownerPass = this.ownerPass;
cmd.allow = this.allow;
return cmd;
}
Command.prototype._push = function (command) {
this.commands.push(command);
this.input = command;
return this;
}
Command.prototype._input = function () {
// Non-existant files will throw an error, assume full paths.
try {
return typeof this.input == 'string' ? fs.realpathSync(this.input) : '-';
} catch (e) {
return this.input;
}
};
// Cloning commands.
Command.prototype.encrypt = function(options) {
var cmd = this._copy();
cmd.ownerPass = options.ownerPass;
cmd.userPass = options.userPass;
cmd.allow = options.allow;
return cmd;
};
Command.prototype.range = function (min, max) {
var cmd = this._copy();
return cmd._push([
PDFTKCMD, cmd._input(),
'cat', min + (max === null ? '' : '-' + max),
'output', '-'
]);
};
Command.prototype.pages = function () {
var args = Array.prototype.slice.call(arguments);
var cmd = this._copy();
return cmd._push([
PDFTKCMD, cmd._input(),
'cat'].concat(args.map(Number), [
'output', '-'
]));
};
Command.prototype.odd = function (min, max) {
var cmd = this._copy();
return cmd._push([
PDFTKCMD, cmd._input(),
'cat', 'odd',
'output', '-'
]);
};
Command.prototype.even = function (min, max) {
var cmd = this._copy();
return cmd._push([
PDFTKCMD, cmd._input(),
'cat', 'even',
'output', '-'
]);
};
Command.prototype.reverse = function (min, max) {
var cmd = this._copy();
return cmd._push([
PDFTKCMD, cmd._input(),
'cat', 'end-1',
'output', '-'
]);
};
Command.prototype.rotate = function (amount) {
var cmd = this._copy();
this.buffer();
var amount = Number(amount) % 360, dir = null;
switch (amount) {
case 90: case -270: dir = 'R'; break;
case 180: case -180: dir = 'D'; break;
case -90: case 270: dir = 'L'; break;
default: return this;
}
return cmd._push([
PDFTKCMD, cmd._input(),
'cat', '1-end' + dir,
'output', '-'
]);
};
Command.prototype.compress = function () {
var cmd = this._copy();
return cmd._push([
PDFTKCMD, cmd._input(), 'output', '-',
'compress'
]);
};
Command.prototype.uncompress = function () {
var cmd = this._copy();
return cmd._push([
PDFTKCMD, cmd._input(), 'output', '-',
'uncompress'
]);
};
Command.prototype.repair = function () {
// pdftk extraction of a single page causes issues for some reason.
// "repairing" using pdftk fixes this.
var cmd = this._copy();
var args = [
PDFTKCMD, this._input(), 'output', '-',
];
// Don't double-repair.
if (JSON.stringify(this.commands[this.commands.length - 1]) != JSON.stringify(args)) {
cmd._push(args);
}
return cmd;
};
Command.prototype.crop = function (l, b, r, t) {
var cmd = this.uncompress();
return cmd._push([path.join(__dirname, 'bin/crop.js'), l, b, r, t]);
};
Command.prototype.pdfStream = function () {
var cmd = this.repair();
return cmd._exec();
};
Command.prototype.pngStream = function (dpi) {
var cmd = this.repair();
cmd._push([path.join(__dirname, 'bin/rasterize.js'), this._input(), 'pdf', 1, dpi || 72]);
var stream = cmd._exec();
return stream;
};
// Consumes this.pdfStream()
Command.prototype._commandStream = function () {
var stream = new BufferStream({
size: 'flexible'
});
var buf = [];
stream.split('\n', function (line) {
var tokens = String(line).split(/[ ](?=[^\)]*?(?:\(|$))/);
var data = (function () {
switch (tokens[0]) {
case 'S': return {type: 'string', x: +tokens[1], y: +tokens[2], string: tokens[3].replace(/^.|.$/g, '')};
case 'F': return {type: 'font', height: +tokens[1], width: +tokens[2], font: (tokens[3] || '').replace(/^.|.$/g, '')};
case 'P': return {type: 'endpage'};
case 'C': return {type: 'color', r: +tokens[1], g: +tokens[2], b: +tokens[3]};
case 'I': return {type: 'image', x: +tokens[1], y: +tokens[2], width: +tokens[3], height: +tokens[4]};
case 'R': return {type: 'rectangle', x: +tokens[1], y: +tokens[2], width: +tokens[3], height: +tokens[4]};
}
})();
if (data) {
stream.emit('data', data);
}
});
var gs = spawn('gs', [
'-q', '-dNODISPLAY',
'-P-',
'-dSAFER',
'-dDELAYBIND',
'-dWRITESYSTEMDICT',
'-dCOMPLEX', path.join(__dirname, 'contrib/ps2ascii.ps'),
'-', '-c', 'quit']);
this.pdfStream().pipe(gs.stdin);
gs.stdout.pipe(stream);
gs.stderr.on('data', function (data) {
console.error('gs encountered an error:\n', String(data));
});
gs.on('error',function(err) {
console.error('child process gs encountered an error:\n');
throw err;
});
gs.on('exit', function (code) {
stream.emit('end');
});
return stream;
};
// Consumes this.pdfStream()
Command.prototype.contentStream = function () {
function isNextStringPartOfLastString (b, a, font) {
// NOTE: This is a completely arbitrary hueristic.
// I wouldn't trust it to not break.
return Math.abs(a.y - b.y) < 50 && Math.abs((a.x + (a.string.length*(font.width / 3))) - b.x) < (font.width + 10);
}
function decode (str) {
return String(str).replace(/\\(\d{3}|.)/g, function (str, esc) {
if (esc.length == 3) {
return String.fromCharCode(parseInt(esc, 8));
} else {
try {
return JSON.parse('"' + str + '"');
} catch (e) {
return esc;
}
}
});
}
var stream = new Stream(), str = '', first = null, last = null, font = null, color = null, imgindex = 0;
this._commandStream().on('data', function (cmd) {
if (cmd.type == 'string') {
if (!last || isNextStringPartOfLastString(cmd, last, font)) {
str += decode(cmd.string);
} else {
stream.emit('data', {
type: 'string', x: (first || cmd).x, y: (first || cmd).y,
string: str, font: font, color: color
});
str = decode(cmd.string);
first = cmd;
}
last = cmd;
} else if (cmd.type == 'image') {
cmd.index = imgindex++;
stream.emit('data', cmd);
} else if (cmd.type == 'font') {
delete cmd.type;
font = cmd;
} else if (cmd.type == 'color') {
delete cmd.type;
color = cmd;
}
}).on('end', function () {
if (str) {
stream.emit('data', {
type: 'string', x: (first || cmd).x, y: (first || cmd).y,
string: str, font: font, color: color
});
str = '';
process.nextTick(function() {
stream.emit('end');
});
}
});
return stream;
};
// Consumes this.pdfStream()
Command.prototype.textStream = function () {
var stream = new Stream();
this.contentStream().on('data', function (cmd) {
if (cmd.type == 'string') {
stream.emit('data', cmd.string);
}
});
this.contentStream().on('end', function () {
stream.emit('end');
});
return stream;
};
// Consumes this.pdfStream()
Command.prototype.extractImageStream = function (i) {
// NOTE: This is pretty costly and uses another dependency.
// Preferrably, this would be done in Ghostscript.
i = i || 0;
var stream = new Stream();
if (!this._pdfimages) {
var callback = this._pdfimages = promise();
temp.mkdir('pdfimages', function (err, dirPath) {
this.pdfStream()
.pipe(fs.createWriteStream(path.join(dirPath, 'file.pdf')))
.on('close', function () {
var prog = spawn('pdfimages', ['-j', dirPath + '/file.pdf', dirPath + '/A']);
prog.stderr.on('data', function (data) {
process.stderr.write('pdfimages: ' + String(data));
});
prog.on('exit', function (code) {
if (code) {
console.error('pdfimages exited with failure code:', code);
throw new Error('pdfimages failed.');
}
var files = fs.readdirSync(dirPath).slice(0, -1).map(function (file) {
return dirPath + '/' + file;
});
callback.deliver(files);
});
}.bind(this))
}.bind(this));
}
// Add callback to promise.
this._pdfimages(function (pdfimages) {
if (!pdfimages[i]) {
stream.emit('error', new Error('Image ' + i + ' out of bounds.'));
return;
}
proxyStream(fs.createReadStream(pdfimages[i]), stream);
});
return stream;
};
Command.prototype._exec = function () {
var self = this;
var stream = new Stream(), commands = this.commands.slice();
this.onready(function () {
proxyStream(commands.reduce(function (input, command) {
var args = command.slice(1);
if (self.userPass || self.ownerPass) {
args.splice(1,0,'input_pw',(self.ownerPass||self.userPass));
}
if (self.ownerPass) {
args.push('owner_pw',self.ownerPass);
}
if (self.userPass) {
args.push('user_pw',self.userPass);
}
if (self.allow) {
args.push('allow');
args = args.concat(self.allow);
}
var prog = spawn(command[0], args);
if (input) {
input.pipe(prog.stdin);
}
prog.stderr.on('data', function (data) {
process.stderr.write(command[0].match(/[^\/]*$/)[0] + ': ' + String(data));
});
prog.on('exit', function (code) {
if (code) {
console.error(command[0], 'exited with failure code:', code);
}
});
return prog.stdout;
}, null), stream);
});
return stream;
}
var scissors = function (path) {
return new Command(path);
}
var joinTemp = temp.mkdirSync('pdfimages'), joinindex = 0;
scissors.join = function () {
var args = Array.prototype.slice.call(arguments);
var outfile = joinTemp + '/' + (joinindex++) + '.pdf';
var pdf = new Command(outfile, false);
async.map(args, function (arg, next) {
var file = joinTemp + '/' + (joinindex++) + '.pdf';
arg.pdfStream()
.pipe(fs.createWriteStream(file))
.on('close', function () {
next(null, file);
});
}, function (err, files) {
command = [PDFTKCMD].concat(files, ['output', outfile]);
var prog = spawn(command[0], command.slice(1));
prog.stderr.on('data', function (data) {
process.stderr.write(command[0].match(/[^\/]*$/)[0] + ': ' + String(data));
});
prog.on('exit', function (code) {
if (code) {
console.error(command[0], 'exited with failure code:', code);
}
// PDF is now ready.
pdf.onready.deliver();
});
});
return pdf;
}
module.exports = scissors;
/*
references
## References
* http://hzqtc.github.com/2012/04/pdf-tools-merging-extracting-and-cropping.html
* http://documentcloud.github.com/docsplit/
* http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/
* http://segfault.in/2010/07/pdf-manipulations-and-conversions-from-linux-command-prompt/
* http://www.maths.ox.ac.uk/help/faqs/files/manipulating-pdf-files
* http://stackoverflow.com/questions/11754556/ghostscript-convert-a-pdf-and-output-in-a-textfile
* http://right-sock.net/linux/better-convert-pdf-to-jpg-using-ghost-script/
* http://stackoverflow.com/questions/12484353/how-to-crop-a-section-of-a-pdf-file-to-png-using-ghostscript?lq=1
*/