菜鸟云打印
安装菜鸟打印组件
https://support-cnkuaidi.taobao.com/doc.htm#?docId=108934&docType=1
Html:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JS_CloudPrint.aspx.cs" Inherits="Demo.JS_CloudPrint" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> <script src="https://g.alicdn.com/fi/bui/jquery-1.8.1.min.js"></script> <script src="CloudPrinter.js" type="text/javascript"></script> </head> <body> <a href="javascript:;" onclick="doPrint()">打印</a><br /><br /> <a href="javascript:;" onclick="doPreview()">预览</a><br /><br /> </body> </html>
CloudPrinter.js:(jquery-1.8.1.min大家自己下载)
var socket; var printers; var defaultPrinter; var printTaskId; var taskers; var waybillPrintStatus; var waybillNO = '000000000000'; var waybillTemplateURL = 'http://cloudprint.cainiao.com/template/standard/101/123'; var printData; //自动连接 doConnect() //连接打印机并监听 function doConnect() { socket = new WebSocket('ws://127.0.0.1:13528'); var result = "打印成功"; var tmptaskID = "0"; // 打开Socket socket.onopen = function (event) { // 监听消息 socket.onmessage = function (event) { var data = JSON.parse(event.data); printers = data.printers; defaultPrinter = data.defaultPrinter; if (data.cmd == 'notifyPrintResult') { var result = "打印成功"; if (data.taskID != tmptaskID) { for (var i = 0; i < data.printStatus.length; i++) { if (data.printStatus[i].status == 'failed') { result = "打印失败"; break; } else if (data.printStatus[i].status == 'canceled') { result = "取消打印任务"; break; } } tmptaskID = data.taskID; alert(result); } } else if (data.cmd == "print") { if (data.previewURL) window.open(data.previewURL); } }; // 监听Socket的关闭 socket.onclose = function (event) { alert("打印机失去联系,请刷新网页重新连接"); }; }; socket.onerror = function (event) { alert("打印机出现问题,请联系客服"); } } function doPreview() { var printData = getPrintData(); printData.task.preview = true; socket.send(JSON.stringify(printData)); } //打印电子面单 function doPrint() { var printData = getPrintData(); socket.send(JSON.stringify(request)); } function getPrintData() { waybillNO = "123456789123"; printTaskId = parseInt(1000 * Math.random()); request = { cmd: "print", requestID: "12345678901234567890", version: "1.0", task: { taskID: '' + printTaskId, preview: false, printer: defaultPrinter, notifyMode: "allInOne", documents: [ { "documentID": waybillNO, contents: [ //电子面单部分 { templateURL: waybillTemplateURL, signature: "MD:kw4V6Gtj/7ZasLyotby+HA==", "data": { "recipient": { "address": { "city": "济南市", "detail": "解放东路100号", "district": "历下区", "province": "山东省" }, "mobile": "13888888888", "name": "张三", "phone": "13888888888" }, "routingInfo": { "consolidation": { "name": "济南", "code": "jinan" }, "origin": { "code": "STO" }, "sortation": { "name": "济南" }, "routeCode": "380D-56-04" }, "sender": { "address": { "city": "东营市", "detail": "广兴路300号", "district": "垦利县", "province": "山东省" }, "mobile": "13412341234", "name": "仓库", "phone": "13412341234" }, "waybillCode": "773224859224902" } //电子面单数据 } ] } ] } }; return request; }