在发送到 Zebra 打印机之前使用 .NET WinForm 打印预览 ZPL II 命令

发布于 2024-12-05 01:10:15 字数 101 浏览 3 评论 0原文

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

I have an .NET Windows application that prints commands to Zebra printer using ZPL II or EPL2.
Is there any way to print preview the data in a form before printing it directly from Zebra printer?

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

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

发布评论

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

评论(5

故乡的云 2024-12-12 01:10:15

查看 Labelary Web 服务,它允许您以编程方式将 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 查看器,让您可以直接在网页上编辑和查看 ZPL。

Have a look at the Labelary web service, which allows you to convert ZPL to an image programmatically.

Just build a URL containing the ZPL that you want to render, get the image back from the web server, and show the image to the user from within your application.

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");
}

There's also a ZPL viewer lets you edit and view ZPL directly on a web page.

○愚か者の日 2024-12-12 01:10:15

我需要能够在我的应用程序中显示标签。所以我连接了 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 地址打印机。 (但这并不意味着没有办法。)

I needed the ability to show the label in my Application. So I hooked up Fiddler and figured out what the communication was to get the image of the label.

I got it running in LinqPad. The HTTP stuff can be cleaned up a bit, but I thought I would post the code for others to use:

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;
    }   
}

This has the following References:

HtmlAgilityPack
System.Drawing
System.Net

and the following Uses:

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

NOTE: I could not find a way to communicate with a serial/non-IP Addressed printer. (Though that does not mean that there is not a way.)

萌化 2024-12-12 01:10:15

如果您可以使用 Chrome 插件,请尝试
Simon Binkert 的 Zpl 打印机。该插件基于之前评论中链接的 Labelary Web Service

另请参阅:https://stackoverflow.com/a/33066790/3196753

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

chrome zpl 打印机

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

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

If you're ok with using a Chrome Plugin, please try
Simon Binkert's Zpl Printer. This plugin is based on the Labelary Web Service linked in previous comments.

See also: https://stackoverflow.com/a/33066790/3196753

Note, although this plugin runs inside Google Chrome, it can work with any application on the computer capable of sending raw commands.

chrome zpl printer

It can be configured to listen on localhost or dedicated IP address, so it can be used to emulate a ZPL printer on the PC and local network. If you need a local printer or printer share, you can set up a raw printer queue on the PC which points to it on hostname/ip and port and other desktop applications will be able to send ZPL to it.

Note, if you need to make it available to other computers on the network, make sure to change 127.0.0.1 in the Printer Settings to a valid LAN IP address.

惜醉颜 2024-12-12 01:10:15

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

更多信息可在此处获取 BinaryKits.Zpl.Viewer (GitHub)

还有一个 nuget 包 可用

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

If you don't want to send your data to a cloud service you can have a look at our project we have developed an open ZPL viewer that converts ZPL data into a graphic. The project is based on .NET.

More Informations are available here BinaryKits.Zpl.Viewer (GitHub)

Also a nuget package is available

PM> install-package BinaryKits.Zpl.Viewer

Example code

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);
}
向日葵 2024-12-12 01:10:15

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

如果您转到打印机的目录列表 http:///dir 并单击已保存的标签(或创建新标签),则可以单击“预览标签”

The only way to preview the label is on the printer's web page.

If you go to the printer's directory listing http://<printer IP>/dir and click on the saved label (or create a new one) then you can click "Preview Label"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文