LodopFuncs.js 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //==本JS是加载Lodop插件或Web打印服务CLodop/Lodop7的综合示例,可直接使用,建议理解后融入自己程序==
  2. var CreatedOKLodopObject, CLodopIsLocal, CLodopJsState;
  3. //==判断是否需要CLodop(那些不支持插件的浏览器):==
  4. function needCLodop() {
  5. try {
  6. var ua = navigator.userAgent;
  7. if (ua.match(/Windows\sPhone/i)) return true;
  8. if (ua.match(/iPhone|iPod|iPad/i)) return true;
  9. if (ua.match(/Android/i)) return true;
  10. if (ua.match(/Edge\D?\d+/i)) return true;
  11. var verTrident = ua.match(/Trident\D?\d+/i);
  12. var verIE = ua.match(/MSIE\D?\d+/i);
  13. var verOPR = ua.match(/OPR\D?\d+/i);
  14. var verFF = ua.match(/Firefox\D?\d+/i);
  15. var x64 = ua.match(/x64/i);
  16. if ((!verTrident) && (!verIE) && (x64)) return true;
  17. else if (verFF) {
  18. verFF = verFF[0].match(/\d+/);
  19. if ((verFF[0] >= 41) || (x64)) return true;
  20. } else if (verOPR) {
  21. verOPR = verOPR[0].match(/\d+/);
  22. if (verOPR[0] >= 32) return true;
  23. } else if ((!verTrident) && (!verIE)) {
  24. var verChrome = ua.match(/Chrome\D?\d+/i);
  25. if (verChrome) {
  26. verChrome = verChrome[0].match(/\d+/);
  27. if (verChrome[0] >= 41) return true;
  28. }
  29. }
  30. return false;
  31. } catch (err) {
  32. return true;
  33. }
  34. }
  35. //加载CLodop时用双端口(http是8000/18000,而https是8443/8444)以防其中某端口被占,
  36. //主JS文件名“CLodopfuncs.js”是固定名称,其内容是动态的,与其链接的打印环境有关:
  37. function loadCLodop() {
  38. if (CLodopJsState == "loading" || CLodopJsState == "complete") return;
  39. CLodopJsState = "loading";
  40. var head = document.head || document.getElementsByTagName("head")[0] || document.documentElement;
  41. var JS1 = document.createElement("script");
  42. var JS2 = document.createElement("script");
  43. if (window.location.protocol=='https:') {
  44. JS1.src = "https://localhost.lodop.net:8443/CLodopfuncs.js";
  45. JS2.src = "https://localhost.lodop.net:8444/CLodopfuncs.js";
  46. } else {
  47. JS1.src = "http://localhost:8000/CLodopfuncs.js";
  48. JS2.src = "http://localhost:18000/CLodopfuncs.js";
  49. }
  50. JS1.onload = JS2.onload = function() {CLodopJsState = "complete";}
  51. JS1.onerror = JS2.onerror = function(evt) {CLodopJsState = "complete";}
  52. head.insertBefore(JS1, head.firstChild);
  53. head.insertBefore(JS2, head.firstChild);
  54. CLodopIsLocal = !!((JS1.src + JS2.src).match(/\/\/localho|\/\/127.0.0./i));
  55. }
  56. if (needCLodop()){loadCLodop();}//开始加载
  57. //==获取LODOP对象主过程,判断是否安装、需否升级:==
  58. function getLodop(oOBJECT, oEMBED) {
  59. var strFontTag= "<br><font color='#FF00FF'>打印控件";
  60. var strLodopInstall = strFontTag+"未安装!点击这里<a href='install_lodop32.exe' target='_self'>执行安装</a>";
  61. var strLodopUpdate = strFontTag+"需要升级!点击这里<a href='install_lodop32.exe' target='_self'>执行升级</a>";
  62. var strLodop64Install = strFontTag+"未安装!点击这里<a href='install_lodop64.exe' target='_self'>执行安装</a>";
  63. var strLodop64Update = strFontTag+"需要升级!点击这里<a href='install_lodop64.exe' target='_self'>执行升级</a>";
  64. var strCLodopInstallA = "<br><font color='#FF00FF'>Web打印服务CLodop未安装启动,点击这里<a href='CLodop_Setup_for_Win32NT.exe' target='_self'>下载执行安装</a>";
  65. var strCLodopInstallB = "<br>(若此前已安装过,可<a href='CLodop.protocol:setup' target='_self'>点这里直接再次启动</a>)";
  66. var strCLodopUpdate = "<br><font color='#FF00FF'>Web打印服务CLodop需升级!点击这里<a href='CLodop_Setup_for_Win32NT.exe' target='_self'>执行升级</a>";
  67. var strLodop7FontTag = "<br><font color='#FF00FF'>Web打印服务Lodop7";
  68. var strLodop7HrefX86 = "点击这里<a href='Lodop7_Linux_X86_64.tar.gz' target='_self'>下载安装</a>(下载后解压,点击lodop文件开始执行)";
  69. var strLodop7HrefARM = "点击这里<a href='Lodop7_Linux_ARM64.tar.gz' target='_self'>下载安装</a>(下载后解压,点击lodop文件开始执行)";
  70. var strLodop7Install_X86 = strLodop7FontTag+"未安装启动,"+strLodop7HrefX86;
  71. var strLodop7Install_ARM = strLodop7FontTag+"未安装启动,"+strLodop7HrefARM;
  72. var strLodop7Update_X86 = strLodop7FontTag+"需升级,"+strLodop7HrefX86;
  73. var strLodop7Update_ARM = strLodop7FontTag+"需升级,"+strLodop7HrefARM;
  74. var strInstallOK = ",成功后请刷新本页面或重启浏览器。</font>";
  75. var LODOP;
  76. try {
  77. var isWinIE = (/MSIE/i.test(navigator.userAgent)) || (/Trident/i.test(navigator.userAgent));
  78. var isWinIE64 = isWinIE && (/x64/i.test(navigator.userAgent));
  79. var isLinuxX86 = (/Linux/i.test(navigator.platform)) && (/x86/i.test(navigator.platform));
  80. var isLinuxARM = (/Linux/i.test(navigator.platform)) && (/aarch/i.test(navigator.platform));
  81. if (needCLodop() || isLinuxX86 || isLinuxARM) {
  82. try {
  83. LODOP = getCLodop();
  84. } catch (err) {}
  85. if (!LODOP && CLodopJsState !== "complete") {
  86. if (CLodopJsState == "loading") alert("网页还没下载完毕,请稍等一下再操作."); else alert("未曾加载Lodop主JS文件,请先调用loadCLodop过程.");
  87. return;
  88. }
  89. var strAlertMessage;
  90. if (!LODOP) {
  91. if (isLinuxX86) strAlertMessage = strLodop7Install_X86; else
  92. if (isLinuxARM) strAlertMessage = strLodop7Install_ARM; else
  93. strAlertMessage = strCLodopInstallA + (CLodopIsLocal ? strCLodopInstallB : "") ;
  94. document.body.innerHTML = strAlertMessage + strInstallOK + document.body.innerHTML;
  95. return;
  96. } else {
  97. if (isLinuxX86 && LODOP.CVERSION < "7.0.4.2") strAlertMessage = strLodop7Update_X86; else
  98. if (isLinuxARM && LODOP.CVERSION < "7.0.4.2") strAlertMessage = strLodop7Update_ARM; else
  99. if (CLODOP.CVERSION < "4.1.5.8") strAlertMessage = strCLodopUpdate;
  100. if (strAlertMessage) document.body.innerHTML = strAlertMessage+ strInstallOK + document.body.innerHTML;
  101. }
  102. } else { //==如果页面有Lodop插件就直接使用,否则新建:==
  103. if (oOBJECT || oEMBED) {
  104. if (isWinIE)
  105. LODOP = oOBJECT;
  106. else
  107. LODOP = oEMBED;
  108. } else if (!CreatedOKLodopObject) {
  109. LODOP = document.createElement("object");
  110. LODOP.setAttribute("width", 0);
  111. LODOP.setAttribute("height", 0);
  112. LODOP.setAttribute("style", "position:absolute;left:0px;top:-100px;width:0px;height:0px;");
  113. if (isWinIE)
  114. LODOP.setAttribute("classid", "clsid:2105C259-1E0C-4534-8141-A753534CB4CA");
  115. else
  116. LODOP.setAttribute("type", "application/x-print-lodop");
  117. document.documentElement.appendChild(LODOP);
  118. CreatedOKLodopObject = LODOP;
  119. } else
  120. LODOP = CreatedOKLodopObject;
  121. //==Lodop插件未安装时提示下载地址:==
  122. if ((!LODOP) || (!LODOP.VERSION)) {
  123. document.body.innerHTML = (isWinIE64 ? strLodop64Install : strLodopInstall) + strInstallOK + document.body.innerHTML;
  124. return LODOP;
  125. }
  126. if (LODOP.VERSION < "6.2.2.6") {
  127. document.body.innerHTML = (isWinIE64 ? strLodop64Update : strLodopUpdate) + strInstallOK + document.body.innerHTML;
  128. }
  129. }
  130. //===如下空白位置适合调用统一功能(如注册语句、语言选择等):=======================
  131. //===============================================================================
  132. return LODOP;
  133. } catch (err) {
  134. alert("getLodop出错:" + err);
  135. }
  136. }
  137. export { getLodop }; //导出getLodop