【问题标题】:Print preview ZPL II commands using .NET WinForm before sending it to Zebra printer在将其发送到 Zebra 打印机之前,使用 .NET WinForm 打印预览 ZPL II 命令
【发布时间】:2011-11-16 18:07:22
【问题描述】:

我有一个 .NET Windows 应用程序,它使用 ZPL II 或 EPL2 将命令打印到 Zebra 打印机。 有什么方法可以在直接从 Zebra 打印机打印之前打印预览表单中的数据?

【问题讨论】:

    标签: .net winforms zpl epl zpl-ii


    【解决方案1】:

    查看Labelary web service,它允许您以编程方式将 ZPL 转换为图像。

    只需构建一个包含您要呈现的 ZPL 的 URL,从 Web 服务器取回图像,然后从您的应用程序中将图像显示给用户。

    string zpl = "YOUR ZPL HERE";
    string url = "http://api.labelary.com/v1/printers/8dpmm/labels/4x6/0/" + zpl;
    using (WebClient client = new WebClient()) {
        client.DownloadFile(url, "zpl.png");
    }
    

    还有一个ZPL viewer 可让您直接在网页上编辑和查看 ZPL。

    【讨论】:

      【解决方案2】:

      我需要能够在我的应用程序中显示标签。所以我连接了 Fiddler,并弄清楚了获得标签图像的通信是什么。

      我让它在 LinqPad 中运行。 HTTP的东西可以清理一下,但我想我会发布代码供其他人使用:

      void Main()
      {
          string printerIpAddress = "10.92.0.167";
          string zpl="^XA^CFD^CVY^PON^FWN^LS0^LT0^LH15,17^FS^FO0,2^FO14,3^FH^FDHi^FS^XZ";
      
          // post the data to the printer
          var imageName = PostZplAndReturnImageName(zpl, printerIpAddress);
      
          // Get the image from the printer
          var image = LoadImageFromPrinter(imageName, printerIpAddress);
      
          Console.WriteLine(image);   
      }
      
      
      public static string PostZplAndReturnImageName(string zpl, string printerIpAddress)
      {
          string response = null;
          // Setup the post parameters.
          string parameters = "data="+ zpl;
          parameters = parameters + "&" + "dev=R";
          parameters = parameters + "&" + "oname=UNKNOWN";
          parameters = parameters + "&" + "otype=ZPL";
          parameters = parameters + "&" + "prev=Preview Label";
          parameters = parameters + "&" + "pw=";
      
          // Post to the printer
          response = HttpPost("http://"+ printerIpAddress +"/zpl", parameters);
      
          // Parse the response to get the image name.  This image name is stored for one retrieval only.
          HtmlAgilityPack.HtmlDocument doc = new HtmlDocument();
          doc.LoadHtml(response);
          var imageNameXPath = "/html[1]/body[1]/div[1]/img[1]/@alt[1]";
          var imageAttributeValue = doc.DocumentNode.SelectSingleNode(imageNameXPath).GetAttributeValue("alt","");
          // Take off the R: from the front and the .PNG from the back.
          var imageName = imageAttributeValue.Substring(2);
          imageName = imageName.Substring(0,imageName.Length - 4);
      
          // Return the image name.
          return imageName;
      }
      
      
      public static string HttpPost(string URI, string Parameters) 
      {
         System.Net.WebRequest req = System.Net.WebRequest.Create(URI);
         req.Proxy = new System.Net.WebProxy();
      
         //Add these, as we're doing a POST
         req.ContentType = "application/x-www-form-urlencoded";
         req.Method = "POST";
      
         //We need to count how many bytes we're sending. 
         //Post'ed Faked Forms should be name=value&
         byte [] bytes = System.Text.Encoding.ASCII.GetBytes(Parameters);
         req.ContentLength = bytes.Length;
      
         System.IO.Stream os = req.GetRequestStream();
         os.Write (bytes, 0, bytes.Length); //Push it out there
         os.Close ();
      
         System.Net.WebResponse resp = req.GetResponse();
      
         if (resp== null) return null;
         System.IO.StreamReader sr = 
               new System.IO.StreamReader(resp.GetResponseStream());
         return sr.ReadToEnd().Trim();
      }
      
      public static Image LoadImageFromPrinter(string imageName, string printerIpAddress)
      {
          string url = "http://"+ printerIpAddress +"/png?prev=Y&dev=R&oname="+ imageName +"&otype=PNG";  
      
          var response = Http.Get(url);   
      
          using (var ms = new MemoryStream(response))
          {       
              Image image = Image.FromStream(ms);
      
              return image;
          }  
      
      
      }
      
      public static class Http
      {
          public static byte[] Get(string uri)
          {               
              byte[] response = null;
              using (WebClient client = new WebClient())
              {
                  response = client.DownloadData(uri);
              }
              return response;
          }   
      }
      

      这有以下参考:

      HtmlAgilityPack
      System.Drawing
      System.Net
      

      以及以下用途:

      HtmlAgilityPack
      System.Drawing
      System.Drawing.Imaging
      System.Net
      

      注意:我找不到与串行/非 IP 地址打印机通信的方法。 (虽然这并不意味着没有办法。)

      【讨论】:

      • 看起来不错!您能否添加您的代码使用的打印机供应商和型号?
      • @bluish - Zebra 标签打印机。我在 ZM400 上进行了测试。
      • 也适用于 Zebra170Xi4。我发现打印机生成的图像会在渲染标签周围产生大量空白。我不确定在 ZPL 中设置标签宽度是否可以解决这个问题(我们的大多数设计都没有)。
      • 您的打印机 IP 地址使用了什么?它是否通过 USB 连接到您的计算机?或者它只是在打印服务器下?知道如何仅通过 USB 或本地网络使用它吗?
      • @JWiley - 我不知道这可以通过 USB 工作。我们使用局域网上的打印机。它有一个固定的 IP 地址(由我的系统管理员设置)。您可以从我使用的 ZM400 打印机获取 IP 地址。
      【解决方案3】:

      如果您可以使用 Chrome 插件,请尝试 西蒙宾克特的Zpl Printer。此插件基于之前 cmets 中链接的Labelary Web Service

      另见:https://stackoverflow.com/a/33066790/3196753

      请注意,尽管此插件在 Google Chrome 中运行,但它可以与计算机上能够发送原始命令的任何应用程序一起使用。

      它可以配置为侦听本地主机或专用 IP 地址,因此可以用于在 PC 和本地网络上模拟 ZPL 打印机。如果您需要本地打印机或打印机共享,您可以在 PC 上设置一个原始打印机队列,该队列在主机名/IP 和端口上指向它,其他桌面应用程序将能够向它发送 ZPL。

      注意,如果您需要将其提供给网络上的其他计算机,请确保将打印机设置中的 127.0.0.1 更改为有效的 LAN IP 地址。

      【讨论】:

        【解决方案4】:

        预览标签的唯一方法是在打印机的网页上。

        如果您转到打印机的目录列表http://<printer IP>/dir 并单击已保存的标签(或创建一个新标签),则可以单击"Preview Label"

        【讨论】:

        • 还有其他几种方法可以使用打印机格式化标签而不打印。可以接受在线打印机吗?
        • 如果我只安装了打印机驱动程序而不是实际的打印机怎么办?我还能访问此目录列表吗?
        • 对于其他寻找如何执行此操作的人,请参阅我的答案,其中包含获取标签预览的代码。
        【解决方案5】:

        如果您不想将数据发送到云服务,可以查看我们的项目,我们开发了一个开放的 ZPL 查看器,可以将 ZPL 数据转换为图形。该项目基于.NET

        更多信息请点击此处BinaryKits.Zpl.Viewer (GitHub)

        还有nuget package 可用

        PM> 安装包 BinaryKits.Zpl.Viewer

        示例代码

        IPrinterStorage printerStorage = new PrinterStorage();
        var drawer = new ZplElementDrawer(printerStorage);
        
        var analyzer = new ZplAnalyzer(printerStorage);
        var analyzeInfo = analyzer.Analyze("^XA^FT100,100^A0N,67,0^FDTestLabel^FS^XZ");
        
        var labels = new List<LabelDto>();
        foreach (var labelInfo in analyzeInfo.LabelInfos)
        {
            var imageData = drawer.Draw(labelInfo.ZplElements);
        }
        

        【讨论】:

          猜你喜欢
          • 2011-01-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-28
          • 2011-05-25
          • 2015-02-10
          • 2023-03-19
          • 1970-01-01
          相关资源
          最近更新 更多