got pdf generation working
This commit is contained in:
parent
0090c98ad2
commit
71d315a2d0
|
@ -205,9 +205,10 @@ exports.listSubmissions = function(req, res) {
|
||||||
* Update a form
|
* Update a form
|
||||||
*/
|
*/
|
||||||
exports.update = function(req, res) {
|
exports.update = function(req, res) {
|
||||||
|
console.log('in form.update()');
|
||||||
|
|
||||||
var form = req.form;
|
var form = req.form;
|
||||||
console.log(req.body.form);
|
// console.log(req.body.form);
|
||||||
form = _.extend(form, req.body.form);
|
form = _.extend(form, req.body.form);
|
||||||
form.admin = req.user;
|
form.admin = req.user;
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ var mongoose = require('mongoose'),
|
||||||
config = require('../../config/config'),
|
config = require('../../config/config'),
|
||||||
path = require('path'),
|
path = require('path'),
|
||||||
fs = require('fs-extra'),
|
fs = require('fs-extra'),
|
||||||
|
async = require('async'),
|
||||||
Field = mongoose.model('Field', FieldSchema);
|
Field = mongoose.model('Field', FieldSchema);
|
||||||
|
|
||||||
|
|
||||||
|
@ -73,9 +74,20 @@ var FormSchema = new Schema({
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//Delete template PDF of current Form
|
||||||
|
FormSchema.pre('remove', function (next) {
|
||||||
|
if(this.pdf){
|
||||||
|
//Delete template form
|
||||||
|
fs.unlink(this.pdf.path, function(err){
|
||||||
|
if (err) throw err;
|
||||||
|
console.log('successfully deleted', this.pdf.path);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
//Create folder for user's pdfs
|
//Create folder for user's pdfs
|
||||||
FormSchema.pre('save', function (next) {
|
FormSchema.pre('save', function (next) {
|
||||||
var newDestination = path.join(config.pdfUploadPath, this.admin.username.trim()),
|
var newDestination = path.join(config.pdfUploadPath, this.admin.username.replace(/ /g,'')),
|
||||||
stat = null;
|
stat = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -91,149 +103,200 @@ FormSchema.pre('save', function (next) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//Update lastModified and created everytime we save
|
||||||
//Update lastModified everytime we save
|
|
||||||
FormSchema.pre('save', function (next) {
|
FormSchema.pre('save', function (next) {
|
||||||
this.lastModified = Date.now();
|
var now = new Date();
|
||||||
|
this.lastModified = now;
|
||||||
|
if( !this.created ){
|
||||||
|
this.created = now;
|
||||||
|
}
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
//Move PDF to permanent location after new template is uploaded
|
//Move PDF to permanent location after new template is uploaded
|
||||||
FormSchema.pre('save', function (next) {
|
FormSchema.pre('save', function (next) {
|
||||||
|
|
||||||
console.log(this.pdf);
|
|
||||||
console.log("isModified: "+this.isModified('pdf'));
|
|
||||||
var that = this;
|
|
||||||
if(this.pdf){
|
if(this.pdf){
|
||||||
if(this.isModified('pdf')){
|
var that = this;
|
||||||
|
async.series([
|
||||||
|
function(callback){
|
||||||
|
if(that.isModified('pdf')){
|
||||||
|
console.log('about to move PDF');
|
||||||
|
|
||||||
var new_filename = this.title.trim()+'_template.pdf';
|
var new_filename = that.title.replace(/ /g,'')+'_template.pdf';
|
||||||
|
|
||||||
var newDestination = path.join(config.pdfUploadPath, this.admin.username.trim(), this.title.trim()),
|
var newDestination = path.join(config.pdfUploadPath, that.admin.username.replace(/ /g,''), that.title.replace(/ /g,'')),
|
||||||
stat = null;
|
stat = null;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
stat = fs.statSync(newDestination);
|
stat = fs.statSync(newDestination);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
fs.mkdirSync(newDestination);
|
fs.mkdirSync(newDestination);
|
||||||
}
|
}
|
||||||
if (stat && !stat.isDirectory()) {
|
if (stat && !stat.isDirectory()) {
|
||||||
console.log('Directory '+newDestination+' cannot be created');
|
console.log('Directory '+newDestination+' cannot be created');
|
||||||
next( new Error('Directory cannot be created because an inode of a different type exists at "' + config.pdfUploadPath + '"') );
|
callback( new Error('Directory cannot be created because an inode of a different type exists at "' + config.pdfUploadPath + '"') );
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log('about to move PDF');
|
console.log('about to move PDF');
|
||||||
//Move pdf to permanent location
|
|
||||||
fs.move(this.pdf.path, path.join(newDestination, new_filename), function (err) {
|
|
||||||
// if (err) {
|
|
||||||
// console.error(err);
|
|
||||||
// next( new Error(err.message) );
|
|
||||||
// }
|
|
||||||
|
|
||||||
// //Delete old pdf file
|
fs.move(that.pdf.path, path.join(newDestination, new_filename), function (err) {
|
||||||
// fs.unlink(that.pdf.path, function(err){
|
if (err) {
|
||||||
if (err) {
|
console.error(err);
|
||||||
console.error(err);
|
callback( new Error(err.message) );
|
||||||
|
}
|
||||||
|
that.pdf.path = path.join(newDestination, new_filename);
|
||||||
|
that.pdf.name = new_filename;
|
||||||
|
|
||||||
|
console.log('\n\n PDF file:'+that.pdf.name+' successfully moved to: '+that.pdf.path);
|
||||||
|
|
||||||
|
callback(null,null);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
callback(null,null);
|
||||||
|
},
|
||||||
|
function(callback){
|
||||||
|
if(that.isGenerated){
|
||||||
|
that.pdf.path = path.join(config.pdfUploadPath, that.admin.username.replace(/ /g,''), that.title.replace(/ /g,''), that.title.replace(/ /g,'')+'_template.pdf');
|
||||||
|
that.pdf.name = that.title.replace(/ /g,'')+'_template.pdf';
|
||||||
|
var _typeConvMap = {
|
||||||
|
'Multiline': 'textarea',
|
||||||
|
'Text': 'textfield',
|
||||||
|
'Button': 'checkbox',
|
||||||
|
'Choice': 'radio',
|
||||||
|
'Password': 'password',
|
||||||
|
'FileSelect': 'filefield',
|
||||||
|
'Radio': 'radio'
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log('autogenerating form');
|
||||||
|
console.log(that.pdf.path);
|
||||||
|
|
||||||
|
try {
|
||||||
|
pdfFiller.generateFieldJson(that.pdf.path, function(_form_fields){
|
||||||
|
|
||||||
|
//Map PDF field names to FormField field names
|
||||||
|
for(var i = 0; i < _form_fields.length; i++){
|
||||||
|
var field = _form_fields[i];
|
||||||
|
|
||||||
|
//Convert types from FDF to 'FormField' types
|
||||||
|
if(_typeConvMap[ field.fieldType+'' ]){
|
||||||
|
field.fieldType = _typeConvMap[ field.fieldType+'' ];
|
||||||
|
}
|
||||||
|
|
||||||
|
field.fieldValue = '';
|
||||||
|
field.created = Date.now();
|
||||||
|
field.required = true;
|
||||||
|
field.disabled = false;
|
||||||
|
|
||||||
|
// field = new Field(field);
|
||||||
|
// field.save(function(err) {
|
||||||
|
// if (err) {
|
||||||
|
// console.error(err.message);
|
||||||
|
// throw new Error(err.message);
|
||||||
|
// });
|
||||||
|
// } else {
|
||||||
|
// _form_fields[i] = that;
|
||||||
|
// }
|
||||||
|
// });
|
||||||
|
_form_fields[i] = field;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('NEW FORM_FIELDS: ');
|
||||||
|
console.log(_form_fields);
|
||||||
|
|
||||||
|
console.log('\n\nOLD FORM_FIELDS: ');
|
||||||
|
console.log(that.form_fields);
|
||||||
|
|
||||||
|
that.form_fields = _form_fields;
|
||||||
|
callback();
|
||||||
|
});
|
||||||
|
} catch(err){
|
||||||
next( new Error(err.message) );
|
next( new Error(err.message) );
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// console.log('successfully deleted', that.pdf.path);
|
callback(null,null);
|
||||||
|
}
|
||||||
that.pdf.path = path.join(newDestination, new_filename);
|
], function(err, results) {
|
||||||
that.pdf.name = new_filename;
|
if(err){
|
||||||
|
next(new Error({
|
||||||
console.log('\n\n PDF file:'+that.pdf.name+' successfully moved to: '+that.pdf.path);
|
message: err.message
|
||||||
|
}));
|
||||||
next();
|
}
|
||||||
// });
|
next();
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
}else {
|
|
||||||
next();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
//Delete template PDF of current Form
|
|
||||||
FormSchema.pre('remove', function (next) {
|
|
||||||
if(this.pdf){
|
|
||||||
//Delete template form
|
|
||||||
fs.unlink(this.pdf.path, function(err){
|
|
||||||
if (err) throw err;
|
|
||||||
console.log('successfully deleted', this.pdf.path);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
//Autogenerate FORM from PDF if 'isGenerated' flag is 'true'
|
|
||||||
FormSchema.pre('save', function (next) {
|
|
||||||
var field, _form_fields;
|
|
||||||
|
|
||||||
if(this.isGenerated && this.pdf){
|
|
||||||
|
|
||||||
var _typeConvMap = {
|
|
||||||
'Multiline': 'textarea',
|
|
||||||
'Text': 'textfield',
|
|
||||||
'Button': 'checkbox',
|
|
||||||
'Choice': 'radio',
|
|
||||||
'Password': 'password',
|
|
||||||
'FileSelect': 'filefield',
|
|
||||||
'Radio': 'radio'
|
|
||||||
};
|
|
||||||
|
|
||||||
var that = this;
|
|
||||||
console.log('autogenerating form');
|
|
||||||
|
|
||||||
try {
|
|
||||||
pdfFiller.generateFieldJson(this.pdf.path, function(_form_fields){
|
|
||||||
|
|
||||||
//Map PDF field names to FormField field names
|
|
||||||
for(var i = 0; i < _form_fields.length; i++){
|
|
||||||
field = _form_fields[i];
|
|
||||||
|
|
||||||
//Convert types from FDF to 'FormField' types
|
|
||||||
if(_typeConvMap[ field.fieldType+'' ]){
|
|
||||||
field.fieldType = _typeConvMap[ field.fieldType+'' ];
|
|
||||||
}
|
|
||||||
|
|
||||||
field.fieldValue = '';
|
|
||||||
field.created = Date.now();
|
|
||||||
field.required = true;
|
|
||||||
field.disabled = false;
|
|
||||||
|
|
||||||
// field = new Field(field);
|
|
||||||
// field.save(function(err) {
|
|
||||||
// if (err) {
|
|
||||||
// console.error(err.message);
|
|
||||||
// throw new Error(err.message);
|
|
||||||
// });
|
|
||||||
// } else {
|
|
||||||
// _form_fields[i] = this;
|
|
||||||
// }
|
|
||||||
// });
|
|
||||||
_form_fields[i] = field;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log('NEW FORM_FIELDS: ');
|
|
||||||
console.log(_form_fields);
|
|
||||||
|
|
||||||
console.log('\n\nOLD FORM_FIELDS: ');
|
|
||||||
console.log(that.form_fields);
|
|
||||||
|
|
||||||
that.form_fields = _form_fields;
|
|
||||||
next();
|
|
||||||
});
|
|
||||||
} catch(err){
|
|
||||||
next( new Error(err.message) );
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//Throw error if we encounter form with invalid type
|
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//Autogenerate Form_fields from PDF if 'isGenerated' flag is set
|
||||||
|
// FormSchema.pre('save', function (next) {
|
||||||
|
// var field, _form_fields;
|
||||||
|
|
||||||
|
// if(this.pdf){
|
||||||
|
// if(this.isGenerated){
|
||||||
|
|
||||||
|
// var _typeConvMap = {
|
||||||
|
// 'Multiline': 'textarea',
|
||||||
|
// 'Text': 'textfield',
|
||||||
|
// 'Button': 'checkbox',
|
||||||
|
// 'Choice': 'radio',
|
||||||
|
// 'Password': 'password',
|
||||||
|
// 'FileSelect': 'filefield',
|
||||||
|
// 'Radio': 'radio'
|
||||||
|
// };
|
||||||
|
|
||||||
|
// var that = this;
|
||||||
|
// console.log('autogenerating form');
|
||||||
|
|
||||||
|
// try {
|
||||||
|
// pdfFiller.generateFieldJson(this.pdf.path, function(_form_fields){
|
||||||
|
|
||||||
|
// //Map PDF field names to FormField field names
|
||||||
|
// for(var i = 0; i < _form_fields.length; i++){
|
||||||
|
// field = _form_fields[i];
|
||||||
|
|
||||||
|
// //Convert types from FDF to 'FormField' types
|
||||||
|
// if(_typeConvMap[ field.fieldType+'' ]){
|
||||||
|
// field.fieldType = _typeConvMap[ field.fieldType+'' ];
|
||||||
|
// }
|
||||||
|
|
||||||
|
// field.fieldValue = '';
|
||||||
|
// field.created = Date.now();
|
||||||
|
// field.required = true;
|
||||||
|
// field.disabled = false;
|
||||||
|
|
||||||
|
// // field = new Field(field);
|
||||||
|
// // field.save(function(err) {
|
||||||
|
// // if (err) {
|
||||||
|
// // console.error(err.message);
|
||||||
|
// // throw new Error(err.message);
|
||||||
|
// // });
|
||||||
|
// // } else {
|
||||||
|
// // _form_fields[i] = this;
|
||||||
|
// // }
|
||||||
|
// // });
|
||||||
|
// _form_fields[i] = field;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// console.log('NEW FORM_FIELDS: ');
|
||||||
|
// console.log(_form_fields);
|
||||||
|
|
||||||
|
// console.log('\n\nOLD FORM_FIELDS: ');
|
||||||
|
// console.log(that.form_fields);
|
||||||
|
|
||||||
|
// that.form_fields = _form_fields;
|
||||||
|
// next();
|
||||||
|
// });
|
||||||
|
// } catch(err){
|
||||||
|
// next( new Error(err.message) );
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// next();
|
||||||
|
// });
|
||||||
|
|
||||||
FormSchema.methods.convertToFDF = function (cb) {
|
FormSchema.methods.convertToFDF = function (cb) {
|
||||||
var _keys = _.pluck(this.form_fields, 'title'),
|
var _keys = _.pluck(this.form_fields, 'title'),
|
||||||
_values = _.pluck(this.form_fields, 'fieldValue');
|
_values = _.pluck(this.form_fields, 'fieldValue');
|
||||||
|
|
|
@ -90,12 +90,11 @@ FormSubmissionSchema.pre('save', function (next) {
|
||||||
|
|
||||||
if(_form.autofillPDFs){
|
if(_form.autofillPDFs){
|
||||||
|
|
||||||
dest_filename = _form.title.trim()+'_submission_'+Date.now()+'.pdf';
|
dest_filename = _form.title.replace(/ /g,'')+'_submission_'+Date.now()+'.pdf';
|
||||||
dest_path = path.join(config.pdfUploadPath, dest_filename);
|
dest_path = path.join(config.pdfUploadPath, dest_filename);
|
||||||
|
|
||||||
this.pdfFilePath = dest_path;
|
this.pdfFilePath = dest_path;
|
||||||
|
|
||||||
// console.log('autofillPDFs check');
|
|
||||||
|
|
||||||
pdfFiller.fillForm(_form.pdf.path, dest_path, this.fdfData, function(err){
|
pdfFiller.fillForm(_form.pdf.path, dest_path, this.fdfData, function(err){
|
||||||
console.log('fdfData: \n');
|
console.log('fdfData: \n');
|
||||||
|
@ -108,7 +107,7 @@ FormSubmissionSchema.pre('save', function (next) {
|
||||||
console.log('\n err.message: '+err.message);
|
console.log('\n err.message: '+err.message);
|
||||||
next( new Error(err.message) );
|
next( new Error(err.message) );
|
||||||
}
|
}
|
||||||
console.log('Field data from Form: '+_form.title.trim()+' outputed to new PDF: '+dest_path);
|
console.log('Field data from Form: '+_form.title.replace(/ /g,'')+' outputed to new PDF: '+dest_path);
|
||||||
next();
|
next();
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -5,9 +5,10 @@ angular.module('forms').controller('EditFormController', ['$scope', '$state', '$
|
||||||
|
|
||||||
$scope.isNewForm = false;
|
$scope.isNewForm = false;
|
||||||
$scope.pdfLoading = false;
|
$scope.pdfLoading = false;
|
||||||
var _current_upload = null;
|
|
||||||
$scope.log = '';
|
|
||||||
$scope.form = {};
|
$scope.form = {};
|
||||||
|
$scope.log = '';
|
||||||
|
var _current_upload = null;
|
||||||
|
|
||||||
// Get current form if it exists, or create new one
|
// Get current form if it exists, or create new one
|
||||||
if($stateParams.formId){
|
if($stateParams.formId){
|
||||||
|
@ -24,12 +25,13 @@ angular.module('forms').controller('EditFormController', ['$scope', '$state', '$
|
||||||
$scope.cancelUpload = function(){
|
$scope.cancelUpload = function(){
|
||||||
_current_upload.abort();
|
_current_upload.abort();
|
||||||
$scope.pdfLoading = false;
|
$scope.pdfLoading = false;
|
||||||
|
$scope.removePDF();
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.removePDF = function(){
|
$scope.removePDF = function(){
|
||||||
$scope.form.pdf = null;
|
$scope.form.pdf = null;
|
||||||
$scope.isGenerated = false;
|
$scope.form.isGenerated = false;
|
||||||
$scope.autofillPDFs = false;
|
$scope.form.autofillPDFs = false;
|
||||||
|
|
||||||
console.log('form.pdf: '+$scope.form.pdf+' REMOVED');
|
console.log('form.pdf: '+$scope.form.pdf+' REMOVED');
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue