简体   繁体   中英

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

I have an ASP Web API and network printer(Epson TM-U220). I need to select the printer by code and print a bill. I just try as bellow. But not work fine. 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();

As many have commented, the Epson TM-U220 does not have the ability to print PDFs.
You need to convert the print content to an ESC/POS control sequence supported by the Epson TM-U220 before sending.

For example, this article and the libraries introduced there may be helpful.
Printing PDF doc to esc/pos Thermal printer
MoienTajik/EscPrinterSample

However, the Epson TM-U220 is a dot printer, and due to its low resolution and low printing speed, the above methods for thermal printers may not give comfortable results.
The printing example you have presented is for a thermal printer, and a dot printer cannot print such beautiful and detailed prints.

By the way, the commented Epson OPOS ADK for .NET v1.14.20E is a client-side printing method, and it is very difficult to operate it on the server side.
What's more, simply embedding it requires understanding and addressing concepts and subsystems such as POS for.NET/OPOS.


Given the above situation, does the receipt need to be a PDF?

If it is okay to call the function to convert to ESC/POS control sequence instead at the timing of creating PDF, you may be able to use the following library.
lukevp/ESC-POS-.NET

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

Even if you use this library, you cannot directly specify printing of PDF.
Print pdf file #13

As mentioned in the above issue, it is possible to convert PDF to PNG using an application or another library and then call the above library, but it will take time and the print result will be poor.
HOW TO CONVERT PDF TO PNG IN C# .NET?

I try this and it works fine. But still, I cannot get printer status.

//--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";
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM