surendra
surendra

Reputation: 63

How to print receipt without print dialog box in client side

I want print receipt in client side without print dialog box, i am using mvc this is my solution to achieve my problem. EPSON printer was installed in my system.This solution is working when host in my local iis but its not work when host in server and accessing from my local system getting "An error occurred while processing your request" error. In server no printer is installed.

$.ajax({
type: "POST",
url: '../Service/print',
cache: false,
data: { iprintData: printData, iprinterName: sPrinterName },
success: function (data) {
// alert('print Send Successfully');
},
error: function (ex) {
   alert(ex.responseText);
// alert('error while Seding print');
}
});

this is my code in controller

    public JsonResult print(string iprintData, string iprinterName)
    {
        Boolean bflag = false;
        System.Web.HttpContext.Current.Session["_printData"] = iprintData; 
        PrintDocument printDocument = new PrintDocument();
        printDocument.PrintController = new StandardPrintController();            
        printDocument.PrintPage += PrintDocumentOnPrintPage;
        printDocument.PrinterSettings.PrinterName = iprinterName;
        //printFont = new System.Drawing.Font("Arial", 10);
        printDocument.Print();
        bflag = true;            
        return Json(bflag, JsonRequestBehavior.AllowGet);
    }

    public static Image resizeImage(Image image, int new_height, int new_width)
    {
        Bitmap new_image = new Bitmap(new_height, new_width);
        Graphics g = Graphics.FromImage((Image)new_image);
        g.InterpolationMode = InterpolationMode.High;
        g.DrawImage(image, 0, 0, new_width, new_height);
        return new_image;
    }
    private void PrintDocumentOnPrintPage(object sender, PrintPageEventArgs e)
    {
        string printstring = System.Web.HttpContext.Current.Session["_printData"].ToString();
        string path = HttpContext.Server.MapPath("~/content/Images/logo.png");
        System.Drawing.Image img = Image.FromFile(path);


        //img = resizeImage(img, 80, 60);
        e.Graphics.DrawImage(img, 6, 100);
        e.Graphics.DrawString(printstring, new System.Drawing.Font("ronnia", 9), Brushes.Black, 10, 150);
    }

can any one help me from this ?

Upvotes: 1

Views: 1423

Answers (2)

Jithi Vasudev
Jithi Vasudev

Reputation: 13

  1. first you have to write windows service that contain HttpListener.
  2. write your printing code inside the service
  3. Install the service in client machine
  4. call that service using ajax like below.

    function PrintReceipt() {

        var PrintData = JSON.parse($("#receiptData").html())

        if (PrintData.length > 0) {

            $.ajax({
                type: "POST", 
                url: "http://localhost:41963/printOrder",
                data: JSON.stringify({ "PrintData": PrintData }), //reciept data
                crossDomain: true, 
                success: function (response) {

                    
                },
                error: function () {
                  
                }
            });
        }

       
    }

Upvotes: 1

sarath
sarath

Reputation: 314

set System.Drawing.dll property

Copy Local=true

Upvotes: 0

Related Questions