forked from jasonphillips/pdffiller-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfdf.js
More file actions
85 lines (77 loc) · 1.61 KB
/
fdf.js
File metadata and controls
85 lines (77 loc) · 1.61 KB
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
// var fs = require('fs');
var _ = require('lodash')
const dateFormat = require('dateformat')
// only this sequence in FDF header requires char codes
var headerChars = Buffer.from(
(String.fromCharCode(226)) +
(String.fromCharCode(227)) +
(String.fromCharCode(207)) +
(String.fromCharCode(211)) +
'\n'
)
var header = Buffer.concat([
Buffer.from('%FDF-1.2\n'),
headerChars,
Buffer.from(
'1 0 obj \n' +
'<<\n' +
'/FDF \n' +
'<<\n' +
'/Fields [\n'
)
])
var footer = Buffer.from(
']\n' +
'>>\n' +
'>>\n' +
'endobj \n' +
'trailer\n' +
'\n' +
'<<\n' +
'/Root 1 0 R\n' +
'>>\n' +
'%%EOF\n'
)
var escapeString = function escapeString (value) {
// format dates
var out
if (value instanceof Date) {
out = dateFormat(value, 'dd.mm.yyyy')
} else {
out = value.toString()
}
out = out.replace(/\\/g, '\\\\')
out = out.replace(/\(/g, '\\(')
out = out.replace(/\)/g, '\\)')
out = out.toString('utf8')
return out
}
var concatBody = function concatBody (body, value, name) {
body = Buffer.concat([
body,
Buffer.from(
'<<\n' +
'/T (' +
escapeString(name) +
')\n' +
'/V (' +
escapeString(value) +
')\n' +
'>>\n'
)
])
return body
}
exports.createFdf = function (data) {
var body = Buffer.from([])
_.mapKeys(data, function (value, name) {
if (_.isObject(value)) {
_.mapKeys(value, function (value2, name2) {
body = concatBody(body, value2, name + '.' + name2)
})
} else {
body = concatBody(body, value, name)
}
})
return Buffer.concat([header, body, footer])
}