如何使用套接字从 .net 中的 POS 打印机打印账单?

How to print a bill from POS printer in .net using socket?

我有一个 ASP Web API 和网络打印机(Epson TM-U220)。我需要通过代码 select 打印机并打印帐单。我只是尝试如下。但不能正常工作。 I want to print this direct using pos printer

var server = "192.168.1.164";
var nome = "www.pdf";

Socket clientSocket = new Socket(AddressFamily.InterNetwork, 
SocketType.Stream, ProtocolType.Tcp);
clientSocket.NoDelay = true;

IPAddress ip = IPAddress.Parse(server);
IPEndPoint ipep = new IPEndPoint(ip, 9100);
clientSocket.Connect(ipep);

clientSocket.Send(File.ReadAllBytes(nome));
clientSocket.Close();

正如许多人评论的那样,爱普生 TM-U220 没有打印 PDF 的功能。
发送前需要将打印内容转换为爱普生TM-U220支持的ESC/POS控制序列

例如,这篇文章和其中介绍的库可能会有帮助。

MoienTajik/EscPrinterSample

不过,爱普生TM-U220是点阵打印机,分辨率低,打印速度慢,热敏打印机的上述方法可能效果不佳。
您提供的打印示例是热敏打印机,点式打印机无法打印出如此精美和细致的打印件。

顺便说一句Epson OPOS ADK for .NET v1.14.20E是客户端打印方式,在服务端操作难度很大
更重要的是,简单地嵌入它需要理解和解决概念和子系统,例如 POS for.NET/OPOS.


鉴于上述情况,收据是否需要为PDF格式?

如果可以在创建PDF时调用转换为ESC/POS控制序列的函数,您可以使用以下库。
lukevp/ESC-POS-.NET

ESCPOS.NET - Easy to use, Cross-Platform, Fast and Efficient.

即使使用这个库,也不能直接指定打印PDF。
Print pdf file #13

如上期所述,可以使用应用程序或其他库将PDF转PNG,再调用上述库,但耗时,打印效果较差
HOW TO CONVERT PDF TO PNG IN C# .NET?

我试了一下,效果很好。但是,我仍然无法获取打印机状态。

//--printer script model
public class DirectPrintingScript
{
    public string Value { get; set; }
    public int PrintMethodID { get; set; }
    public bool IsNeedPrint { get; set; }
}

//--print method
public void print(List<DirectPrintingScript> result, string IP, int Port)
{
    var job = new DirectPrinterProcess();
    Socket clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
    clientSocket.NoDelay = true;

    IPAddress ip = IPAddress.Parse(IP);
    IPEndPoint ipep = new IPEndPoint(ip, Port);
    clientSocket.Connect(ipep);
    Encoding enc = Encoding.ASCII;

    foreach (DirectPrintingScript item in result)
    {
        var command = job.SelectMethod(item.PrintMethodID);
        byte[] commandBytes = Encoding.ASCII.GetBytes(command);
        byte[] contentBytes = Encoding.ASCII.GetBytes(item.Value);
        clientSocket.Send(commandBytes);

        if (item.IsNeedPrint)
        {
           clientSocket.Send(contentBytes);
           var n = job.NewLine();
           byte[] nBytes = Encoding.ASCII.GetBytes(n);
           clientSocket.Send(nBytes);
        }
     }

     // Line feed hexadecimal values
     byte[] bEsc = new byte[4];
     bEsc[0] = 0x0A;
     bEsc[1] = 0x0A;
     bEsc[2] = 0x0A;
     bEsc[3] = 0x0A;

     // Send the bytes over 
     clientSocket.Send(bEsc);

     clientSocket.Close();
}

//--print method process
    public class DirectPrinterProcess
    {
        public string SelectMethod(int MethodID)
        {
            switch (MethodID)
            {
                case 1:
                    return JustificationCenter();
                case 2:
                    return JustificationLeft();
                case 3:
                    return DoubleHeight();
                case 4:
                    return DoubleWidth();
                case 5:
                    return CancelDoubleHeightWidth();
                case 6:
                    return SetColorRed();
                case 7:
                    return SetColorBlack();
                default:
                    return CancelDoubleHeightWidth();
            }
        }

        private string JustificationCenter()
        {
            return "" + (char)27 + (char)97 + (char)1;
        }

        private string JustificationLeft()
        {
            return "" + (char)27 + (char)97 + (char)0;
        }

        private string DoubleHeight()
        {
            return "" + (char)27 + (char)33 + (char)16;
        }

        private string DoubleWidth()
        {
            return "" + (char)27 + (char)33 + (char)32;
        }

        private string CancelDoubleHeightWidth()
        {
            return "" + (char)27 + (char)33 + (char)0;
        }

        private string SetColorRed()
        {
            return "" + (char)27 + (char)114 + (char)1;
        }

        private string SetColorBlack()
        {
            return "" + (char)27 + (char)114 + (char)0;
        }

        public string NewLine()
        {
            return "" + "\n";
        }
    }