概述
在html页下使用Epson P60II 热敏纸下打印小票,使用的打印方案为调用window.print()。
代码实现
1、定义窗体,设置宽度和高度
<body onload="window.external.Print(0,0);" style="margin-top:0px;" ><form id="form1" runat="server" ><div align="center" valign="top" id ="PrintDoc" ><table style="width:4.8cm; height:9.9cm; border-width:1px; border-color:Red; border-style:dashed" ><tr align="center" valign="top"><td><asp:Label id="Label_proname" runat="server" ></asp:Label></td></tr></table></div></form>
</body>
2、使用ActiveXObject,创建 Scripting.FileSystemObject
function print_onclick() {var objfs = new ActiveXObject("Scripting.FileSystemObject");var objprinter=objfs.CreateTextFile("LPT1:",true);objprinter.Write(String.fromCharCode(0x1B) + "@");objprinter.Write(String.fromCharCode(0x1B) + String.fromCharCode(0x69));objprinter.Close();
}
3、设置隐藏模式打印
<style media="print">.Noprint { DISPLAY: none }</style>
4、填充数据
private void ListBind(){try{string strVoucherID = Request.QueryString["voucherid"];if (strVoucherID != null && strVoucherID != string.Empty){this.Label_proname.Text = this.DataBase.GetShopVoucherDetail(strVoucherID);this.DataBase.VoucherPrintUpdate(strVoucherID, this.DataBase.Areaid);}}catch (Exception ex){Console.WriteLine(ex.Message);}}
完整代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Print.aspx.cs" Inherits="Print" enableEventValidation="false" validateRequest="false" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server"><title>好牛网移动优惠卷</title><style media="print">.Noprint { DISPLAY: none }</style>
<script type='text/javascript'> function print_onclick() {var objfs = new ActiveXObject("Scripting.FileSystemObject");var objprinter=objfs.CreateTextFile("LPT1:",true);objprinter.Write(String.fromCharCode(0x1B) + "@");objprinter.Write(String.fromCharCode(0x1B) + String.fromCharCode(0x69));objprinter.Close();
}
</script>
<script language="javascript">function PrintCutrefresh()
{print_onclick();}setTimeout('PrintCutrefresh()',7000); //指定7秒后切纸
</script>
</head>
<body onload="window.external.Print(0,0);" style="margin-top:0px;" ><form id="form1" runat="server" ><div align="center" valign="top" id ="PrintDoc" ><table style="width:4.8cm; height:9.9cm; border-width:1px; border-color:Red; border-style:dashed" ><tr align="center" valign="top"><td><asp:Label id="Label_proname" runat="server" ></asp:Label></td></tr></table></div></form>
</body>
</html>