This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
forked from pdffillerjs/pdffiller
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
174 lines (139 loc) · 6.18 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
/*
* File: index.js (pdffiller)
* Project: PDF Filler
* Date: May 2015.
*
* Description: This PDF filler module takes a data set and creates a filled out
* PDF file with the form fields populated.
*/
(function(){
var child_process = require('child_process'),
execFile = require('child_process').execFile,
fdf = require('utf8-fdf-generator'),
_ = 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, callback){
var regName = /FieldName: ([^\n]*)/,
regType = /FieldType: ([A-Za-z\t .]+)/,
regFlags = /FieldFlags: ([0-9\t .]+)/,
regValue = /FieldValue: ([^\n]*)/,
regDefault = /FieldValueDefault: ([^\n]*)/,
regOptions = /(FieldStateOption: ([^\n]*))/g,
fieldArray = [],
currField = {};
if(nameRegex !== null && (typeof nameRegex) == 'object' ) regName = nameRegex;
execFile( "pdftk", [sourceFile, "dump_data_fields_utf8"], function (error, stdout, stderr) {
if (error) {
console.log('exec error: ' + error);
return callback(error, null);
}
fields = stdout.toString().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'] = '';
}
if(field.match(regValue)){
currField['fieldValue'] = field.match(regValue)[1].trim()|| '';
}else{
currField['fieldValue'] = '';
}
if(field.match(regDefault)){
currField['fieldDefault'] = field.match(regDefault)[1].trim()|| '';
}else{
currField['fieldDefault'] = '';
}
if(field.match(regOptions)){
var matches = []
field.replace(/(FieldStateOption: ([^\n]*))/g, (m)=>{
var match = m.match(/FieldStateOption: ([^\n]*)/)[1].trim() || false;
if(match) matches.push(match);
})
currField['fieldOptions'] = matches
}else{
currField['fieldOptions'] = [];
}
fieldArray.push(currField);
});
return callback(null, fieldArray);
});
},
generateFDFTemplate: function( sourceFile, nameRegex, callback ){
this.generateFieldJson(sourceFile, nameRegex, function(err, _form_fields){
if (err) {
console.log('exec error: ' + err);
return callback(err, null);
}
return callback(null, this.convFieldJson2FDF(_form_fields));
}.bind(this));
},
fillFormWithOptions: function( sourceFile, destinationFile, fieldValues, shouldFlatten, tempFDFPath, callback ) {
//Generate the data from the field values.
var randomSequence = Math.random().toString(36).substring(7);
var currentTime = new Date().getTime();
var tempFDFFile = "temp_data" + currentTime + randomSequence + ".fdf",
tempFDF = (typeof tempFDFPath !== "undefined"? tempFDFPath + '/' + tempFDFFile: tempFDFFile),
formData = fdf.generator( fieldValues, tempFDF );
var args = [sourceFile, "fill_form", tempFDF, "output", destinationFile];
if (shouldFlatten) {
args.push("flatten");
}
execFile( "pdftk", args, function (error, stdout, stderr) {
if ( error ) {
console.log('exec error: ' + error);
return callback(error);
}
//Delete the temporary fdf file.
fs.unlink( tempFDF, function( err ) {
if ( err ) {
return callback(err);
}
// console.log( 'Sucessfully deleted temp file ' + tempFDF );
return callback();
});
} );
},
fillFormWithFlatten: function( sourceFile, destinationFile, fieldValues, shouldFlatten, callback ) {
this.fillFormWithOptions( sourceFile, destinationFile, fieldValues, shouldFlatten, undefined, callback);
},
fillForm: function( sourceFile, destinationFile, fieldValues, callback) {
this.fillFormWithFlatten( sourceFile, destinationFile, fieldValues, true, callback);
}
};
module.exports = pdffiller;
}())