forked from pdffillerjs/pdffiller
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
142 lines (113 loc) · 3.23 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
/*
* 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.
*/
"use strict";
const child_process = require("child_process");
const exec = child_process.exec;
const spawn = child_process.spawn;
const fdf = require("fdf");
const PDFDocument = require("pdfkit");
const pdffiller = {
getFdf: function (sourceFile, callback) {
const pdftkCommand = `pdftk ${sourceFile} generate_fdf output -`;
exec(pdftkCommand, (err, stdout) => {
if (err) {
console.log(err);
return callback(err);
}
return callback(null, stdout.toString());
});
},
getFormFields: function (sourceFile, callback) {
const pdftkCommand = `pdftk ${sourceFile} dump_data_fields_utf8`;
exec(pdftkCommand, (err, stdout) => {
if (err) {
console.log(err);
return callback(err);
}
const fieldSections = stdout.toString().split("---").slice(1);
const formFields = fieldSections.map((fieldSection) => {
const fieldAttributes = fieldSection.split(/\n/);
const result = {};
fieldAttributes.forEach((fieldAttribute) => {
const packedAttribute = fieldAttribute.split(/:/);
let key = packedAttribute[0];
let value = packedAttribute[1];
if (key) {
key = key[0].toLowerCase() + key.substring(1);
result[key.trim()] = value.trim();
}
});
return result;
});
return callback(null, formFields);
});
},
fillForm: function (inputFile, data, callback) {
// Generate FDF template from data
const fdfData = fdf.generate(data);
const child = spawn("pdftk", [inputFile, "fill_form", "-", "output", "-", "flatten"]);
let buffers = [];
let buffersLength = 0;
child.stdout.on("data", (data) => {
buffers.push(data);
buffersLength += data.length;
});
child.on("error", (err) => {
return callback(err);
});
child.on("close", () => {
data = Buffer.concat(buffers, buffersLength);
return callback(null, data);
});
child.stdin.write(fdfData);
child.stdin.end();
},
stamp: function (inputFile, image, imageLocation, callback) {
loadImageToPdf(image, imageLocation, (err, imagePdf) => {
if (err) {
return callback(err);
}
let buffers = [];
let buffersLength = 0;
const child = spawn("pdftk", [inputFile, "stamp", "-", "output", "-", "flatten"]);
child.stdout.on("data", (data) => {
buffers.push(data);
buffersLength += data.length;
});
child.on("error", (err) => {
return callback(err);
});
child.on("close", () => {
let data = Buffer.concat(buffers, buffersLength);
return callback(null, data);
});
child.stdin.write(imagePdf);
child.stdin.end();
});
},
};
function loadImageToPdf(image, imageLocation, callback) {
const imagePdf = new PDFDocument();
let buffers = [];
let buffersLength = 0;
imagePdf.on("data", (data) => {
buffers.push(data);
buffersLength += data.length;
});
imagePdf.on("error", (err) => {
return callback(err);
});
imagePdf.on("end", () => {
let data = Buffer.concat(buffers, buffersLength);
return callback(null, data);
});
imagePdf.image(image, imageLocation);
imagePdf.end();
}
module.exports = pdffiller;