Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending raw data to FedEx Label printer

I'm working on a .NET WinForms app that needs to print a FEDEX shipping label. As part of the FedEx api, I can get raw label data for the printer.

I just don't know how to send that data to the printer through .NET (I'm using C#). To be clear, the data is already pre formatted into ZPL (Zebra printer language) I just need to send it to the printer without windows mucking it up.

like image 232
Sukhbir S. Dadwal Avatar asked Dec 13 '22 06:12

Sukhbir S. Dadwal


2 Answers

C# doesn't support raw printing, you'll have to use the win32 spooler, as detailed in this KB article How to send raw data to a printer by using Visual C# .NET.

Hope this helps.

-Adam

like image 122
Adam Davis Avatar answered Dec 15 '22 18:12

Adam Davis


I think you just want to send the ZPL (job below) directly to your printer.

private void SendPrintJob(string job)
{
    TcpClient client = null;
    NetworkStream ns = null;
    byte[] bytes;
    int bytesRead;

    IPEndPoint remoteIP;
    Socket sock = null;

    try
    {
        remoteIP = new IPEndPoint( IPAddress.Parse(hostName), portNum );
        sock = new Socket(AddressFamily.InterNetwork,
            SocketType.Stream,
            ProtocolType.Tcp);
        sock.Connect(remoteIP);


        ns = new NetworkStream(sock);

        if (ns.DataAvailable)
        {
            bytes = new byte[client.ReceiveBufferSize];
            bytesRead = ns.Read(bytes, 0, bytes.Length);
        }

        byte[] toSend = Encoding.ASCII.GetBytes(job);
        ns.Write(toSend, 0, toSend.Length);

        if (ns.DataAvailable)
        {
            bytes = new byte[client.ReceiveBufferSize];
            bytesRead = ns.Read(bytes, 0, bytes.Length);
        }
    }
    finally
    {           
        if( ns != null )            
            ns.Close();

        if( sock != null && sock.Connected )
            sock.Close();

        if (client != null)
            client.Close();
    }
}
like image 26
Austin Salonen Avatar answered Dec 15 '22 20:12

Austin Salonen