从网站打印票

时间:2017-05-18 13:51:11

标签: c# asp.net ajax printing

您好我试图从我的网站打印收据/票据,但我没有普通打印机,我有一台Epson TM-T88V收据打印机,所以我无法使用正常ctrl + p或打印为普通文档,如何使用此打印机进行打印?我尝试使用此代码,但似乎只能在Windows窗体应用程序中使用,而不是使用Web表单可能问题是我尝试使用ajax调用在WebMethod中执行我的代码。

WebMethod和打印代码

[WebMethod]
public string print()
{
   try
   {
       pd.PrintPage += new PrintPageEventHandler(Imprimir);
       return "Printing...";
    }
    catch (Exception ex)
    {
        return "Error: " + ex.Message;
    }
}

public void Imprimir(object sender, PrintPageEventArgs e)
{
   string titulo = "MyCompany";
   string direccion = "Address";
   string datos = "number             RFC: ";
   string datos2 = "description             17/05/2017 3:55 p.m.";
   PrintDocument ticket = new PrintDocument();

   Graphics g = e.Graphics;
   g.DrawRectangle(Pens.Black, 5, 5, 410, 530);

   Font fBody = new Font("Lucida Console", 15, FontStyle.Bold);
   Font fBody1 = new Font("Lucida Console", 15, FontStyle.Regular);
   Font fBody2 = new Font("Lucida Console", 9, FontStyle.Regular);
   Font rs = new Font("Stencil", 25, FontStyle.Bold);
   Font fTType = new Font("", 150, FontStyle.Bold);
   SolidBrush sb = new SolidBrush(Color.Black);

   g.DrawString(titulo, fBody, sb, 10, 120);
   g.DrawString(direccion, fBody1, sb, 10, 120);
   g.DrawString(datos, fBody1, sb, 10, 120);
   g.DrawString(datos2, fBody1, sb, 10, 120);

   g.DrawString("------------------------------", fBody1, sb, 10, 120);

   g.Dispose();

   pd.PrintController = new StandardPrintController();
   pd.DefaultPageSettings.Margins.Left = 0;
   pd.DefaultPageSettings.Margins.Right = 0;
   pd.DefaultPageSettings.Margins.Top = 0;
   pd.DefaultPageSettings.Margins.Bottom = 0;

   pd.Print();
}

AJAX CALL

function Imprimir() {
      CallWM('../../ws_webservice.asmx/print',
         {  },
         function (r_json) {
              bootbox.alert(r_json);
          });
}

我收到Printing...消息,但没有打印,这是我的问题?另外我想知道是否可以调用Imprimir方法但是发送参数,我该怎么做?

PD。我使用的是C#,ASP.NET,html页面和ajax。

1 个答案:

答案 0 :(得分:2)

将此部分代码从Imprimir转移到print()

   pd.PrintController = new StandardPrintController();
   pd.DefaultPageSettings.Margins.Left = 0;
   pd.DefaultPageSettings.Margins.Right = 0;
   pd.DefaultPageSettings.Margins.Top = 0;
   pd.DefaultPageSettings.Margins.Bottom = 0;

   pd.Print();

有关PrintDocument外观here

的详情,请参阅