-
Notifications
You must be signed in to change notification settings - Fork 32
/
test.js
109 lines (94 loc) · 3.06 KB
/
test.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
var assert = require('assert'),
temporary = require('temporary'),
exec = require('child_process').exec,
fillPdf = require('../index.js'),
fs = require('fs'),
path = require('path');
var dir = new temporary.Dir();
describe('pdftk', function() {
it('should exist', function(done) {
var pdftk = exec('pdftk', ['--version'], function(err, stdout, stderr) {
assert(!err && !stderr, 'pdftk is not installed, fill-pdf requires pdftk to be installed and added to your PATH');
done(err);
});;
});
});
describe('fill-pdf instantiation', function() {
it('should be an object', function() {
assert.equal(typeof fillPdf, 'object');
});
it('should have method: generateFdf', function() {
assert.equal(typeof fillPdf.generateFdf, 'function');
});
it('should have method: generatePdf', function() {
assert.equal(typeof fillPdf.generatePdf, 'function');
});
});
describe('Temp File Generation', function() {
it('should generate tmp directory object', function() {
assert.equal(typeof dir, 'object', true);
});
});
describe('fill-pdf', function() {
describe('#generatePdf()', function() {
var formData = {
Name_Last: 'Doe',
Name_First: 'John',
Name_Middle: '(Sir) Francis',
Telephone_Home: 1112223333,
Sex: 'MALE',
Address_2: '1234 Some Rd',
City: 'Annapolis',
STATE: 'MD',
ZIP: 22334,
PHD: 'Yes',
TRADE_CERTIFICATE: 'Yes',
PARENS: 'a)\n>>\n<<\n/T (Name_Last)\n/V (VALUE NOT ESCAPED)\n>>\n<<\n/T (asdf)\n/V ('
}
it('should accept test/resources/test.pdf', function(done) {
fillPdf.generatePdf(formData, 'test/resources/test.pdf', function(err, pdf) {
// Uncomment if you want to open the file to view
/*
fs.writeFileSync('test-result.pdf', pdf);
exec('open test-result.pdf');
*/
assert(pdf);
done(err);
});
});
it('should accept an absolute path as a file location', function(done) {
fillPdf.generatePdf(formData, path.resolve(__dirname + '/resources/test.pdf'), function(err, pdf) {
assert(pdf);
done(err);
});
});
it('should accept additional arguments', function(done) {
fillPdf.generatePdf(formData, 'test/resources/test.pdf', ["flatten"], function(err, pdf) {
assert(pdf);
done(err);
});
});
// TODO: Do PDF parsing to verify fill worked
});
describe('#generateFdf()', function() {
var formData = {
// "Doe" in UTF-16LE
Name_Last: new Buffer([0x44, 0x00, 0x6f, 0x00, 0x65, 0x00]),
Name_First: 'John',
Name_Middle: '(Sir) Francis',
Telephone_Home: 1112223333,
Sex: 'MALE',
Address_2: '1234 Some Rd',
City: 'Annapolis',
STATE: 'MD',
ZIP: 22334,
PHD: 'Yes',
TRADE_CERTIFICATE: 'Yes'
};
it('should generate valid data', function() {
var actual = fillPdf.generateFdf(formData);
var expected = fs.readFileSync('test/resources/test.fdf');
assert(actual.compare(expected) === 0);
});
});
});