Sunteți pe pagina 1din 10

var page = require('webpage').create(), system = require('system'), fileName = 'financialCard-export-27-05', fileNumber = 9, waitTime = 5000, address = 'http://localhost:8080/BAB_CCA_UI/loadFinancialCardDetails.htm l', extension = ".

pdf", exportedFileName = "testInterfaceName", t = Date.now(), pageTitle = 'Financial Card Details' exportPath = "C:\Upload\InterfaceExport", domain = "localhost", interfaceName = "testInterface", bpmSession = "", appId = 0, timeoutVar = null; if (system.args.length === 1) { console.log('Error: Try to pass some args when invoking this script!'); phantom.exit(); } else { for(var key in system.args){ console.log("arg key: " + key); } system.args.forEach(function (arg, i) { switch(i){ case 1: exportPath = arg; break; case 2: serverPath = arg; break; case 3: interfaceName = arg; break; case 4: pageTitle = arg; break; case 5: bpmSession = arg != null ? arg : ""; break; case 6: appId = arg != null && arg != "" ? arg : 0; break; } console.log(i + ': ' + arg); }); } exportedFileName = (exportPath + "/" +interfaceName + "-export-" + t); address = serverPath + "/load"+interfaceName+".html" domain = serverPath.split("http://")[1].split(":")[0] page.onResourceRequested = function(requestData, request) { clearRenderTimeout(); console.log("REQ= method:" + requestData['method'] + " url:" + requestDa ta['url'] + " - type:" + requestData['Content-Type'] + " req:" + request); }; page.onResourceReceived = function(response) {

clearRenderTimeout(); setRenderTimeout(); console.log('Response= (#' + response.id + ' status: ' +response.status + '-'+response.statusText+' , stage "' + response.stage + '"): ' + JSON.stringif y(response)); }; page.onAlert = function(msg) { console.log('ALERT: ' + msg); }; page.onError = function(msg, trace) { console.log("Page error:" + msg + " : " + trace); }; page.onLoadFinished = function(status){ if(status == "success"){ console.log("Page load " + status); console.log(page.content); //page.Sys = {}; //page.GetField = function (fieldId){ switch(fieldId){case "_b pmSecurityToken": return ""; break; case "_bpmApplicationID": return 288; br eak; default: return null;} } //page.evaluateJavaScript('function GetField(fieldId){ switch(f ieldId){case "_bpmSecurityToken": return "" break; case "_bpmApplicationID" : return 288; break; default: return null;} }'); }else{ console.log("page load failed: " + status); } } page.settings.localToRemoteUrlAccessEnabled = true; phantom.injectJs("jquery.js"); console.log("addCookie:" + phantom.addCookie({'domain': ecurityToken', 'value': bpmSession}) ); console.log("addCookie:" + phantom.addCookie({'domain': pplicationID', 'value': appId}) ); domain ,'name': '_bpmS domain ,'name': '_bpmA

function BpmTabs(tabId, settings) { var self = this, defaults = { triggerEvent: "click", classActive: "active", cbBeforeOpen: null, cbBeforeClose: null, cbAfterOpen: null, cbAfterClose: null, cbOnFirstOpen: null, cbOnChange: null, cbAfterChange: null, activ: 0, ulContext: null, divContext: null }, getref = function (li) { var a = $(li).children("a"); return a.length ? strip(a.first().attr("href")) : null; }, strip = function (str) { var pos = str.indexOf("#"); return pos === -1 ? str : str.substring(pos + 1);

}, tab = { getActive: function () { var lia = self.jqli.filter("." + self.options.classActive), stab ; if (lia.length) { stab = getref(lia); return { "index": self.s.nameToIndex[stab], "name": stab }; } else { return { "index": -1, "name": "" }; } }, getIndex: function (str) { var to = typeof str, sstr; if (to === "numeric" || $.isNumeric(str)) { sstr = parseInt(str); return self.s.indexToName[sstr] ? sstr : null; } else { sstr = strip(str); return self.s.nameToIndex[sstr] > -1 ? self.s.nameToIndex[ss tr] : null; } }, close: function (idx, trigger) { var li, callFn, ret; if (idx !== null) { li = $(self.jqli[idx]); li.removeClass(self.options.classActive); self.s.tabs[idx].hide(); selected = -1; if (trigger) { this.runFn(idx, self.options.cbAfterClose); } return true; } return false; }, open: function (idx, trigger) { var li; if (idx !== null) { if (self.s.firstTime[idx]) { this.runFn(idx, self.options.cbOnFirstOpen); self.s.firstTime[idx] = false; } li = $(self.jqli[idx]); li.addClass(self.options.classActive); self.s.tabs[idx].show(); selected = idx; if (trigger) { this.runFn(idx, self.options.cbAfterOpen); } return true;

} return false; }, getFn: function (idx, functions) { var fn; if (functions) { if ($.isFunction(functions)) { return functions; } else { for (fn in functions) { if (functions.hasOwnProperty(fn) && idx === this.get Index(fn)) { return functions[fn]; } } } } return null; }, runFn: function (idx, functions) { var callFn = this.getFn(idx, functions), ret; if (callFn && $.isFunction(callFn)) { ret = callFn.call(self.s.tabs[idx][0], { "index": idx, "name ": self.s.indexToName[idx] }); return typeof ret === "undefined" || ret === null ? true : r et; } else { return true; } } }, loop, len, linkDiv, selected = -1, temp; this.options = $.extend({}, defaults, settings ? settings : {}); this.jq = null; this.jqli = null; this.s = { tab: tab, indexToName: [], nameToIndex: [], firstTime: [], tabs: [] }; // == add prototype methods ========== if (typeof this.actions === "undefined") { BpmTabs.prototype.actions = {}; } if (typeof this.exec === "undefined") { BpmTabs.prototype.exec = function (command) { var fn = this.actions[command]; if (!fn) { throw ("BpmTabs - Metoda nu exista: " + command); }

return fn.apply(this, $.makeArray(arguments).slice(1)); }; } if (typeof this.attach === "undefined") { BpmTabs.prototype.attach = function () { var self = this; this.jqli.unbind(".btevents"); this.jqli.bind(this.options.triggerEvent + ".btevents", function (e) { var t = $(this); if (t.hasClass(self.options.classActive)) { return; } else { var idxFrom = self.s.tab.getActive().index, idxTo = self.s.nameToIndex[getref(t)], callFn = self.options.cbOnChange, callFn2 = self.options.cbAfterChange, ret; if ($.isFunction(callFn)) { ret = callFn.call({ from: self.s.tabs[idxFrom][0], to: s elf.s.tabs[idxTo][0] }, { "index": idxFrom, "name": self.s.indexToName[idxFrom] }, { "index": idxTo, "name": self.s.indexToName[idxTo] }); if (typeof ret !== "undefined" && ret !== null && !ret) { return false; } } if (self.s.tab.runFn(idxFrom, self.options.cbBeforeClose) && self.s.tab.runFn(idxTo, self.options.cbBeforeOpen)) { self.s.tab.close(idxFrom, true); self.s.tab.open(idxTo, true); } if ($.isFunction(callFn2)) { ret = callFn2.call({ from: self.s.tabs[idxFrom][0], to: self.s.tabs[idxTo][0] }, { "index": idxFrom, "name": self.s.indexToName[idxFrom] }, { "index": idxTo, "name": self.s.indexToName[idxTo] }); if (typeof ret !== "undefined" && ret !== null && !ret) { return false; } } } }); }; $.extend(this.actions, { "attach": this.attach }); } if (typeof this.getActiveTab === "undefined") { BpmTabs.prototype.getActiveTab = function () { selected = this.s.tab.getActive().index; return { "container": this.s.tabs[selected][0], "index": selected, " name": this.s.indexToName[selected] }; }; $.extend(this.actions, { "getActiveTab": this.getActiveTab });

} if (typeof this.selectTab === "undefined") { BpmTabs.prototype.selectTab = function (toTab, forced, trigger) { var idxTo = this.s.tab.getIndex(toTab), idxFrom = this.s.tab.getActive().index, events = trigger === null || typeof trigger === "undefined" ? tr ue : trigger, checker = 0; if (idxTo === null) { throw ("BpmTabs - selectTab: Tabul nu exista."); return; } if (idxTo !== idxFrom) { checker += this.s.tab.runFn(idxFrom, this.options.cbBeforeClose) ? 0 : 1; checker += checker == 0 && this.s.tab.runFn(idxTo, this.options. cbBeforeOpen) ? 0 : 1; if (!forced && checker > 0) { return false; } this.s.tab.close(idxFrom, events); this.s.tab.open(idxTo, events); } return true; }; $.extend(this.actions, { "selectTab": this.selectTab }); } if (typeof this.set === "undefined") { BpmTabs.prototype.set = function (newOptions) { $.extend(this.options, newOptions ? newOptions : {}); return true; }; $.extend(this.actions, { "set": this.set }); } if (typeof this.remove === "undefined") { BpmTabs.prototype.remove = function (newOptions) { this.jqli.unbind(".btevents"); return true; }; $.extend(this.actions, { "remove": this.remove }); } // == init check and logic ========== temp = this.options.ulContext ? this.options.ulContext.find("#" + tabId) : $ ("#" + tabId); if (!temp.length) { throw ("BpmTabs - Containerul de taburi nu exista: " + tabId); return; } this.jq = temp;

temp = temp.find("li"); if (!temp.length) { throw ("BpmTabs - Containerul de taburi nu contine taburi: " + tabId); return; } this.jqli = temp; len = this.jqli.length; temp = []; for (loop = 0; loop < len; loop++) { linkDiv = getref(this.jqli[loop]); if (linkDiv) { this.s.indexToName[loop] = linkDiv; this.s.nameToIndex[linkDiv] = loop; this.s.tabs[loop] = this.options.divContext ? this.options.divContex t.find("#" + linkDiv) : $(document.getElementById(linkDiv)); this.s.firstTime[loop] = true; } else { temp.push(String(loop)); } } if (this.s.indexToName.length != len) { throw ("BpmTabs - Exista LI care nu contin legaturi: " + temp.join(", ") ); return; } selected = this.s.tab.getActive().index; if (selected > -1 && this.options.activ !== null) { var activ = this.s.tab.getIndex(this.options.activ); if (activ == null) { throw ("BpmTabs - Tabul setat activ nu exista: " + this.options.acti v); return; } if (activ != selected) { this.s.tab.close(selected, false); } this.s.tab.open(activ, true); } } function pdfExportLoad() { // Write to page page.evaluate(function() { var tabsWrapp = $('.tabs-wrapper'), pageHeight = 11.69, //inch i = 0, pageWidth = 'auto', totalHeight = null, itabs = null, tabsArray = [], dots = window.screen.deviceXDPI, //number tabs = [], totalPageHeightInPixels = dots*pageHeight - 120,

//number // (120px ----> header+footer) allTabs = tabsWrapp.find('a'); for (i in allTabs){ if(allTabs[i].hash != ""){ var x = allTabs[i].hash; x = (""+x).replace("#",""); console.log(x); tabsArray.push(x); }else{ break; } } totalHeight = 3615/dots +'in'; // for (i in tabsArray){ // if( i = 0){ // $('#' +tabsArray[i]).css('he ight',pageHeight + 'in'); // }else{ // $('#' +tabsArray[i]).css('he ight',pageHeight+ ($('.scrollable-area').css('height') - $(".scrollable-area").h eight())*dots + 'in'); // } // }

var scrlAreaHeight = $('.container').find('.scrollable-area').cs s('height'), tbsWrpHeight = tabsWrapp.css('height'); $('.container').find('.scrollable-area').css('overflowx','hidden').css('overflow-y','hidden'); $('.container.with-header').find('.tab-content').css('display', 'block') .css('position', 'relative').css('height', '12in'); $('.footer').css('display','none'); for (i in tabsArray){ if( i == 0){ $('#' +tabsArray[i]).css('height','800px'); continue; } tabsWrapp.find('a[href='+'#' +tabsArray[parseInt(i)-1]+']').pare nt().removeClass('active'); tabsWrapp.find('a[href='+'#' +tabsArray[i]+']').parent().addClas s('active'); tabsWrapp.clone().appendTo($('#' +tabsArray[i])); $('#' +tabsArray[i]).css('padding-top','90px'); $('#' +tabsArray[i]).css('height','800px'); if(tabsArray.length == parseInt(i)+1){ $('#' +tabsArray[i]).css('padding-bottom','90px'); $('#' +tabsArray[i]).css('height','800px'); } } tabsWrapp.find('a[href='+'#' +tabsArray[parseInt(tabsArray.length)-1]+'] ').parent().removeClass('active'); tabsWrapp.find('a[href='+'#' +tabsArray[0]+']').parent().addClas s('active');

// $('.container').css('width',pageWidth).css('height','60in'); // $('#body').css('width',pageWidth).css('height',$('.scrollable-ar ea')[0].scrollHeight); // var xxx = $('.scrollable-area')[0].scrollHeight/dots; $('.scrollable-area').css('width',pageWidth).css('height','100in');

}); // Set the page size and orientation page.paperSize = { format: 'A4', orientation: 'portrait', // width: '1300px', // height: '3100px', border: '1px', zoom: 1, header: { height: "60px", contents: phantom.callback(function(pageNum, numPages) { return "<h2 style='text-align:center;color:#8E00 30;'>"+ pageTitle +"</h1>"; }) }, footer: { height: "60px", contents: phantom.callback(function(pageNum, numPages) { if (pageNum == numPages) { return ""; } return "<span style='float:right;font-size:12px; color:#8E0030;'>" + pageNum + " / " + numPages + "</span>"; }) } }; console.log("Before render"); page.render(exportedFileName + extension); }

function clearRenderTimeout(){ if(timeoutVar != null){ console.log("clear timoeut"); window.clearTimeout(timeoutVar); timeoutVar = null; } } function setRenderTimeout(){ console.log("setting timoeut"); timeoutVar = setTimeout(function() {

pdfExportLoad(); console.log ('Done in ' + t + 'ms - now exiting!'); phantom.exit(); }, waitTime); } page.open(address, function (status) { console.log ('Waiting until page is loaded, script delay is ' + waitTime + 'ms'); // Check if page loaded successfuly if (status !== 'success') { console.log('Error! Unable to load the page!'); phantom.exit(); } else { console.log("Load page " + status + " html"); //document.getElementById("_bpmSecurityToken").value = " "; //document.getElementById("_bpmApplicationID").value = " 2"; // Set page size page.viewportSize = { 'width': 980, 'height': 7000 }; phantom.viewportSize = { 'width': 980, 'height': 7000 }; // Begin export //timeoutVar = setRenderTimeout(); } });