1 | var shell = require('shelljs');
|
2 | var utils = require('./utils');
|
3 |
|
4 |
|
5 | shell.config.silent = true;
|
6 |
|
7 | function pdfinfo (filename, options) {
|
8 | this.options = options || {};
|
9 | this.options.additional = ['"' + filename + '"'];
|
10 |
|
11 | pdfinfo.prototype.add_options = function(optionArray) {
|
12 | if (typeof optionArray.length !== undefined) {
|
13 | var self = this;
|
14 | optionArray.forEach(function(el) {
|
15 | if (el.indexOf(' ') > 0) {
|
16 | var values = el.split(' ');
|
17 | self.options.additional.push(values[0], values[1]);
|
18 | } else {
|
19 | self.options.additional.push(el);
|
20 | }
|
21 | });
|
22 | }
|
23 | return this;
|
24 | };
|
25 |
|
26 | pdfinfo.prototype.getInfoSync = function() {
|
27 | var self = this;
|
28 | var child = shell.exec('pdfinfo ' + self.options.additional.join(' '));
|
29 | if (child.code === 0) {
|
30 | return utils.parse(child.output);
|
31 | }
|
32 | else {
|
33 | if (!shell.which('pdfinfo')) {
|
34 | throw new Error('Sorry, this script requires pdfinfo.');
|
35 | }
|
36 | throw new Error("pdfinfo error: "+ child.output);
|
37 | }
|
38 | }
|
39 |
|
40 | pdfinfo.prototype.getSync = function() {
|
41 | console.warn("\033[31m`getSync` is now obsolete please use `getInfoSync` instead. Eventually `getSync` will be soon removed.\033[0m");
|
42 | return this.getInfoSync();
|
43 | }
|
44 |
|
45 | pdfinfo.prototype.getInfo = function(cb) {
|
46 | var self = this;
|
47 | var child = shell.exec('pdfinfo ' + self.options.additional.join(' '), function(code, data) {
|
48 | if (code === 0) {
|
49 | data = utils.parse(data);
|
50 | if (cb && typeof cb === "function") {
|
51 | cb(null, data, self.options.additional);
|
52 | }
|
53 | }
|
54 | else {
|
55 | var err;
|
56 | if (!shell.which('pdfinfo')) {
|
57 | err = new Error('pdfinfo (poppler-utils) is missing. Hint: sudo apt-get install poppler-utils');
|
58 | }
|
59 | else {
|
60 | err = new Error(data);
|
61 | }
|
62 | if (cb && typeof cb === "function") {
|
63 | cb(err, null, self.options.addtional);
|
64 | }
|
65 | }
|
66 | });
|
67 | }
|
68 |
|
69 | pdfinfo.prototype.get = function() {
|
70 | console.warn("\033[31m`get` is now obsolete please use `getInfo` instead. Eventually `get` will be soon removed.\033[0m");
|
71 | var self = this;
|
72 | var child = shell.exec('pdfinfo ' + self.options.additional.join(' '), function(code, data) {
|
73 | if (code === 0) {
|
74 | if (self.options.success
|
75 | && typeof self.options.success === "function") {
|
76 | self.options.success(utils.parse(data));
|
77 | }
|
78 | }
|
79 | else {
|
80 | if (!shell.which('pdfinfo')) {
|
81 | echo('Sorry, this script requires pdfinfo.');
|
82 | }
|
83 | if (self.options.error
|
84 | && typeof self.options.error === "function") {
|
85 | self.options.error(data);
|
86 | }
|
87 | }
|
88 |
|
89 | });
|
90 | };
|
91 |
|
92 | pdfinfo.prototype.error = function(callback) {
|
93 | this.options.error = callback;
|
94 | return this;
|
95 | };
|
96 |
|
97 | pdfinfo.prototype.success = function(callback) {
|
98 | this.options.success = callback;
|
99 | return this;
|
100 | };
|
101 | }
|
102 |
|
103 |
|
104 | exports = module.exports = function(filename, args) {
|
105 | return new pdfinfo(filename, args);
|
106 | };
|