当前位置: 代码迷 >> 综合 >> LODOP JS 获取电脑打印机,并指定打印机打印
  详细解决方案

LODOP JS 获取电脑打印机,并指定打印机打印

热度:21   发布时间:2023-12-04 01:26:53.0
//获取本地电脑可选的打印机(驱动),并默认选择cookie中存储的打印机
function printerList() {//读cookie中存储已经选择的打印机:var arrStr = document.cookie.split("; ");var strPName;for (var i = 0; i < arrStr.length; i++) {var temp = arrStr[i].split("=");if (temp[0] == "printn" && unescape(temp[1]) != "") {LODOP.SET_PRINTER_INDEX(unescape(temp[1]));strPName = LODOP.GET_PRINTER_NAME(unescape(temp[1]));}}var count = LODOP.GET_PRINTER_COUNT();$("#printer").html("");for (var i = 0; i < count; i++) {//根据设备序号获取设备名var msg = LODOP.GET_PRINTER_NAME(i);//将设备名添加到select块,并添加相应value值if (msg == strPName) {$("#printer").append("<option value='" + i + "' selected>" + msg + "</option>");} else {$("#printer").append("<option value='" + i + "'>" + msg + "</option>");}}
}//选择打印机后存储到cookie
$("#printer").change(function () {var Days = 300;var exp = new Date();//设置cookie过期时间exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);document.cookie = "printn=" + $("#printer").val() + ";expires=" + exp.toGMTString();
});window.onload = function () {printerList();
};

//获取选择的打印机

var LODOP;
var indexPrinter;
//获取控件实例
if (navigator.appVersion.indexOf("MSIE") >= 0 || navigator.appVersion.indexOf("Trident") >= 0) {LODOP = document.getElementById('LODOP_OB');
} else {LODOP = document.getElementById('LODOP_EM');
}

var indexPrinter;//指定的打印机序号,声明为全局变量

function getPrinter() {var arrStr = document.cookie.split("; ");for (var i = 0; i < arrStr.length; i++) {var temp = arrStr[i].split("=");if (temp[0] == "printn" && unescape(temp[1]) != "") {indexPrinter = unescape(temp[1]);//指定打印机LODOP.SET_PRINTER_INDEX(indexPrinter);}}
}

效果图: