-
Notifications
You must be signed in to change notification settings - Fork 6
/
make-pdf-thumbnail.js
89 lines (71 loc) · 2.7 KB
/
make-pdf-thumbnail.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
(function() {
'use strict';
var spawn = require('child_process').spawn;
function spawnGsProcess(resolution, inputFilename, outputFilename) {
outputFilename = outputFilename || '-';
inputFilename = inputFilename || '-';
var gs = '/usr/bin/gs';
if (process.platform === 'darwin') {
gs = '/usr/local/bin/gs';
}
return spawn(gs, ['-dQUIET', '-dPARANOIDSAFER', '-dBATCH', '-dNOPAUSE',
'-dNOPROMPT', '-sDEVICE=png16m', '-dTextAlphaBits=4',
'-dGraphicsAlphaBits=4', '-r' + resolution,
'-dFirstPage=1', '-dLastPage=1', '-sOutputFile=' + outputFilename, inputFilename]);
}
/* This function converts a stream, containing pdf data, to a png thumbnail.
* Only the first page is used for the thumbnail.
*
* Resolution sets the device resolution.
*
* Callback must have the form of function(error, outputStream) ...
*/
module.exports.fromStream = function fromStream(readStream, resolution, callback) {
var gsProcess = spawnGsProcess(resolution);
readStream.pipe(gsProcess.stdin);
gsProcess.on('error', callback);
gsProcess.on('exit', function () {
callback(null, gsProcess.stdout);
});
return gsProcess.stdout;
};
/* This function converts a buffer, containing pdf data, to a png thumbnail.
* Only the first page is used for the thumbnail.
*
* Resolution sets the device resolution.
*
* Callback must have the form of function(error, outputBuffer) ...
*/
module.exports.fromBuffer = function fromBuffer(buffer, resolution, callback) {
var gsProcess = spawnGsProcess(resolution);
var buffers = [];
gsProcess.stdin.write(buffer, null, function () {
gsProcess.stdin.end();
});
gsProcess.stdout.on('data', function (pieceBuffer) {
buffers.push(pieceBuffer);
});
gsProcess.on('error', callback);
gsProcess.on('exit', function () {
callback(null, Buffer.concat(buffers));
});
gsProcess.stdout.on('error', function (err) {
callback(err);
});
};
module.exports.fromFile = function fromFile(inputFilename, outputFilename, resolution, callback) {
var gsProcess = spawnGsProcess(resolution, inputFilename, outputFilename);
gsProcess.on('error', callback);
gsProcess.on('exit', function () {
callback(null);
});
};
module.exports.fromStreamToFile = function toFile(inputStream, outputFilename, resolution, callback) {
var gsProcess = spawnGsProcess(resolution, '-', outputFilename);
inputStream.pipe(gsProcess.stdin);
gsProcess.on('error', callback);
gsProcess.on('exit', function () {
callback(null, outputFilename);
});
};
})();