简体   繁体   English

如何在客户端没有打印对话框的情况下打印收据

[英]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.我想在没有打印对话框的客户端打印收据,我正在使用 mvc 这是我解决问题的解决方案。 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. EPSON 打印机安装在我的系统中。当主机在我的本地 iis 中时,此解决方案有效,但当主机在服务器中并从我的本地系统访问时出现“处理您的请求时出错”错误时,它不起作用。 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 ?任何人都可以帮助我吗?

设置 System.Drawing.dll 属性

Copy Local=true
  1. first you have to write windows service that contain HttpListener.首先,您必须编写包含 HttpListener 的 Windows 服务。
  2. write your printing code inside the service在服务中编写您的打印代码
  3. Install the service in client machine在客户端机器上安装服务
  4. call that service using ajax like below.使用 ajax 调用该服务,如下所示。

 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 () { } }); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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