forked from pdffillerjs/pdffiller
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
182 lines (135 loc) · 5.26 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
var spawn = require('child_process').spawn,
fdf = require("./fdf.js"),
_ = require('lodash'),
fs = require('fs');
var pdffiller = {
mapForm2PDF: function (formFields, convMap) {
var tmpFDFData = this.convFieldJson2FDF(formFields);
tmpFDFData = _.mapKeys(tmpFDFData, function (value, key) {
try {
convMap[key];
} catch (err) {
return key;
}
return convMap[key];
});
return tmpFDFData;
},
convFieldJson2FDF: function (fieldJson) {
var _keys = _.map(fieldJson, 'title'),
_values = _.map(fieldJson, 'fieldValue');
_values = _.map(_values, function (val) {
if (val === true) {
return 'Yes';
} else if (val === false) {
return 'Off';
}
return val;
});
var jsonObj = _.zipObject(_keys, _values);
return jsonObj;
},
generateFieldJson: function (sourceFile, nameRegex) {
var regName = /FieldName: ([^\n]*)/,
regType = /FieldType: ([A-Za-z\t .]+)/,
regFlags = /FieldFlags: ([0-9\t .]+)/,
fieldArray = [],
currField = {};
if (nameRegex !== null && (typeof nameRegex) == 'object') regName = nameRegex;
return new Promise(function (resolve, reject) {
var childProcess = spawn("pdftk", [sourceFile, "dump_data_fields_utf8"]);
var output = '';
childProcess.on('error', function (err) {
console.log('pdftk exec error: ' + err);
reject(err);
});
childProcess.stdout.on('data', function (data) {
output += data;
});
childProcess.stdout.on('end', function () {
fields = output.split("---").slice(1);
fields.forEach(function (field) {
currField = {};
currField['title'] = field.match(regName)[1].trim() || '';
if (field.match(regType)) {
currField['fieldType'] = field.match(regType)[1].trim() || '';
} else {
currField['fieldType'] = '';
}
if (field.match(regFlags)) {
currField['fieldFlags'] = field.match(regFlags)[1].trim() || '';
} else {
currField['fieldFlags'] = '';
}
currField['fieldValue'] = '';
fieldArray.push(currField);
});
resolve(fieldArray);
});
});
},
generateFDFTemplate: function (sourceFile, nameRegex) {
return new Promise(function (resolve, reject) {
this.generateFieldJson(sourceFile, nameRegex).then(function (_form_fields) {
var _keys = _.map(_form_fields, 'title'),
_values = _.map(_form_fields, 'fieldValue'),
jsonObj = _.zipObject(_keys, _values);
resolve(jsonObj);
}).catch(function (err) {
reject(err);
});
}.bind(this));
},
fillFormWithOptions: function (sourceFile, fieldValues, shouldFlatten) {
var promised = new Promise(function (resolve, reject) {
//Generate the data from the field values.
var FDFinput = fdf.createFdf(fieldValues);
var args = [sourceFile, "fill_form", '-', "output", '-'];
if (shouldFlatten) {
args.push("flatten");
}
var childProcess = spawn("pdftk", args);
childProcess.stderr.on('data', function (err) {
console.log('pdftk exec error: ' + err);
reject(err);
});
function sendData (data) {
childProcess.stdout.pause();
childProcess.stdout.unshift(data);
resolve(childProcess.stdout);
childProcess.stdout.removeListener('data', sendData);
};
childProcess.stdout.on('data', sendData);
// now pipe FDF to pdftk
childProcess.stdin.write(FDFinput);
childProcess.stdin.end();
});
// bind convenience method toFile for chaining
promised.toFile = toFile.bind(null, promised);
return promised;
},
fillFormWithFlatten: function (sourceFile, fieldValues, shouldFlatten) {
return this.fillFormWithOptions(sourceFile, fieldValues, shouldFlatten);
},
fillForm: function (sourceFile, fieldValues) {
return this.fillFormWithFlatten(sourceFile, fieldValues, true);
},
};
/**
* convenience chainable method for writing to a file (see examples)
**/
function toFile (promised, path) {
return new Promise(function (resolve, reject) {
promised.then(function(outputStream) {
var output = fs.createWriteStream(path);
outputStream.pipe(output);
outputStream.on('close', function() {
output.end();
resolve();
});
}).catch(function (error) {
reject(error);
});
});
}
module.exports = pdffiller;