1. 配置打印模板
【基础设置(集团)】-【打印配置】
实体:需要打印的实体
数据类型:常量、字段(取值来自实体)
字段:实体下的字段
字段(公式):通过公式取值
打印类型:文本、条码、二维码
数据:当数据类型为常量时需要维护常量的值
PosX:数据在纸张上的横轴位置单位dot
PosY:数据在纸张上的竖轴位置单位dot
字段高度:当打印类型为文本时有效
对齐方式:
条码类型:当打印类型为条码时有效
数据长度/小数位数:数据为字符串时用于截取字符串的长度,数据为数字时小数位数有效
日期格式:数据为日期时对日期进行格式化
数据截取类型:当数据为字符串时截取字符串(从左截取、从右截取)
1.1. 字段
用于设置数据的取值来源,使用字段(公式)实现更加灵活的取值方式。
1.2. 字段(公式)
- 由于未实现字段的树形参照,通过此字段取层级深的数据比如取标准销售-客户-立账条件的名称,字段(公式):
OrderBy.Customer.ARConfirmTerm.Name - 通过中间表取数,取收款计划的收款的整单金额,字段(公式):
Entity:UFIDA.U9.SM.InstalmentRecSchedule.InstalmentRecSchedule:DocID=ID:Status - 通过(OrderBy.Customer.ARConfirmTerm.Name+验)可以实现动态数据(OrderBy.Customer.ARConfirmTerm.Name)+静态数据(验)
2. 安装WebPrint、配置打印机IP(打印机连接的电脑的IP)
2.1. 安装WebPrint
- WebPrint下载地址:
https://s3.cn-north-1.amazonaws.com.cn/godex/GXDXqOixaOHR6gZvVnUBgQ.file?v01 - 只有连接打印机的电脑需要安装WebPrint,电脑安装好WebPrint并连接好打印机后在U9配置打印机IP
2.2. 配置打印机IP
【基础设置(集团)】-【公共设置】-【参数设置】配置【连接打印机的电脑的IP地址】
2.3. 网络配置
U9服务器需要可以访问打印机连接电脑的IP
3. 选择、修改打印数据、打印
3.1. 选择需要打印的数据
选择需要打印的数据点击打印条码
3.2. 修改需要打印的数据
3.3 调用打印机
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
using UFIDA.U9.Cust.IESLabBE.CommonBE;
using UFIDA.U9.Cust.IESLabBP.Util;
namespace UFIDA.U9.Cust.IESLabBP.PrinterBP
{
/// <summary>
/// 科诚Web打印
/// </summary>
class GoDEXWebPrinter
{
private const string PROFILE_GODEXWEBURL = "GoDEXWebUrl";
/// <summary>
/// 向打印机发送打印请求
/// </summary>
/// <param name="commond"></param>
/// <param name="printData"></param>
private void DoCommand(string commond, PrintData printData)
{
string url = GetGoDEXWebUrl() + commond+"/";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "Post";
request.ContentType = "application/x-www-form-urlencoded";
string data = SerializeToJSONString(printData);
data = Convert.ToBase64String(Encoding.Default.GetBytes(data));
data = "EncodeData=" + data;
request.ContentLength = data.Length;
using (var postStream = new StreamWriter(request.GetRequestStream()))
{
postStream.Write(data);
}
var response = (HttpWebResponse)request.GetResponse();
using (StreamReader reader=new StreamReader(response.GetResponseStream()))
{
reader.ReadToEnd();
}
}
/// <summary>
/// 发送需要打印的数据
/// </summary>
/// <param name="data"></param>
private void PrintDataCommand(List<PrintDataDTO> data)
{
foreach (PrintDataDTO printData in data)
{
PrintData printConfig = new PrintData(printData);
CommandEnum commandEnum = CommandEnum.ecTextOut;
if (printData.DataType.Value== DataTypeEnum.BarCode.Value)
{
commandEnum = CommandEnum.PrintBarcode;
}
if (printData.DataType.Value == DataTypeEnum.QRCode.Value)
{
commandEnum = CommandEnum.PrintQrcode;
}
DoCommand(commandEnum.ToString(), printConfig);
}
}
/// <summary>
/// 发送开始打印请求
/// </summary>
private void SendLCommand()
{
PrintData printConfig = new PrintData();
printConfig.Data = Convert.ToBase64String(Encoding.Default.GetBytes("^L"));
DoCommand(CommandEnum.Send.ToString(), printConfig);
}
/// <summary>
/// 发送打印结束请求
/// </summary>
private void SendECommand()
{
PrintData printConfig = new PrintData();
printConfig.Data = Convert.ToBase64String(Encoding.Default.GetBytes("E"));
DoCommand(CommandEnum.Send.ToString(), printConfig);
}
/// <summary>
/// 打印
/// </summary>
/// <param name="data"></param>
public void Print(List<PrintDataDTO> data)
{
SendLCommand();
PrintDataCommand(data);
SendECommand();
}
/// <summary>
/// 对象转JSON字符串
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
private string SerializeToJSONString(object obj)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
return jss.Serialize(obj);
}
/// <summary>
/// 获取打印机IP地址
/// </summary>
/// <returns></returns>
private string GetGoDEXWebUrl()
{
string baseUrl=ProfileUtils.GetProfileValue(PROFILE_GODEXWEBURL);
if (string.IsNullOrEmpty(baseUrl))
{
throw new Exception("请先在【基础设置(集团)】-【参数设置】中添加连接科德打印机的电脑的IP地址");
}
return baseUrl;
}
}
/// <summary>
/// 传给打印机的数据
/// </summary>
class PrintData
{
public string Interface { get; set; }
public string USB { get; set; }
public string COM { get; set; }
public string BaudRate { get; set; }
public string LPT { get; set; }
public string IP { get; set; }
public string Port { get; set; }
public string PosX { get; set; }
public string PosY { get; set; }
public string FontHeight { get; set; }
public string Height { get; set; }
public string Wide { get; set; }
public string Narrow { get; set; }
public string FontName { get; set; }
public string Align { get; set; }
public string Data { get; set; }
public string BarcodeType { get; set; }
public string Readable { get; set; }
private void CommondInstance()
{
Interface = "USB";
USB = "";
COM = "";
BaudRate = "";
LPT = "";
IP = "";
Port = "";
PosX = "";
PosY = "";
FontHeight = "";
Height = "";
Wide = "";
Narrow = "";
FontName = "";
Align = "";
Data = "";
BarcodeType = "";
Readable = "";
}
public PrintData()
{
CommondInstance();
}
public PrintData(PrintDataDTO printData)
{
CommondInstance();
PosX = printData.PosX.ToString();
PosY = printData.PosY.ToString();
Data = Convert.ToBase64String(Encoding.Default.GetBytes(printData.Data));
FontHeight = printData.FontHeight.ToString();
Height = printData.FontHeight.ToString();
Align = printData.Align.Value.ToString();
BarcodeType = printData.BarcodeType.Value.ToString();
Readable = printData.Readable.Value.ToString();
Narrow = printData.Narrow.ToString();
Wide = printData.Wide.ToString();
}
}
enum CommandEnum
{
Send,ecTextOut, PrintBarcode, PrintQrcode
}
}