aboutsummaryrefslogtreecommitdiff
path: root/simpleSigning/src/main/resources/js/pdfas.js
diff options
context:
space:
mode:
Diffstat (limited to 'simpleSigning/src/main/resources/js/pdfas.js')
-rw-r--r--simpleSigning/src/main/resources/js/pdfas.js241
1 files changed, 241 insertions, 0 deletions
diff --git a/simpleSigning/src/main/resources/js/pdfas.js b/simpleSigning/src/main/resources/js/pdfas.js
new file mode 100644
index 0000000..3b74994
--- /dev/null
+++ b/simpleSigning/src/main/resources/js/pdfas.js
@@ -0,0 +1,241 @@
+/**
+ *
+ */
+
+/**
+ * Operation Decsription
+ */
+var operation = {
+ /**
+ * Content might be empty, can contain text or an URL
+ */
+ content: "Beispiel Text",
+
+ /**
+ * Connector: all PDF-AS Connector values
+ */
+ connector: "local",
+
+ /**
+ * DIV id where to place to UI
+ */
+ uiId: ""
+};
+
+var pdfAsOrigin = "##REPLACE##DOMAIN##";
+
+var pdfAsUrl = "##REPLACE##DOMAIN####REPLACE##CONTEXT##/Start";
+
+var pdfAs = {};
+
+/**
+ * create signed PDF Document containing
+ * @param content The text content of the PDF Document
+ * @returns
+ */
+pdfAs.signText = function(content, error, success) {
+ this.createAsyncSignature(content.content, content.connector, content.uiId, pdfAsUrl, "TEXT", success, error);
+};
+
+pdfAs.signPdf = function(content, error, success) {
+ this.createAsyncSignature(content.content, content.connector, content.uiId, pdfAsUrl, "PDF", success, error);
+};
+
+var postman = new NoJQueryPostMessageMixin('postMessage', 'receiveMessage');
+
+var guid = (function() {
+ function s4() {
+ return Math.floor((1 + Math.random()) * 0x10000)
+ .toString(16)
+ .substring(1);
+ }
+ return function() {
+ return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
+ s4() + '-' + s4() + s4() + s4();
+ };
+})();
+
+function encodeQueryData(data)
+{
+ var ret = [];
+ for (var d in data) {
+ if (typeof data[d] != "undefined") {
+ ret.push(encodeURIComponent(d) + "=" + encodeURIComponent(data[d]));
+ }
+ }
+ return ret.join("&");
+};
+
+function addDataToForm(data, form) {
+ var ret = [];
+ for (var d in data) {
+ if (typeof data[d] != "undefined") {
+ var input = document.createElement('input');
+ input.type = 'hidden';
+ input.name = d;
+ input.value = data[d];
+ form.appendChild(input);
+ }
+ }
+}
+
+
+pdfAs.cbMap = {};
+
+pdfAs.autoResize = function(eventId, height, width){
+ var newheight = height;
+ var newwidth = width;
+
+ console.log("Resising iframe " + width + "x" + height);
+
+ newheight = newheight + 20;
+
+ document.getElementById(eventId).height= (newheight) + "px";
+ document.getElementById(eventId).width= (newwidth) + "px";
+};
+
+pdfAs.createAsyncSignature = function(pdfUrl, connector, divID, pdfAsURL, type, success, error) {
+ // generate EventID
+ var eventId = guid();
+
+ var parameters = {
+ PDFURL: pdfUrl,
+ CONNECTOR: connector,
+ EVENTID: eventId,
+ TYPE: type,
+ TARGETURL: document.URL,
+ NEW: "1"
+ };
+
+ /*var querystring = encodeQueryData(parameters);
+
+ console.log("Querystring: " + querystring);
+
+ var url = pdfAsURL;
+
+ if(pdfAsURL.indexOf('?') === -1) {
+ url = url + "?" + querystring;
+ } else {
+ url = url + "&" + querystring;
+ }
+
+ console.log("URL: " + url);*/
+ var iframeid = eventId + "_iframe";
+ var iframe = document.createElement('iframe');
+ iframe.width = '95%';
+ iframe.height = '95%';
+ iframe.frameBorder = '0';
+ //iframe.src = url;
+ iframe.id = iframeid;
+ iframe.name = iframeid;
+ iframe.style.overflow="auto";
+ iframe.scrolling="no";
+
+ if (typeof divID === "undefined") {
+ // Create a div
+ var custDiv = eventId + "_div";
+ var docHeight = window.innerHeight; //grab the height of the page
+ var scrollTop = window.screenTop; //grab the px value from the top of the page to where you're scrolling
+ var div = document.createElement('div');
+ document.body.appendChild(div);
+ div.id = custDiv;
+ div.style.display = 'block';
+ div.style.zIndex = 2;
+ div.style.background = '#FFF';
+ div.style.position = 'absolute';
+ div.style.width = '45em';
+ //div.style.height = '300px';
+ div.style.top = '50%';
+ div.style.left = '50%';
+ div.style.margin = '-150px 0 0 -22.5em';
+ div.style.textAlign = 'center';
+ div.style.borderRadius = '4px';
+ div.style.boxShadow = '0 0 5px rgba(0,0,0,0.9)';
+
+ div.appendChild(iframe);
+ } else {
+ var element = document.getElementById(divID);
+ element.appendChild(iframe);
+ }
+
+ var form = document.createElement('form');
+ form.method = "POST";
+ form.action = pdfAsURL;
+ form.target = iframeid;
+ form.enctype = "application/x-www-form-urlencoded";
+
+ addDataToForm(parameters, form);
+
+ this.addCallback(eventId, custDiv, error, success);
+ document.body.appendChild(form);
+ form.submit();
+};
+
+pdfAs.addCallback = function(eventId, custDiv, error, success) {
+ pdfAs.cbMap.eventId = {suc: success, err: error, div: custDiv};
+};
+
+pdfAs.checkOverlay = function(eventId) {
+ if(pdfAs.cbMap.eventId) {
+ if(typeof pdfAs.cbMap.eventId.div != "undefined") {
+ var element = document.getElementById(pdfAs.cbMap.eventId.div);
+ element.style.display = 'none';
+ element.parentNode.removeChild(element);
+ }
+ }
+};
+
+pdfAs.doSuccessCB = function(eventId, pdfurl) {
+ if(pdfAs.cbMap.eventId) {
+ pdfAs.cbMap.eventId.suc(decodeURIComponent(pdfurl));
+ }
+};
+
+pdfAs.doErrorCB = function(eventId, error, cause) {
+ if(pdfAs.cbMap.eventId) {
+ pdfAs.cbMap.eventId.err(error, cause);
+ }
+};
+
+pdfAs.messageHandler = function(event) {
+ if(event.origin !== pdfAsOrigin) return;
+ console.log('message received: ' + event.data, event);
+ if(event.data.suc || event.data.err) {
+ pdfAs.checkOverlay(event.data.eventId);
+ if(event.data.suc) {
+ console.log('Success Message: ' + event.data.suc.pdfUrl);
+ pdfAs.doSuccessCB(event.data.eventId, event.data.suc.pdfUrl);
+ } else if(event.data.err) {
+ console.log('Error Message: ' + event.data.err.msg);
+ pdfAs.doErrorCB(event.data.eventId, event.data.err.error, event.data.err.cause);
+ }
+ } else if(event.data.resize) {
+ pdfAs.autoResize(event.data.resize, event.data.height, event.data.width);
+ }
+};
+
+function loadScript(url, callback)
+{
+ // Adding the script tag to the head as suggested before
+ var head = document.getElementsByTagName('head')[0];
+ var script = document.createElement('script');
+ script.type = 'text/javascript';
+ script.src = url;
+
+ // Then bind the event to the callback function.
+ // There are several events for cross browser compatibility.
+ script.onreadystatechange = callback;
+ script.onload = callback;
+
+ // Fire the loading
+ head.appendChild(script);
+}
+
+var jqueryRegistration = function() {
+ postman.receiveMessage(pdfAs.messageHandler, pdfAsOrigin);
+ //window.addEventListener("message", pdfAs.messageHandler, false);
+};
+
+
+jqueryRegistration();
+