-
Notifications
You must be signed in to change notification settings - Fork 32
/
index.js
146 lines (118 loc) · 3.97 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
'use strict';
const fs = require('fs'),
path = require('path'),
spawn = require('child_process').spawn,
async = require('async'),
temp = require('temp');
exports.generateFdf = function(data) {
var header, body, footer;
header = new Buffer('%FDF-1.2\n\u00e2\u00e3\u00cf\u00d3\n1 0 obj \n<<\n/FDF \n<<\n/Fields [\n');
footer = new Buffer(']\n>>\n>>\nendobj \ntrailer\n\n<<\n/Root 1 0 R\n>>\n%%EOF\n');
body = new Buffer([]);
for(let key in data) {
var name = escapeFdf(key);
var value = escapeFdf(data[key]);
body = Buffer.concat([ body, new Buffer('<<\n/T (')]);
body = Buffer.concat([ body, name]);
body = Buffer.concat([ body, new Buffer(')\n/V (')]);
body = Buffer.concat([ body, value]);
body = Buffer.concat([ body, new Buffer(')\n>>\n')]);
}
var fdf = Buffer.concat([ header, body, footer ]);
return fdf;
};
exports.generatePdf = function(data, templatePath, extendArgs, callback) {
let tempNameResult = temp.path({suffix: '.pdf'}),
pdfPath = isAbsolute(templatePath) ? templatePath : path.join(__dirname, templatePath);
let normalized = normalizeArgs(extendArgs, callback);
extendArgs = normalized.args;
callback = normalized.callback;
let processArgs = [pdfPath, 'fill_form', '-', 'output', tempNameResult].concat(extendArgs);
let child = spawn('pdftk', processArgs);
handlePdftkError(child, callback);
handlePdftkExit(child, tempNameResult, callback);
writeFdfToPdftk(child, data);
};
// Escape data and return it as a buffer
function escapeFdf(data) {
let escaped = new Buffer([]);
let buf;
if(typeof data === 'string' || data instanceof Buffer) {
buf = new Buffer(data);
} else if(typeof data.toString === 'function') {
buf = new Buffer(data.toString());
} else {
buf = new Buffer(Object.prototype.toString.call(data));
}
for(let i=0; i<buf.length; i++) {
let c1 = String.fromCharCode(buf[i]);
let c2 = String.fromCharCode(buf[i+1]);
if(c1 === '(' || c1 === ')') {
escaped = Buffer.concat([escaped, new Buffer('\\' + c1)]);
} else if(c1 === '\r' && c2 === '\n') {
escaped = Buffer.concat([escaped, new Buffer('\r')]);
} else {
escaped = Buffer.concat([escaped, new Buffer([buf[i]])]);
}
}
return escaped;
}
function normalizeArgs(extendArgs, callback) {
// Check if extendArgs is our callback, adds backwards compat
if (typeof extendArgs === 'function') {
callback = extendArgs;
extendArgs = [];
}
else if (!(extendArgs instanceof Array)) {
extendArgs = [];
}
return { args: extendArgs, callback: callback };
}
function writeFdfToPdftk(child, data) {
child.stdin.write(exports.generateFdf(data));
child.stdin.end();
}
function handlePdftkError(child, callback) {
child.on('error', function (err) {
callback(err);
});
child.stderr.on('data', function (data) {
console.error('stderr: ' + data);
});
}
function handlePdftkExit(child, tempNameResult, callback) {
child.on('exit', createHandlePdftkExit(tempNameResult, callback));
}
function createHandlePdftkExit(tempNameResult, callback) {
return function(code) {
if ( code ) {
return callback(new Error('Non 0 exit code from pdftk spawn: ' + code));
}
async.waterfall([
(cb) => {
fs.readFile(tempNameResult, (err, filledPdf) => cb(err, filledPdf));
},
(filledPdf, cb) => {
fs.unlink(tempNameResult, (err) => cb(err, filledPdf));
}
],
function(err, result) {
callback(err, result);
});
};
}
function normalizeArgs(extendArgs, callback) {
// Check if extendArgs is our callback, adds backwards compat
if (typeof extendArgs === 'function') {
callback = extendArgs;
extendArgs = [];
}
else if (!(extendArgs instanceof Array)) {
extendArgs = [];
}
return { args: extendArgs, callback: callback };
}
function isAbsolute(Path) {
return (path.isAbsolute && path.isAbsolute(Path)) ||
(path.normalize(Path + '/') === path.normalize(path.resolve(Path) + '/'));
}