我有一个.NET Windows应用程序,它使用ZPL II或EPL2将命令打印到Zebra打印机。是否有办法在直接从Zebra打印机打印之前预览表单中的数据?
请查看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。
我需要在我的应用程序中显示标签。因此,我使用了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地址打印机进行通信的方法。(尽管这并不意味着没有方法。)
127.0.0.1
更改为有效的LAN IP地址。示例代码PM> install-package 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);
}
唯一预览标签的方法是在打印机的网页上。
如果您进入打印机的目录列表 http://<打印机IP>/dir
,然后单击保存的标签(或创建新标签),则可以单击 "预览标签"