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

View file

@ -41,7 +41,7 @@ var FormSchema = new Schema({
}, },
form_fields: [Schema.Types.Mixed], form_fields: [Schema.Types.Mixed],
submission: [{ submissions: [{
type: Schema.Types.ObjectId, type: Schema.Types.ObjectId,
ref: 'FormSubmission' ref: 'FormSubmission'
}], }],
@ -67,18 +67,17 @@ var FormSchema = new Schema({
}, },
}); });
//Move PDF to permanent location after first save
FormSchema.pre('save', function (next) { FormSchema.pre('save', function (next) {
// console.log(this.pdf); // console.log(this.pdf);
// debugger; // debugger;
//Move PDF to permanent location after first save
if(this.pdf){ if(this.pdf){
if(this.pdf.modified){ if(this.pdf.modified){
var new_filename = this.pdf.title.trim()+'_template.pdf'; 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.pdf.title.trim()),
var newDestination = path.join(config.pdfUploadPath+this.title+"/", this.pdf.title.trim()),
stat = null; stat = null;
try { try {
@ -92,18 +91,17 @@ FormSchema.pre('save', function (next) {
} }
console.log('about to move PDF'); console.log('about to move PDF');
fs.move(this.pdf.path, path.join(newDestination, new_filename), function (err) { fs.move(this.pdf.path, path.join(newDestination, new_filename), function (err) {
if (err) { if (err) {
console.error(err); console.error(err);
next( new Error(err.message) ); next( new Error(err.message) );
} }
console.log('PDF file successfully moved');
this.pdf.path = path.join(newDestination, new_filename); this.pdf.path = path.join(newDestination, new_filename);
this.pdf.name = new_filename; this.pdf.name = new_filename;
console.log('PDF file:'+this.pdf.name+' successfully moved to: '+this.pdf.path);
next(); 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; //Autogenerate FORM from PDF if 'isGenerated' flag is 'true'
// console.log('autogenerating form'); FormSchema.pre('save', function (next) {
var field;
if(this.isGenerated && this.pdf){
// try { var _typeConvMap = {
// pdfFiller.generateFieldJson(this.pdf.path, function(_form_fields){ 'Text': 'textfield',
'Button': 'checkbox'
};
// _form_fields.forEach(function(field){ var that = this;
// if(_pdfConvMap[ field.fieldType+'' ]){ console.log('autogenerating form');
// field.fieldType = _pdfConvMap[ field.fieldType+'' ];
// }
// field.created = Date.now();
// field.fieldValue = '';
// field.required = true;
// field.disabled = false;
// // field = new Field(field); try {
// // field.save() pdfFiller.generateFieldJson(this.pdf.path, function(_form_fields){
// });
// // console.log('NEW FORM_FIELDS: '); //Map PDF field names to FormField field names
// // console.log(_form_fields); for(var i = 0; i < _form_fields.length; i++){
field = _form_fields[i];
// // console.log('\n\nOLD FORM_FIELDS: '); //Convert types from FDF to 'FormField' types
// // console.log(that.form_fields); if(_typeConvMap[ field.fieldType+'' ]){
field.fieldType = _pdfConvMap[ field.fieldType+'' ];
}
// that.form_fields = _form_fields; field.created = Date.now();
// next(); field.fieldValue = '';
// }); field.required = true;
// } catch(err){ field.disabled = false;
// next( new Error(err.message) );
// }
// } // field = new Field(field);
// field.save()
}
// //Throw error if we encounter form with invalid type console.log('NEW FORM_FIELDS: ');
// next(); 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'), var _keys = _.pluck(this.form_fields, 'title'),
_values = _.pluck(this.form_fields, 'fieldValue'); _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(); next();
}); });
//Generate autofilled PDF if flags are set
FormSubmissionSchema.pre('save', function (next) { FormSubmissionSchema.pre('save', function (next) {
// debugger; // debugger;
var fdfData, dest_filename, dest_path; var fdfData, dest_filename, dest_path;
@ -82,23 +83,31 @@ FormSubmissionSchema.pre('save', function (next) {
Form.findById(that.form, function(err, _form){ Form.findById(that.form, function(err, _form){
if(err) next( new Error(err.mesasge) ); if(err) next( new Error(err.mesasge) );
this.title = _form.title; // that.title = _form.title;
console.log(_form); // console.log(_form);
//Create filled-out PDF, if there is a pdf template
if(_form.autofillPDFs){
dest_filename = this.title.trim()+'_submission_'+Date.now()+'.pdf'; if(true){ //_form.autofillPDFs){
dest_path = path.join(config.pdfUploadPath, this.title.trim(), dest_filename);
dest_filename = _form.title.trim()+'_submission_'+Date.now()+'.pdf';
dest_path = path.join(config.pdfUploadPath, dest_filename);
this.pdfFilePath = dest_path; this.pdfFilePath = dest_path;
console.log('autofillPDFs check'); // 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(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(); next();
}); });
} else { } else {

View file

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

View file

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

View file

@ -1,6 +1,6 @@
<div class="field row"> <div class="field row">
<br> <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> <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> <span class="required-error" ng-show="field.required && field.fieldValue == 0">(* required)</span>
<div class="field row"> <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.