如何在没有提示的情况下从 Internet Explorer 直接打印到标签打印机

发布于 2024-08-16 10:03:47 字数 1002 浏览 6 评论 0原文

我有一个包含 upc 编号的 mysql 数据库,我想用斑马打印机 (LP2824) 打印到标签上。我希望能够以某种方式将这些数字直接发送到我的标签打印机,这不是默认打印机,无需提示用户。

据我所知,IE 是唯一的选择,因为需要某种类型的 ActiveX 控件才能实现这一点。我在 meadroid.com 上找到了使用 ScriptX 的直接打印示例,但我不愿意支付他们的许可费。我还发现了一个使用 VBScript 的示例,它将页面发送到默认打印机而不提示用户。我的问题是我无法弄清楚如何删除 IE 中存在的默认页眉和页脚,并且我无法选择要使用的打印机。

我的问题是:

是否有一个 activeX 控件,我可以通过类似于 MeadCo 的 ScriptX 的 javascript 使用,但无需许可,或者 VBScript 方法会更好,在这种情况下,人们如何删除页眉和页脚,并打印到默认情况下没有设置标签打印机?直接向打印机发送一系列命令的能力将是理想的。

这是我到目前为止的测试代码:

<head>
<script language='VBScript'>
Sub Print()
       OLECMDID_PRINT = 6
       OLECMDEXECOPT_DONTPROMPTUSER = 2
       OLECMDEXECOPT_PROMPTUSER = 1
       call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,1)
End Sub
document.write "<object ID='WB' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>"
</script>

</head>

<body>
Helo!!!
<a href="javascript:window.print();">Print</a>

</body>

I have a mysql database containing upc numbers that I would like to print to labels with a zebra printer (LP2824). I would like to somehow be able to send those numbers directly to my label printer, which is not the default printer without the user being prompted.

As far as I can tell, IE is the only option as some type of activeX control is required to make this happen. I found a direct printing example at meadroid.com using ScriptX, but I'm not willing to pay their licensing fee. I also found an example using VBScript that will send the page to the default printer without prompting the user. My problem with that is I can't figure out how to remove the default header and footer present in IE and I can't choose which printer to use.

My question is:

Is there an activeX control out there that I can use via javascript similar to MeadCo's ScriptX but without licensing, or would the VBScript approach be better, in which case how can a person remove the header and footer, and print to the label printer which is not set at default? The ability to just send a series of commands directly to the printer would be ideal.

Here is the test code I have so far:

<head>
<script language='VBScript'>
Sub Print()
       OLECMDID_PRINT = 6
       OLECMDEXECOPT_DONTPROMPTUSER = 2
       OLECMDEXECOPT_PROMPTUSER = 1
       call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,1)
End Sub
document.write "<object ID='WB' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>"
</script>

</head>

<body>
Helo!!!
<a href="javascript:window.print();">Print</a>

</body>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绳情 2024-08-23 10:03:47

当我们需要在将货物放到货架上之前打印传入交货的物品标签时,我在使用基于网络的 ERP 系统时遇到了这个问题。我们最初的问题是,我们无法让 IE 打印到我们为标签打印机 (Zebra TLP-2844) 配置的指定标签尺寸。 IE始终使用默认打印机的A4格式。以“正常方式”打印还需要进行大量额外的点击和设置,而这些简单的任务只需在基于 Windows 的应用程序中单击一次(您可以更好地控制打印机设置)。

我做了很多研究,发现一些ActiveX控件能够详细控制打印机设置,但最终正常的打印方式仍然会导致不必要的点击次数,而客户希望它超级简单超级快速地。

由于我以前有使用标签打印机开发应用程序的经验,因此我知道可以使用打印机的内部打印机语言。在 Zebra 打印机中称为 EPL/ZPL。它是一种简单的基于文本的协议,可通过打印机提供的任何接口(例如并行端口、串行端口、USB 或 10/100 以太网)使用。我之前刚刚使用过串行端口接口,但在这个项目中,我必须能够通过互联网、穿过防火墙打印到连接在客户办公室本地网络上的 Zebra 打印机。经过一些研究,事实证明这实际上非常简单,并且最终对客户来说是一个非常好的解决方案。

基本的 EPL 语言有其局限性,但通常您只想要一个非常简单的打印输出,包括一些文本和一些条形码或其他东西。您只能使用五种尺寸的固定宽度标准字体。可以将字体宽度和高度设置为原始大小的倍数。如果您需要更高级的打印输出,包括图片或某些布局、字体、尺寸等,可以通过使用特殊的标签设计应用程序创建模板来完成。模板可以包含调用模板时可以使用的变量。

为了充分理解我的小例子并了解更多详细信息,我建议您下载 EPL 程序员手册。您可能还需要配置打印机才能接受命令,该过程应该在手册中。

下面是 ASP.NET 2.0 (C#) 的简单示例。当我最初制作一个控制台应用程序进行测试时,我刚刚注释掉了一些错误处理。该示例基于功能齐全的客户解决方案,但我排除并重写了一些部分以使该示例更容易理解。我已经测试了基本功能,返回的字符串似乎没问题,但我还没有测试打印到 Zebra 打印机,因为我目前无法访问任何打印机,因此请注意意外错误。 :) 不过应该没问题。

祝你好运!

资源:

Zebra - 资源库

注意:您需要注册才能下载 EPL 程序员手册

示例代码:

<%@ Import Namespace = "System.Data" %>
<%@ Import Namespace = "System.Text" %>
<%@ Import Namespace = "System.Net.Sockets" %>
<%@ Import Namespace = "System.IO" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script language="C#" runat="server">
    public String strPrintOutput;

    void Page_Load(Object sender,EventArgs e)
    {
        // Build output string - Example - Printer Settings
        //strPrintOutput = BuildString1();

        // Build output string - Example - Print Label with Barcode and some text (Prints two labels)
        strPrintOutput = BuildString2("123456", "2010-01-02", "1234567890", "123", "Harddrive", "D31", "123456", "2");

        // Print output to HTML-page
        txtPrintout.Text = strPrintOutput;

        // Send output to Zebra Printer
        // xxx.xxx.xxx.xxx = The IP of the Zebra printer
        // 9100 = The default printer port, but that could be changed on the printer
        // For external access (e.g. through the internet) you need to use the IP/domain name of the router/firewall and also open a port in the firewall and forward the traffic to the internal IP and port set on the printer
        // Note that for external access appropriate security measures should be added in some way.
        Connect("xxx.xxx.xxx.xxx", 9100, strPrintOutput);
    }

  public void Connect(String ip, Int32 port, String strMessage)
  {
    try
    {
      // Convert String to Byte Array with encoding Windows-1252
      //Byte[] data = System.Text.Encoding.ASCII.GetBytes(strMessage);
      Byte[] data = System.Text.Encoding.GetEncoding(1252).GetBytes(strMessage);

      // Create connection to printer
      TcpClient printer = new TcpClient(ip, port);

      // Get stream object of printer
      NetworkStream strm = printer.GetStream();

      // Write message to stream
      strm.Write(data, 0, data.Length);

      // Status Message
      //Console.WriteLine("Sent: {0}", strMessage);

      // Close stream
      strm.Close();

      //Close connection to printer
      printer.Close();

    } // end try

    catch (ArgumentNullException e)
    {
        //Console.WriteLine("ArgumentNullException: {0}", e);
    }

    catch (SocketException e)
    {
        //Console.WriteLine("SocketException: {0}", e);
    }

    //Console.WriteLine("\n Press Enter to continue...");
    //Console.Read();
  }

  /*
   * \n = LF (Line Feed)
   * \r = CR (Carriage Return)
   * \r\n = CrLf
   * Printer ignores CR when sent
   * CR cannot be used in place of LF
  */
  public String BuildString1()
  {
    StringBuilder sb = new StringBuilder();

    //Clears out printer buffer
    sb.Append("\n");

        //Print the current printer configuration
    sb.Append("U\n");

    return sb.ToString();
  }

  public String BuildString2(String strBarCode, String strDate, String strArticleNumber, String strPurchaseOrderNumber, String strArticleName, String strLocation, String strOrderNumber, String strNoOfLabels)
  {
    StringBuilder sb = new StringBuilder();

    //Clears out printer buffer
    sb.Append("\n");

    //Clears out the image buffer
    sb.Append("N\n");

    //Set 8-bit, CodePage Windows 1252 (Latin 1), KDU Sweden (Printer with display)
    sb.Append("I8,A,046\n");

        // Create Print Objects
    sb.Append("B35,5,0,3C,3,6,60,N,\"" + strBarCode + "\"\n");
    sb.Append("A35,75,0,3,1,1,N,\"Article No:\"\n");
    sb.Append("A35,100,0,3,1,1,N,\"Article Name:\"\n");
    sb.Append("A35,125,0,3,1,1,N,\"Location:\"\n");
    sb.Append("A35,150,0,3,1,1,N,\"Order:\"\n");

    sb.Append("A205,75,0,3,1,1,N,\"" + strArticleNumber + "\"\n");
    sb.Append("A205,100,0,3,1,1,N,\"" + strArticleName + "\"\n");
    sb.Append("A205,125,0,3,1,1,N,\"" + strLocation + "\"\n");
    sb.Append("A205,150,0,3,1,1,N,\"" + strOrderNumber + "\"\n");

    sb.Append("A432,49,0,3,1,1,N,\"" + strDate + "\"\n");
    sb.Append("A530,75,0,3,1,1,N,\"" + strPurchaseOrderNumber + "\"\n");
    sb.Append("P" + strNoOfLabels + "\nN\n\n");

    return sb.ToString();
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Print to Zebra Label Printer</title>
</head>
<body>
    <form id="form1" runat="server">
      <asp:Panel runat="server">
        <asp:TextBox id="txtPrintout" TextMode="MultiLine" runat="server" BackColor="LightBlue" Width="100%" Height="800px"></asp:TextBox>
      </asp:Panel>
    </form>
</body>
</html>

I've had this exact problem with a web based ERP system when we needed to print article labels for incoming deliveries before putting the goods on the storing shelfs. Our initial problem was that we couldn't get IE to print to a specified label size that we configured for the label printer (Zebra TLP-2844). IE always used the default printer's A4 format. To print the "normal way" also resulted in a lot of extra clicks and settings that had to be made just for this simple task that would only take one click in a windows based application (where you have more control over the printer settings).

I made a lot of research and found some ActiveX control that were able to control the printer settings in detail but in the end the normal way of printing still would result in an unnecessary number of clicks and the customer wanted it to be super simple and super fast.

As I have former experience developing applications using label printers I knew of the printer's internal printer language that could be used. In Zebra printers it's called EPL/ZPL. It's a simple text based protocol that can be used through any of the interfaces that the printer offer e.g. Parallell Port, Serial Port, USB or 10/100 Ethernet. I have just used the serial port interface before but in this project I had to be able to print over the internet, through the firewall, to a Zebra printer connected on the local network at the customers office. After some research this proved to be quite easy actually and it ended up to be a really great solution for the customer.

The basic EPL language has its limits but often you just want a quite simple printout including some text and some bar codes or something. You are limited to a fixed width standard font in five sizes. It's possible to set the font width and height to a multiple of the original size. If you need more advanced printouts including pictures or certain layouts, fonts, sizes, etc, that could be done by creating templates with a special Label Design Application. The templates can contain variables that can be used when calling the template.

To fully understand my little example and for more details I recommend you to download the EPL Programmers manual. You also might have to configure the printer to be able to accept the commands, the procedure should be in the manual.

Below you find a simple example for ASP.NET 2.0 (C#). As I initially made a console application for testing there is some error handling that I just commented out. The example is based on a fully functional customer solution but I have excluded and rewritten some parts to make the example more understandable. I have tested the basic functionality and the returned strings seems to be ok, but I haven't tested to print to a Zebra printer as I don't have access to any at the moment, so be aware of unexpected bugs. :) But it should be ok.

Good luck!

Resources:

Zebra - Resource Library

Note: You need to register to be able to download the EPL Programmers Manual

Example code:

<%@ Import Namespace = "System.Data" %>
<%@ Import Namespace = "System.Text" %>
<%@ Import Namespace = "System.Net.Sockets" %>
<%@ Import Namespace = "System.IO" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script language="C#" runat="server">
    public String strPrintOutput;

    void Page_Load(Object sender,EventArgs e)
    {
        // Build output string - Example - Printer Settings
        //strPrintOutput = BuildString1();

        // Build output string - Example - Print Label with Barcode and some text (Prints two labels)
        strPrintOutput = BuildString2("123456", "2010-01-02", "1234567890", "123", "Harddrive", "D31", "123456", "2");

        // Print output to HTML-page
        txtPrintout.Text = strPrintOutput;

        // Send output to Zebra Printer
        // xxx.xxx.xxx.xxx = The IP of the Zebra printer
        // 9100 = The default printer port, but that could be changed on the printer
        // For external access (e.g. through the internet) you need to use the IP/domain name of the router/firewall and also open a port in the firewall and forward the traffic to the internal IP and port set on the printer
        // Note that for external access appropriate security measures should be added in some way.
        Connect("xxx.xxx.xxx.xxx", 9100, strPrintOutput);
    }

  public void Connect(String ip, Int32 port, String strMessage)
  {
    try
    {
      // Convert String to Byte Array with encoding Windows-1252
      //Byte[] data = System.Text.Encoding.ASCII.GetBytes(strMessage);
      Byte[] data = System.Text.Encoding.GetEncoding(1252).GetBytes(strMessage);

      // Create connection to printer
      TcpClient printer = new TcpClient(ip, port);

      // Get stream object of printer
      NetworkStream strm = printer.GetStream();

      // Write message to stream
      strm.Write(data, 0, data.Length);

      // Status Message
      //Console.WriteLine("Sent: {0}", strMessage);

      // Close stream
      strm.Close();

      //Close connection to printer
      printer.Close();

    } // end try

    catch (ArgumentNullException e)
    {
        //Console.WriteLine("ArgumentNullException: {0}", e);
    }

    catch (SocketException e)
    {
        //Console.WriteLine("SocketException: {0}", e);
    }

    //Console.WriteLine("\n Press Enter to continue...");
    //Console.Read();
  }

  /*
   * \n = LF (Line Feed)
   * \r = CR (Carriage Return)
   * \r\n = CrLf
   * Printer ignores CR when sent
   * CR cannot be used in place of LF
  */
  public String BuildString1()
  {
    StringBuilder sb = new StringBuilder();

    //Clears out printer buffer
    sb.Append("\n");

        //Print the current printer configuration
    sb.Append("U\n");

    return sb.ToString();
  }

  public String BuildString2(String strBarCode, String strDate, String strArticleNumber, String strPurchaseOrderNumber, String strArticleName, String strLocation, String strOrderNumber, String strNoOfLabels)
  {
    StringBuilder sb = new StringBuilder();

    //Clears out printer buffer
    sb.Append("\n");

    //Clears out the image buffer
    sb.Append("N\n");

    //Set 8-bit, CodePage Windows 1252 (Latin 1), KDU Sweden (Printer with display)
    sb.Append("I8,A,046\n");

        // Create Print Objects
    sb.Append("B35,5,0,3C,3,6,60,N,\"" + strBarCode + "\"\n");
    sb.Append("A35,75,0,3,1,1,N,\"Article No:\"\n");
    sb.Append("A35,100,0,3,1,1,N,\"Article Name:\"\n");
    sb.Append("A35,125,0,3,1,1,N,\"Location:\"\n");
    sb.Append("A35,150,0,3,1,1,N,\"Order:\"\n");

    sb.Append("A205,75,0,3,1,1,N,\"" + strArticleNumber + "\"\n");
    sb.Append("A205,100,0,3,1,1,N,\"" + strArticleName + "\"\n");
    sb.Append("A205,125,0,3,1,1,N,\"" + strLocation + "\"\n");
    sb.Append("A205,150,0,3,1,1,N,\"" + strOrderNumber + "\"\n");

    sb.Append("A432,49,0,3,1,1,N,\"" + strDate + "\"\n");
    sb.Append("A530,75,0,3,1,1,N,\"" + strPurchaseOrderNumber + "\"\n");
    sb.Append("P" + strNoOfLabels + "\nN\n\n");

    return sb.ToString();
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Print to Zebra Label Printer</title>
</head>
<body>
    <form id="form1" runat="server">
      <asp:Panel runat="server">
        <asp:TextBox id="txtPrintout" TextMode="MultiLine" runat="server" BackColor="LightBlue" Width="100%" Height="800px"></asp:TextBox>
      </asp:Panel>
    </form>
</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文