Web 打印小票 ChicoreDriver - APISampleJavascript js静默打印web 打印, js打印, js 打印, Javascript 打印, js 静默打印,js 不预览打印,浏览器打印,js标签打印,js单据打印,ChicoreDriverCCPrintingAPI for ChicoreDriver, Javascript 打印范例代码,js标签打印,条码,二维码标签,js单据打印,很容易集成。
CCPrintingAPI XI
轻量、简洁、稳定、可靠,打印机API接口(ChicoreDriver)。
打印机API接口 ChicoreDriver
Javascript
CCPrintingAPI for ChicoreDriver, Javascript API打印范例代码,浏览器Web前端打印,通过js 调用打印机接口API。

1. 引入 CCPrintingAPI.min.js
引入CCPrintingAPI.min.js后,即可使用API的相关方法,进行设置和打印操作。
<script src="../../js/CCPrintingAPI.min.js"></script>
2. 获取打印机
获取计算机上所有打印机, 打印时,指定该列表的打印机名称。如果能确保打印机名称不变,该操作也可省去。
function GetPrinters(){
   var cfgData = new Object;
   cfgData .XAction = "Printers";
   CCPrintingAPI.Excute(cfgData ,
	function(XTimeout){  },
	function(XFail){
		document.getElementById("tipsmessage").innerHTML =  "API可能未安装或未启动";	
	},
	function(XSuccess){
		var apiResult = JSON.parse(XSuccess.trim());
		if(apiResult.Status=="1"){ 
				/*获取到打印机列表*/
				var prts =  document.getElementById("Printers");
				prts.length = 0;
				if(apiResult.Message.length  > 1  ){
					for(var i = 1 ;i <  apiResult.Message.length;i++){
						prts.options.add(new Option(apiResult.Message[i],apiResult.Message[i]));					
					}
					/*设置上次使用的打印机*/					
					prts.options[0].selected = 'selected';
		                }
	         }
	}		
  );
}
3. 打印
进行相关参数设置后(指定打印机,设置模板,设置打印数据),将指令通过API传给打印机。
function Print() {
    var prts = document.getElementById("Printers");
    var selPrinter = prts.options[prts.selectedIndex].text;

     var cfgPrintSettings = new Object;
     cfgPrintSettings.XAction = "Print"

                var ccdriverPrinter = new Object;
               /*免驱打印机名称ChicoreDriver-USB-*/
                ccdriverPrinter.Name = selPrinter ;
                ccdriverPrinter.InterfaceType = 0;
               /*打印方向 0-0°,1-90°,2-180°,3-270°*/
                ccdriverPrinter.Direction = 0;
               /*打印浓度0~10*/
                ccdriverPrinter.Density = 5;
               /*打印方式 0-热转印,1-热敏*/
                ccdriverPrinter.MediaType = 1;
               /*介质类型 0-连续纸,2-有间隙的标签,3-有标记的纸*/
                ccdriverPrinter.MediaSensing = 0;
               /*负片打印 0-否,2-是*/
                ccdriverPrinter.ReversePrint= 0;
               /*水平偏移mm*/
                ccdriverPrinter.StartX= 0;
               /*垂直偏移mm*/
                ccdriverPrinter.StartY= 0;

    /*指定打印机*/
   cfgPrintSettings.PrinterName =JSON.stringify(ccdriverPrinter);


    /*模板方式,0-客户端模板,1-远程模板,
                2-如果本地模板存在,使用本地,不存在使用远程模板,3-不使用模板*/
    cfgPrintSettings.TemplateMode = "1";
    /*远程模板URL或本地模板的文件夹*/
    cfgPrintSettings.TemplateURL = "http://img.51321.cn/web/chicore/VPrinting/API";
    //cfgPrintSettings.TemplateURL =  "E:\\临时数据\\打印\\打印样本\\API标签模板";
    /*模板文件名*/
    cfgPrintSettings.TemplateName = "985.Lblx";
    /*打印数据的类型,0-数组,1-Json数组,元素为JSON对象,字段:值*/
    cfgPrintSettings.PrintDataType = 1;
    /*传递打印数据 Json数组*/ /*内容不允许 半角的逗号及半角的分隔符*/
    var arr =[
{"Field1":"吉祥如意餐厅","Field2":"1002104190264","Field3":"2021.04.19 12:20:55","Field4":"0107610/香爆脆羊肉串","Field5":"1.00","Field6":"1.00","Field7":"1.00","Field8":"5.28","Field9":"7.00","Field10":"16.20","Field11":"100.00","Field12":"83.80","Field13":"感谢您的光临,欢迎下次再来","Field14":"http://51321.cn","Field15":"支付宝","Field16":"找零:","Field17":""},
{"Field1":"吉祥如意餐厅","Field2":"1002104190264","Field3":"2021.04.19 12:20:55","Field4":"0107611/德州扒鸡","Field5":"62.00","Field6":"1.00","Field7":"62.00","Field8":"5.28","Field9":"7.00","Field10":"16.20","Field11":"100.00","Field12":"83.80","Field13":"感谢您的光临,欢迎下次再来","Field14":"http://51321.cn","Field15":"支付宝","Field16":"找零:","Field17":""},
{"Field1":"吉祥如意餐厅","Field2":"1002104190264","Field3":"2021.04.19 12:20:55","Field4":"0107612/北京片皮烤鸭","Field5":"158.00","Field6":"1.00","Field7":"158.00","Field8":"5.28","Field9":"7.00","Field10":"16.20","Field11":"100.00","Field12":"83.80","Field13":"感谢您的光临,欢迎下次再来","Field14":"http://51321.cn","Field15":"支付宝","Field16":"找零:","Field17":""}
];
    cfgPrintSettings.PrintData = JSON.stringify(arr);
    /*是否启用即打即停,0-否,1-是*/
    cfgPrintSettings.PrintToStop = "1";	

    /*打印 发送给打印机进行打印*/
    CCPrintingAPI.Excute(cfgPrintSettings,
        function (XTimeout) { },
        function (XFail) {
            document.getElementById("tipsmessage").innerHTML = "API可能未安装或未启动";
        },
        function (XSuccess) {
            var apiResult = JSON.parse(XSuccess.trim());
            if (apiResult.Status == "1") {
                var smsg = apiResult.Message[0]; 
                if(apiResult.Message.length > 1)
                       smsg  += " : " + apiResult.Message[1]
                document.getElementById("tipsmessage").innerHTML = smsg;
            }else{
			  if (apiResult.Message.length > 1)
					  document.getElementById("tipsmessage").innerHTML = apiResult.Message[0] +  " :  " + apiResult.Message[1];
			   else
					  document.getElementById("tipsmessage").innerHTML = apiResult.Message[0];
	     }
        }
    );
}