got output PDFs to save

This commit is contained in:
David Baldwynn 2015-06-29 19:14:43 -07:00
parent 87b351efea
commit c9e97cefdc
23 changed files with 102 additions and 81 deletions

View file

@ -1,4 +1,4 @@
'use strict';
/**
* Module dependencies.
@ -38,7 +38,7 @@ exports.create = function(req, res) {
/**
* Upload PDF
*/
exports.uploadPDF = function(req, res) {
exports.uploadPDF = function(req, res, next) {
var parser = new PDFParser(),
pdfFile = req.files.file;
@ -54,21 +54,21 @@ exports.uploadPDF = function(req, res) {
}
fs.exists(pdfFile.path, function(exists) {
if(exists) {
console.log("UPLOADING FILE \N\N");
// res.end('Got your file!');
// next()
return res.status(200);
console.log('UPLOADING FILE \N\N');
res.end('Got your file!');
next();
// return res.status(200);
} else {
// res.end('DID NOT get your file!');
// next()
return res.status(400);
res.end('DID NOT get your file!');
next();
// return res.status(400);
}
});
}
// next(new Error('FILE NOT UPLOADED'));
next(new Error('FILE NOT UPLOADED'));
return res.status(400);
// return res.status(400);
// res.json(pdfFile);
};
@ -77,6 +77,7 @@ exports.uploadPDF = function(req, res) {
*/
exports.read = function(req, res) {
// console.log(req.form);
console.log(req.form.form_fields[7]);
res.json(req.form);
};
@ -86,36 +87,38 @@ exports.read = function(req, res) {
exports.createSubmission = function(req, res) {
var submission = new FormSubmission(),
form = req.form, fdfData;
form = req.form,
fdfData,
fdfTemplate;
submission.form = form;
submission.admin = req.user;
submission.form_fields = req.body.form_fields;
submission.title = req.body.title;
submission.timeElapsed = req.body.timeElapsed;
// submission.ipAddr = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
if (form.isGenerated){
fdfData = form.convertToJSON();
fdfTemplate = form.convertToFDF();
} else {
try {
fdfData = pdfFiller.mapForm2PDF(form.convertToJSON(), form.pdfFieldMap);
fdfTemplate = pdfFiller.mapForm2PDF(form.convertToFDF(), form.pdfFieldMap);
} catch(err){
throw new Error(err.message);
}
}
fdfData = pdfFiller.fillFdfTemplate(fdfTemplate, submission.form_fields, null);
submission.fdfData = fdfData;
//Create new file
pdfFiller.fillForm( form.pdf.path, config.pdfUploadPath+form.title+"/"+form.title+"_"+Date.now()+"_submission.pdf", fdfData, function() {
console.log("\n\n\n fdfData");
console.log(fdfData);
});
// submission.ipAddr = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
// pdfFiller.fillForm( form.pdf.path, config.pdfUploadPath+form.title+'/'+form.title+'_'+Date.now()+'_submission.pdf', fdfData, function() {
// console.log('\n\n\n fdfData');
// console.log(fdfData);
// console.log('\n\n\n :\n');
// console.log(req.body);
submission.save(function(err){
if (err) {
@ -127,6 +130,7 @@ exports.createSubmission = function(req, res) {
return res.status(200);
}
});
// });
};

View file

@ -41,7 +41,7 @@ var FormSchema = new Schema({
},
form_fields: [Schema.Types.Mixed],
submission: [{
submissions: [{
type: Schema.Types.ObjectId,
ref: 'FormSubmission'
}],
@ -67,18 +67,17 @@ var FormSchema = new Schema({
},
});
//Move PDF to permanent location after first save
FormSchema.pre('save', function (next) {
// console.log(this.pdf);
// debugger;
//Move PDF to permanent location after first save
if(this.pdf){
if(this.pdf.modified){
var new_filename = this.pdf.title.trim()+'_template.pdf';
// TODO: DAVID - need to remove dependence on relative paths
var newDestination = path.join(config.pdfUploadPath+this.title+"/", this.pdf.title.trim()),
var newDestination = path.join(config.pdfUploadPath, this.pdf.title.trim()),
stat = null;
try {
@ -92,18 +91,17 @@ FormSchema.pre('save', function (next) {
}
console.log('about to move PDF');
fs.move(this.pdf.path, path.join(newDestination, new_filename), function (err) {
if (err) {
console.error(err);
next( new Error(err.message) );
}
console.log('PDF file successfully moved');
this.pdf.path = path.join(newDestination, new_filename);
this.pdf.name = new_filename;
console.log('PDF file:'+this.pdf.name+' successfully moved to: '+this.pdf.path);
next();
});
@ -113,55 +111,63 @@ FormSchema.pre('save', function (next) {
}
});
// FormSchema.pre('save', function (next) {
// //Autogenerate FORM from PDF
// if(this.isGenerated && this.pdf && this.autofillPDFs){
// this.autofillPDFs = false;
// var _pdfConvMap = {
// 'Text': 'textfield',
// 'Button': 'checkbox'
// };
// var that = this;
// console.log('autogenerating form');
//Autogenerate FORM from PDF if 'isGenerated' flag is 'true'
FormSchema.pre('save', function (next) {
var field;
if(this.isGenerated && this.pdf){
// try {
// pdfFiller.generateFieldJson(this.pdf.path, function(_form_fields){
var _typeConvMap = {
'Text': 'textfield',
'Button': 'checkbox'
};
// _form_fields.forEach(function(field){
// if(_pdfConvMap[ field.fieldType+'' ]){
// field.fieldType = _pdfConvMap[ field.fieldType+'' ];
// }
// field.created = Date.now();
// field.fieldValue = '';
// field.required = true;
// field.disabled = false;
var that = this;
console.log('autogenerating form');
// // field = new Field(field);
// // field.save()
// });
try {
pdfFiller.generateFieldJson(this.pdf.path, function(_form_fields){
// // console.log('NEW FORM_FIELDS: ');
// // console.log(_form_fields);
//Map PDF field names to FormField field names
for(var i = 0; i < _form_fields.length; i++){
field = _form_fields[i];
// // console.log('\n\nOLD FORM_FIELDS: ');
// // console.log(that.form_fields);
//Convert types from FDF to 'FormField' types
if(_typeConvMap[ field.fieldType+'' ]){
field.fieldType = _pdfConvMap[ field.fieldType+'' ];
}
// that.form_fields = _form_fields;
// next();
// });
// } catch(err){
// next( new Error(err.message) );
// }
field.created = Date.now();
field.fieldValue = '';
field.required = true;
field.disabled = false;
// }
// field = new Field(field);
// field.save()
}
// //Throw error if we encounter form with invalid type
// next();
console.log('NEW FORM_FIELDS: ');
console.log(_form_fields);
// });
console.log('\n\nOLD FORM_FIELDS: ');
console.log(that.form_fields);
FormSchema.methods.convertToJSON = function (cb) {
that.form_fields = _form_fields;
next();
});
} catch(err){
next( new Error(err.message) );
}
}
//Throw error if we encounter form with invalid type
next();
});
FormSchema.methods.convertToFDF = function (cb) {
var _keys = _.pluck(this.form_fields, 'title'),
_values = _.pluck(this.form_fields, 'fieldValue');

View file

@ -70,10 +70,11 @@ FormSubmissionSchema.pre('save', function (next){
});
}
}
console.log('ipAddr check');
// console.log('ipAddr check');
next();
});
//Generate autofilled PDF if flags are set
FormSubmissionSchema.pre('save', function (next) {
// debugger;
var fdfData, dest_filename, dest_path;
@ -82,23 +83,31 @@ FormSubmissionSchema.pre('save', function (next) {
Form.findById(that.form, function(err, _form){
if(err) next( new Error(err.mesasge) );
this.title = _form.title;
console.log(_form);
//Create filled-out PDF, if there is a pdf template
if(_form.autofillPDFs){
// that.title = _form.title;
// console.log(_form);
dest_filename = this.title.trim()+'_submission_'+Date.now()+'.pdf';
dest_path = path.join(config.pdfUploadPath, this.title.trim(), dest_filename);
if(true){ //_form.autofillPDFs){
dest_filename = _form.title.trim()+'_submission_'+Date.now()+'.pdf';
dest_path = path.join(config.pdfUploadPath, dest_filename);
this.pdfFilePath = dest_path;
console.log('autofillPDFs check');
// console.log('autofillPDFs check');
pdfFiller.fillForm(_form.pdf.path, dest_path, this.fdfData, function(err){
console.log("fdfData: \n");
console.log(that.fdfData);
if(err) next( new Error(err.message) );
// console.log("_form.pdf.path: "+_form.pdf.path);
// console.log("dest_path: "+dest_path);
console.log('Field data from Form: '+this.title.trim()+' outputed to new PDF: '+dest_path);
if(err) {
console.log("\n err.message: "+err.message);
next( new Error(err.message) );
}
console.log('Field data from Form: '+_form.title.trim()+' outputed to new PDF: '+dest_path);
next();
});
} else {

View file

@ -13,6 +13,8 @@ angular.module('forms').directive('formDirective', ['$http', '$timeout', 'timeCo
console.log($scope.form.timeElapsed);
// console.log($scope.form.form_fields[7]);
$http.post('/forms/'+$scope.form._id,$scope.form).
success(function(data, status, headers){
console.log('form submitted successfully');

View file

@ -4,7 +4,7 @@ angular.module('forms').service('timeCounter', [
function(){
var _startTime, _endTime, that=this;
this.timeSpent;
this.timeSpent = 0;
this.startClock = function(){
_startTime = Date.now();

View file

@ -1,6 +1,6 @@
<div class="field row">
<br>
<input ng-model="field.fieldValue" id="{{field.client_id}}" type="checkbox" ng-true-value="1" ng-false-value="0" ng-required="field.required" ng-disabled="field.disabled"/>
<input ng-model="field.fieldValue" id="{{field.client_id}}" type="checkbox" ng-init="field.fieldValue = false" ng-required="field.required" ng-disabled="field.disabled"/>
<label class="form-field-label" for="{{field.client_id}}" ng-cloak>{{field.title}}</label>
<span class="required-error" ng-show="field.required && field.fieldValue == 0">(* required)</span>
<div class="field row">

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.