如何在ASP.NET MVC应用中实现二维码标签打印?
解决方案:Web端用ZPL打印二维码标签
首先明确:二维码和条码在ZPL指令体系中可以用类似逻辑实现,ZPL有专门的二维码打印指令,和条码指令只是参数不同,不存在技术上的本质差异。
针对你的ASP.NET MVC Web应用,要实现标签打印机直接打印带二维码+文本的标签,有两种可行方案:
方案一:后端生成ZPL指令,发送到网络打印机
如果你的标签打印机是网络可访问的(有固定IP和打印端口,一般为9100),可以按以下步骤实现:
1. 后端(ASP.NET MVC)生成ZPL内容
在PrintQRCodeController中新增Action,根据资产数据生成完整ZPL指令:
public ActionResult GenerateZPL() { // 替换为你的资产数据获取逻辑 var assets = GetAssetData(); StringBuilder zpl = new StringBuilder(); // 初始化标签格式(示例:3英寸宽、2英寸高) zpl.AppendLine("^XA"); zpl.AppendLine("^LL600"); // 设置标签高度为600点(约2英寸) foreach (var asset in assets) { // 打印二维码:^BQ指令(位置、QR码类型、纠错等级、模块大小) zpl.AppendLine("^FO50,50^BQN,2,5"); zpl.AppendLine($"^FDQA,{asset.QRCodeContent}^FS"); // QA表示QR码,后跟二维码内容 // 打印下方资产编号文本 zpl.AppendLine("^FO50,250^A0N,30,30^FD" + asset.AssetNumber + "^FS"); // 切割并打印当前标签 zpl.AppendLine("^PQ1"); zpl.AppendLine("^XZ"); zpl.AppendLine("^XA"); // 重置格式,准备下一个标签 } zpl.AppendLine("^XZ"); return Content(zpl.ToString(), "text/plain"); }
2. 后端中转发送ZPL到打印机
前端触发请求后,后端直接将ZPL发送到打印机,避免前端跨域/权限问题:
using System.Net.Sockets; public ActionResult SendToPrinter(string zpl, string printerIp) { try { using (TcpClient client = new TcpClient(printerIp, 9100)) { using (NetworkStream stream = client.GetStream()) { byte[] data = System.Text.Encoding.ASCII.GetBytes(zpl); stream.Write(data, 0, data.Length); } } return Json(new { success = true }); } catch (Exception ex) { return Json(new { success = false, message = ex.Message }); } }
3. 前端触发打印
修改JS代码,请求ZPL并触发后端发送:
$("#printTags").click(function() { $.ajax({ type: "GET", url: '/PrintQRCode/GenerateZPL', dataType: "text", success: function(zplContent) { // 替换为你的打印机IP const printerIp = "192.168.1.100"; $.ajax({ type: "POST", url: '/PrintQRCode/SendToPrinter', data: { zpl: zplContent, printerIp: printerIp }, success: () => alert("标签打印成功"), error: () => alert("打印失败,请检查打印机连接") }); }, error: () => alert("生成ZPL失败") }); });
方案二:前端适配标签尺寸打印(无ZPL支持时)
如果打印机不支持ZPL,可调整前端打印的CSS,让浏览器输出适配标签尺寸:
$("#test2").html(""); let divID = 0; $.ajax({ type: "POST", url: '/PrintQRCode/print', data: null, dataType: "JSON", success: function (d) { if (d != "") { const printWindow = window.open('', '', 'height=600,width=1000'); printWindow.document.write(` <html> <head> <title>Print Asset Barcodes</title> <style> @media print { body { margin: 0; padding: 0; } .tag { width: 3in; height: 2in; page-break-inside: avoid; float: left; padding: 0.2in; box-sizing: border-box; } .qrcode { width: 1.5in; height: 1.5in; margin: 0 auto; } .asset-number { text-align: center; margin-top: 0.1in; font-size: 14px; } } </style> </head> <body>`); // 生成每个标签的HTML $(d.data).each(function (index, row) { printWindow.document.write(` <div class="tag"> <div id="${divID}" class="qrcode"></div> <div class="asset-number">${row.AssetNumber}</div> </div>`); GenerateQRCode(divID); divID++; }); printWindow.document.write('</body></html>'); printWindow.document.close(); // 等待二维码生成后打印 setTimeout(() => printWindow.print(), 1000); } }, error: function () { alert("Some errors have occurred. Please try again later"); } });
关键说明
- ZPL是工业标签打印机的通用指令语言,二维码用
^BQ指令,条码用^BC/^BY等,逻辑一致,你查到的WinForms ZPL方案可直接迁移到Web端,核心是生成正确的ZPL指令。 - Web端直接访问本地打印机受浏览器安全限制,优先使用网络打印机,通过后端中转发送ZPL是最可靠的方式。
- 若必须使用本地打印机,可考虑用Electron封装Web应用,或使用IE专属的ActiveX插件,但这类方案兼容性较差。
内容的提问来源于stack exchange,提问作者Nitesh
相关产品推荐
相关产品推荐

