protected override void OnMessage(MessageEventArgs e)
        {
            if (_printerName == null || _printerName == "")
            {
                this.Send("Failed. Printer is not specified.");
                return;
            }


            if (_printerName == "DEBUG")
            {
                string message;
                if (e.IsBinary)
                {
                    message = "RAW DATA";
                }
                else
                {
                    if (e.Data.Length <= 30)
                    {
                        message = e.Data;
                    }
                    else
                    {
                        message = e.Data.Substring(0, 30);
                    }
                }

                ServerController.LogInfo("Print Command Received: \n" + message);
                this.Send("Success");
                return;
            }


            bool opened = myPrinter.OpenPrint(_printerName, "Receipt");

            if (!opened)
            {
                ServerController.LogWarn("Can not open printer [" + _printerName + "]. Print failed.");
                this.Send("Failed");
                return;
            }


            // send data to printer
            if (e.IsBinary)
            {
                myPrinter.SendToPrinter(_printerName, e.RawData, e.RawData.Length);
                this.Send("Success");
                myPrinter.ClosePrint();
                ServerController.LogInfo("Print successfully. Printer [" + _printerName + "]");
            }
            else
            {
                ServerController.LogInfo("Can not send non binary data to printer [" + _printerName + "]. Print failed.");
                this.Send("Failed");
            }
        }
Esempio n. 2
0
        private static void ProceedPrintRequest(HttpRequestEventArgs e)
        {
            var printer      = e.Request.QueryString.Get("printer");
            var documentName = e.Request.QueryString.Get("title");

            bool printerInstalled = false;

            // check if user specified printer

            if (printer == null)
            {
                Utility.ResponseWrite(e.Response, "Printer not specified");
                e.Response.ContentType = "text/plain";
                e.Response.StatusCode  = 400;
                return;
            }

            // check if printer installed

            foreach (string installedPrinter in PrinterSettings.InstalledPrinters)
            {
                if (installedPrinter == printer)
                {
                    printerInstalled = true;
                    break;
                }
            }

            if (!printerInstalled)
            {
                Utility.ResponseWrite(e.Response, "Printer not installed: " + printer);
                e.Response.ContentType = "text/plain";
                e.Response.StatusCode  = 400;
                return;
            }

            // perform printing

            bool opened = myPrinter.OpenPrint(printer, documentName != null ? documentName : "Receipt");

            // check if printer has opened for printing

            if (!opened)
            {
                ServerController.LogWarn("Can not open printer [" + printer + "]. Print failed.");
                Utility.ResponseWrite(e.Response, "Can not open printer [" + printer + "]. Print failed.");
                e.Response.ContentType = "text/plain";
                e.Response.StatusCode  = 500;
                return;
            }

            if (!e.Request.HasEntityBody)
            {
                Utility.ResponseWrite(e.Response, "Invalid raw printing data.");
                e.Response.ContentType = "text/plain";
                e.Response.StatusCode  = 400;
                return;
            }


            // get data and send to printer. then close print job

            byte[] data = Utility.GetRequestPostDataAsBytes(e.Request);
            myPrinter.SendToPrinter(printer, data, data.Length);
            myPrinter.ClosePrint();


            // response with success

            Utility.ResponseWrite(e.Response, "Print job has been sent to printer successfully!");
            e.Response.ContentType = "text/plain";
            e.Response.StatusCode  = 200;
            e.Response.Close();

            return;
        }