将 ZPL 字符串转换为 JPG 图像和 PDF

Transforming a ZPL string into a JPG image and PDF

下午好,

我目前正在从事一个动态生成 ZPL 字符串的项目。下面你可以看到一个例子,你可以使用 http://labelary.com/viewer.html 查看标签。是否有任何软件可以将 ZPL 字符串转换为 JPG 图像或 PDF 文件?

"^XA^FX Top section with company logo, name and address.^CF0,60^FO50,50^GB100,100,100^FS^FO75,75^FR^GB100,100,100^FS^FO88,88^GB50,50,50^FS^FO220,50^FDInternational Shipping, Inc.^FS^CF0,40^FO220,100^FD1000 Shipping Lane^FS^FO220,135^FDShelbyville TN 38102^FS^FO220,170^FDUnited States (USA)^FS^FO50,250^GB700,1,3^FS^FX Second section with recipient address and permit information.^CFA,30^FO50,300^FDJohn Doe^FS^FO50,340^FD100 Main Street^FS^FO50,380^FDSpringfield TN 39021^FS^FO50,420^FDUnited States (USA)^FS^CFA,15^FO600,300^GB150,150,3^FS^FO638,340^FDPermit^FS^FO638,390^FD123456^FS^FO50,500^GB700,1,3^FS^FX Third section with barcode.^BY5,2,270^FO175,550^BC^FD1234567890^FS^FX Fourth section (the two boxes on the bottom).^FO50,900^GB700,250,3^FS^FO400,900^GB1,250,3^FS^CF0,40^FO100,960^FDShipping Ctr. X34B-1^FS^FO100,1010^FDREF1 F00B47^FS^FO100,1060^FDREF2 BL4H8^FS^CF0,190^FO485,965^FDCA^FS^XZ"

我正在寻找最有效的方法。我可以使用 http://labelary.com api 将 ZPL 转换为 JPG,但 API 经常出现故障,因此欢迎对图书馆提出任何建议。但是,我们不是在寻找库,而是在寻找将 ZPL 字符串转换为 PDF/JPG

的建议

请记住,我们使用的是 .NET Core

经过一番研究,似乎有两种方法可以做到这一点。

Restful 使用 labreary 调用 api:

byte[] zpl = Encoding.UTF8.GetBytes("^xa^cfa,50^fo100,100^fdHello World^fs^xz");

// adjust print density (8dpmm), label width (4 inches), label height (6 inches), and label index (0) as necessary
var request = (HttpWebRequest) WebRequest.Create("http://api.labelary.com/v1/printers/8dpmm/labels/4x6/0/");
request.Method = "POST";
request.Accept = "application/pdf"; // omit this line to get PNG images back
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = zpl.Length;

var requestStream = request.GetRequestStream();
requestStream.Write(zpl, 0, zpl.Length);
requestStream.Close();

try {
    var response = (HttpWebResponse) request.GetResponse();
    var responseStream = response.GetResponseStream();
    var fileStream = File.Create("label.pdf"); // change file name for PNG images
    responseStream.CopyTo(fileStream);
    responseStream.Close();
    fileStream.Close();
} catch (WebException e) {
    Console.WriteLine("Error: {0}", e.Status);
}

如果您不能依赖网络服务并且需要能够在不发送外部请求的情况下进行调用。

Can I use the Labelary engine locally, without relying on the public web service?

We do offer an offline version of the Labelary engine licensed for local use. Please contact us for licensing information. http://labelary.com/faq.html

这使我们能够为没有 Zebra 打印机的客户打印 PDF 和 PNG。

如果有人需要,ZPL 到 x++ 中的 pdf:-

using System.Net;
using System.Text;
class SPSConvertZPLtoPDF
{

/// <summary>
/// Runs the class with the specified arguments.
/// </summary>
/// <param name = "_args">The specified arguments.</param>
public static void main(Args _args)
{
    SPSShipmentPackage spsshipmentpackage;
    select firstonly spsshipmentpackage where spsshipmentpackage.ShipmentId == "S0000087";
    System.Byte[] zpl = Encoding::UTF8.GetBytes(spsshipmentpackage.ImageContent);

    // adjust print density (8dpmm), label width (4 inches), label height (6 inches), and label index (0) as necessary
    HttpWebRequest request = WebRequest::Create("http://api.labelary.com/v1/printers/8dpmm/labels/4x6/0/");
        request.Method = "POST";
    request.Accept = "application/pdf"; // omit this line to get PNG images back
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = zpl.Length;

    var requestStream = request.GetRequestStream();
    requestStream.Write(zpl, 0, zpl.Length);
    requestStream.Close();

    try
    {
        HttpWebResponse response = request.GetResponse();
        var responseStream = response.GetResponseStream();
        File::SendFileToUser(responseStream,"zpl.pdf");//Create("label.pdf"); // change file name for PNG images
        //responseStream.CopyTo(fileStream);
        responseStream.Close();
       // fileStream.Close();
    }
    catch 
    {
        Info("Error");
    }
   }

 }

你可以试试这个项目BinaryKits.Zpl。它仍然是将 zpl 代码转换为图像的 zpl 查看器的早期实现。

IPrinterStorage printerStorage = new PrinterStorage();

var analyzer = new ZplAnalyzer(printerStorage);
var elements = analyzer.Analyze("^XA^FO100,100^BY3^B3N,N,100,Y,N^FD123ABC^FS^XZ");

var drawer = new ZplElementDrawer(printerStorage);
var imageData = drawer.Draw(elements);