0

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();
  • 3
    You can't just send a PDF byte-stream to a printer... _that's not how printers work_ – Dai Dec 23 '21 at 08:20
  • I recommend you read-up on established network printing protocols like CUPS: https://www.cups.org/ – Dai Dec 23 '21 at 08:20
  • I just wanna print kitchen bills with different text sizes using the above methods. Is there a way to handle Epson codes to achieve it and how? – Keerthi Srilal Dec 23 '21 at 08:40
  • See here: https://stackoverflow.com/questions/8562292/cups-printing-of-pdf-files – Dai Dec 23 '21 at 08:46
  • 1
    I suggest you edit the question and explain what you *actually* want. A POS printer is completely different from a normal printer. With normal printers you don't need sockets or TCP connections, you can use .NET's printer classes to print to a configured Windows printer. POS printers don't appear as normal printers though BUT they may appear as virtual serial or LPT ports to which programs send printer commands. Some may have their own printer drivers. – Panagiotis Kanavos Dec 23 '21 at 09:01
  • 1
    You can't just copy a PDF file to them, you have to convert it. It's a lot easier to just send a string with the print commands and text to the printer than create a PDF then try to convert it to print commands. Even then, you can't just send a bunch of bytes to the printer. The packets you send need to have a format understood by the printer. When you work with a virtual serial or LPT port, the driver takes care of packaging the bytes. When you talk to the printer directly, you have to do this. The printer's docs or support site *may* explain what's needed – Panagiotis Kanavos Dec 23 '21 at 09:04
  • 1
    Looking at the [printer's support page](https://epson.com/Support/Point-of-Sale/Impact-Printers-%28Dot-Matrix%29/Epson-TM-U220/s/SPT_C31C514103) I see that `Epson OPOS ADK for .NET v1.14.20E` is the very first download. Have you tried using this? – Panagiotis Kanavos Dec 23 '21 at 09:07

2 Answers2

0

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?

kunif
  • 4,060
  • 2
  • 10
  • 30
0

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