Skip to content

Bug with utf8-fdf-generator #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions fdf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var fs = require('fs');

var escapeString = function escapeString(value) {
var out = value.toString();
out = out.replace(/\\/g, "\\\\");
out = out.replace(/\(/g, "\\(");
out = out.replace(/\)/g, "\\)");
out = out.toString("utf8");
console.log(out)
return out;
}

exports.createFdf = function (data, fileName) {
var header, body, footer, dataKeys;

header = Buffer([]);
header = Buffer.concat([header, new Buffer("%FDF-1.2\n")]);
header = Buffer.concat([header, new Buffer((String.fromCharCode(226)) + (String.fromCharCode(227)) + (String.fromCharCode(207)) + (String.fromCharCode(211)) + "\n")]);
header = Buffer.concat([header, new Buffer("1 0 obj \n")]);
header = Buffer.concat([header, new Buffer("<<\n")]);
header = Buffer.concat([header, new Buffer("/FDF \n")]);
header = Buffer.concat([header, new Buffer("<<\n")]);
header = Buffer.concat([header, new Buffer("/Fields [\n")]);

footer = Buffer([]);
footer = Buffer.concat([footer, new Buffer("]\n")]);
footer = Buffer.concat([footer, new Buffer(">>\n")]);
footer = Buffer.concat([footer, new Buffer(">>\n")]);
footer = Buffer.concat([footer, new Buffer("endobj \n")]);
footer = Buffer.concat([footer, new Buffer("trailer\n")]);
footer = Buffer.concat([footer, new Buffer("\n")]);
footer = Buffer.concat([footer, new Buffer("<<\n")]);
footer = Buffer.concat([footer, new Buffer("/Root 1 0 R\n")]);
footer = Buffer.concat([footer, new Buffer(">>\n")]);
footer = Buffer.concat([footer, new Buffer("%%EOF\n")]);

dataKeys = Object.keys(data);

body = new Buffer([]);

for (var i = 0; i < dataKeys.length; i++) {
var name = dataKeys[i];
var value = data[name];

body = Buffer.concat([body, new Buffer("<<\n")]);
body = Buffer.concat([body, new Buffer("/T (")]);

body = Buffer.concat([body, new Buffer(escapeString(name))]);
body = Buffer.concat([body, new Buffer(")\n")]);
body = Buffer.concat([body, new Buffer("/V (")]);
body = Buffer.concat([body, new Buffer(escapeString(value))]);
body = Buffer.concat([body, new Buffer(")\n")]);
body = Buffer.concat([body, new Buffer(">>\n")]);
}

fs.writeFileSync(fileName, Buffer.concat([header, body, footer]));
}
94 changes: 47 additions & 47 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
/*
* 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.
*/
(function(){
* 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.
*/
(function () {
var child_process = require('child_process'),
execFile = require('child_process').execFile,
fdf = require('utf8-fdf-generator'),
fdf = require("./fdf.js"),
_ = require('lodash'),
fs = require('fs');

var pdffiller = {

mapForm2PDF: function( formFields, convMap ){
mapForm2PDF: function (formFields, convMap) {
var tmpFDFData = this.convFieldJson2FDF(formFields);
tmpFDFData = _.mapKeys(tmpFDFData, function(value, key){
tmpFDFData = _.mapKeys(tmpFDFData, function (value, key) {
try {
convMap[key];
} catch(err){
} catch (err) {

return key;
}
Expand All @@ -30,14 +30,14 @@
return tmpFDFData;
},

convFieldJson2FDF: function(fieldJson){
var _keys = _.pluck(fieldJson, 'title'),
_values = _.pluck(fieldJson, 'fieldValue');
convFieldJson2FDF: function (fieldJson) {
var _keys = _.map(fieldJson, 'title'),
_values = _.map(fieldJson, 'fieldValue');

_values = _.map(_values, function(val){
if(val === true){
_values = _.map(_values, function (val) {
if (val === true) {
return 'Yes';
}else if(val === false) {
} else if (val === false) {
return 'Off';
}
return val;
Expand All @@ -48,36 +48,36 @@
return jsonObj;
},

generateFieldJson: function( sourceFile, nameRegex, callback){
generateFieldJson: function (sourceFile, nameRegex, callback) {
var regName = /FieldName: ([^\n]*)/,
regType = /FieldType: ([A-Za-z\t .]+)/,
regFlags = /FieldFlags: ([0-9\t .]+)/,
fieldArray = [],
currField = {};

if(nameRegex !== null && (typeof nameRegex) == 'object' ) regName = nameRegex;
if (nameRegex !== null && (typeof nameRegex) == 'object') regName = nameRegex;

execFile( "pdftk", [sourceFile, "dump_data_fields_utf8"], function (error, stdout, stderr) {
execFile("pdftk", [sourceFile, "dump_data_fields_utf8"], function (error, stdout, stderr) {
if (error) {
console.log('exec error: ' + error);
return callback(error, null);
}

fields = stdout.toString().split("---").slice(1);
fields.forEach(function(field){
fields.forEach(function (field) {
currField = {};

currField['title'] = field.match(regName)[1].trim() || '';

if(field.match(regType)){
if (field.match(regType)) {
currField['fieldType'] = field.match(regType)[1].trim() || '';
}else {
} else {
currField['fieldType'] = '';
}

if(field.match(regFlags)){
currField['fieldFlags'] = field.match(regFlags)[1].trim()|| '';
}else{
if (field.match(regFlags)) {
currField['fieldFlags'] = field.match(regFlags)[1].trim() || '';
} else {
currField['fieldFlags'] = '';
}

Expand All @@ -90,62 +90,62 @@
});
},

generateFDFTemplate: function( sourceFile, nameRegex, callback ){
this.generateFieldJson(sourceFile, nameRegex, function(err, _form_fields){
generateFDFTemplate: function (sourceFile, nameRegex, callback) {
this.generateFieldJson(sourceFile, nameRegex, function (err, _form_fields) {
if (err) {
console.log('exec error: ' + err);
return callback(err, null);
console.log('exec error: ' + err);
return callback(err, null);
}
var _keys = _.pluck(_form_fields, 'title'),
_values = _.pluck(_form_fields, 'fieldValue'),
var _keys = _.map(_form_fields, 'title'),
_values = _.map(_form_fields, 'fieldValue'),
jsonObj = _.zipObject(_keys, _values);

return callback(null, jsonObj);

});
},

fillFormWithOptions: function( sourceFile, destinationFile, fieldValues, shouldFlatten, tempFDFPath, callback ) {
fillFormWithOptions: function (sourceFile, destinationFile, fieldValues, shouldFlatten, tempFDFPath, callback) {

//Generate the data from the field values.
var randomSequence = Math.random().toString(36).substring(7);
var currentTime = new Date().getTime();
var tempFDFFile = "data" + currentTime + randomSequence + ".fdf",
tempFDF = (typeof tempFDFPath !== "undefined"? tempFDFPath + '/' + tempFDFFile: tempFDFFile),
formData = fdf.generator( fieldValues, tempFDF );
var tempFDFFile = "data" + currentTime + randomSequence + ".fdf",
tempFDF = (typeof tempFDFPath !== "undefined" ? tempFDFPath + '/' + tempFDFFile : tempFDFFile),
formData = fdf.createFdf(fieldValues, tempFDF);

var args = [sourceFile, "fill_form", tempFDF, "output", destinationFile];
if (shouldFlatten) {
args.push("flatten");
}
execFile( "pdftk", args, function (error, stdout, stderr) {
execFile("pdftk", args, function (error, stdout, stderr) {

if ( error ) {
if (error) {
console.log('exec error: ' + error);
return callback(error);
}
//Delete the temporary fdf file.
fs.unlink( tempFDF, function( err ) {
fs.unlink(tempFDF, function (err) {

if ( err ) {
if (err) {
return callback(err);
}
// console.log( 'Sucessfully deleted temp file ' + tempFDF );
return callback();
});
} );
});
},

fillFormWithFlatten: function( sourceFile, destinationFile, fieldValues, shouldFlatten, callback ) {
this.fillFormWithOptions( sourceFile, destinationFile, fieldValues, shouldFlatten, undefined, callback);
fillFormWithFlatten: function (sourceFile, destinationFile, fieldValues, shouldFlatten, callback) {
this.fillFormWithOptions(sourceFile, destinationFile, fieldValues, shouldFlatten, undefined, callback);
},

fillForm: function( sourceFile, destinationFile, fieldValues, callback) {
this.fillFormWithFlatten( sourceFile, destinationFile, fieldValues, true, callback);
fillForm: function (sourceFile, destinationFile, fieldValues, callback) {
this.fillFormWithFlatten(sourceFile, destinationFile, fieldValues, true, callback);
}

};

module.exports = pdffiller;

}())
}())
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
"pdf"
],
"dependencies": {
"lodash": "~3.8.0",
"utf8-fdf-generator": "0.0.3"
"lodash": "^4.17.4"
},
"author": {
"name": "John Taylor and David Baldwynn"
Expand All @@ -30,7 +29,7 @@
"_from": "whitef0x0/pdffiller",
"_resolved": "git://github.com/whitef0x0/pdffiller.git#dc344fd81d90d0434dcc253d9ee14e4328b680dd",
"devDependencies": {
"mocha": "~2.4.5",
"should": "~6.0.1"
"mocha": "^3.2.0",
"should": "^11.1.2"
}
}
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('pdfFiller Tests', function(){
describe('fillForm()', function(){

var _data = {
"first_name" : "John",
"first_name" : "1) John",
"last_name" : "Doe",
"date" : "Jan 1, 2013",
"football" : "Off",
Expand Down