-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.coffee
executable file
·53 lines (49 loc) · 1.71 KB
/
index.coffee
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
# A commonjs, coffeescript FDF generator inspired by Justin Koivisto's "KOIVI HTML Form to FDF Parser for PHP (C) 2004"
# http://koivi.com/fill-pdf-form-fields/tutorial.php
# @author Clark Van Oyen
md5 = require "MD5"
fs = require "fs"
# fdf.generate
# ---------
# Generates an FDF (Form Data File) from a provided input map from field name to field value.
#
# @param form The input map from field name to field value. ie) {name: 'Clark', type: 'superhero'}
# @param file The url or file path of the PDF file which this data is for.
# @result FDF representation of the form. You will usually write this to a file.
module.exports.generate = (form, file) ->
header = (String.fromCharCode 226) + (String.fromCharCode 227) + (String.fromCharCode 207) + (String.fromCharCode 211)
data = """%FDF-1.2
%#{header}
1 0 obj
<<
/FDF
<<
/Fields [
"""
for field, val of form
if form.hasOwnProperty(field)
if typeof val is "object" and Array.isArray(val)
data += """<<
/V(#{val})
/T["""
for opt in val
data += "(" + opt + ")"
data += "]>>"
else
data += """<<
/V(#{val})
/T(#{field})
>>
"""
#time_hash = md5 (new Date()).valueOf()
data += """]
>>
>>
endobj
trailer
<<
/Root 1 0 R
>>
%%EOF
"""
data