Skip to content

Instantly share code, notes, and snippets.

@dennythecoder
Last active May 24, 2018 00:40
Show Gist options
  • Select an option

  • Save dennythecoder/745f4703168a19b9cd08843517a3f80c to your computer and use it in GitHub Desktop.

Select an option

Save dennythecoder/745f4703168a19b9cd08843517a3f80c to your computer and use it in GitHub Desktop.
PDFJS Wrapper to handle DD Form 2875.
/*
Usage:
const file = document.getElementById('a_file').files[0];
const fh = new FileHandler(file);
console.log(fh.fields.name, fh.fields.email);
*/
var FileHandler = (function() {
var formDefinitions = {
fm2875_1: {
name: '2875',
fieldNames: [
null, null, null, null, 'date', 'system', 'systemLocation', 'organization', 'officeSymbol', 'phone', 'email', 'jobTitle', //12
'address', null, null, null, //16
null, null, null, null, //20
null, null, null, 'justification', //24
null, null, null, null, //28
null, null, null, null, //32
null, null, 'name', null, //36
null, null, null, null, //40
null, null, null, null, //44
null, null
],
meta: {
Author: "WHS/ESD/IMD",
CreationDate: "D:20140122142956-05'00'",
Creator: "Adobe LiveCycle Designer ES 9.0",
Title: "DD Form 2875, System Authorization Access Request, August 2009"
}
},
fm2875_2: {
name: '2875',
fieldNames: [
null, null, null, null,
null, 'date', 'system', 'systemLocation', //8
'name', 'organization', 'officeSymbol', 'phone', //12
'email', 'title', 'address', null, //16
null, null, null, null, //20
null, null, null, null, //24
null, 'justification', null, null, //28
null, null, null, null, //32
null, null, null, null, //36
null, null, null, null, //40
null, null, null, null, //44
null, null
],
meta: {
Author: "WHS/ESD/IMD",
CreationDate: "D:20090817144620-04'00'",
Creator: "PScript5.dll Version 5.2",
Title: "DD Form 2875, System Authorization Access Request, August 2009"
}
},
fm1297: {
name: '1297',
fields: function(fh) {
return new Promise(function(resolve, reject) {
fh.pages.forEach(function(page, i) {
page.getTextContent().then(function(tc) {});
if (i >= fh.pdf.numPages) {
resolve();
}
});
});
},
meta: {
CreationDate: "D:20171011152854-04'00'",
Creator: "Adobe LiveCycle Designer 11.0"
}
}
};
var util = {
convertDataURIToBinary: function convertDataURIToBinary(dataURI) {
var BASE64_MARKER = ';base64,';
var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
var base64 = dataURI.substring(base64Index);
var raw = window.atob(base64);
var rawLength = raw.length;
var array = new Uint8Array(new ArrayBuffer(rawLength));
for (var i = 0; i < rawLength; i++) {
array[i] = raw.charCodeAt(i);
}
return array;
}
}
function FileHandler(file) {
this.fields = [];
this.fr = new FileReader();
this.fr.onload = this.onload.bind(this);
this.fr.readAsDataURL(file);
this.isInitialized = false;
this.oninit = function() {};
this.pages = [];
}
FileHandler.prototype.onload = function(e) {
var c = this;
var fileAsArray = util.convertDataURIToBinary(e.target.result);
PDFJS.getDocument(fileAsArray).then(c.processPDF.bind(this)).then(c.getFormDefinition.bind(this)).then(c.setFields.bind(this)).then(function() {
c.isInitialized = true;
c.oninit(c);
});
}
FileHandler.prototype.setFields = function(def) {
var c = this;
this.name = def.name;
this.def = def;
return Promise(function(resolve, reject) {
if (def.fields) {
def.fields(c).then(function() {
resolve();
});
} else {
for (var i = 0; i < c.fields.length; i++) {
if (def.fieldNames[i]) {
c.fields[def.fieldNames[i]] = c.fields[i].fieldValue;
}
}
resolve();
}
});
}
FileHandler.prototype.formDefinitions = formDefinitions;
FileHandler.prototype.processPage = function(page) {
var c = this;
this.pages.push(page);
return page.getAnnotations({
intent: 'display'
}).then(function(annotations) {
annotations.filter(function(an) {
return an.fieldValue && true;
}).forEach(function(field) {
c.fields.push(field);
});
});
}
FileHandler.prototype.processPDF = function(pdf) {
this.pdf = pdf;
var c = this;
return new Promise(function(resolve, reject) {
for (var i = 1; i <= pdf.numPages; i++) {
pdf.getPage(i).then(c.processPage.bind(c)).then(function() {
if (i >= pdf.numPages) {
pdf.getMetadata().then(resolve);
}
});
}
});
}
FileHandler.prototype.doesMetaMatch = function(defMeta, meta) {
for (var key in defMeta) {
if (defMeta[key] !== meta[key]) {
return false;
}
}
return true;
}
FileHandler.prototype.getFormDefinition = function(meta) {
var info = meta.info;
for (var key in this.formDefinitions) {
var def = this.formDefinitions[key];
var defMeta = this.formDefinitions[key].meta;
if (this.doesMetaMatch(defMeta, info)) {
return this.formDefinitions[key];
}
}
console.warn("No matching meta definitions were found.");
};
return FileHandler;
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment