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

时间:2017-05-11 17:45:46

标签: c# asp.net-core zpl

下午好,

我目前正在开发一个动态生成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

3 个答案:

答案 0 :(得分:1)

您可以试试这个项目 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);

答案 1 :(得分:0)

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

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

如果您不能依赖Web服务,并且需要能够在不发送外部请求的情况下进行呼叫。

  

我可以在本地使用Labelary引擎,而无需依赖公共网络服务吗?

     

我们提供许可的Labelary引擎的离线版本   当地使用。请联系我们获取许可信息。   http://labelary.com/faq.html

这使我们可以为没有斑马打印机的客户打印PDF和PNG。

答案 2 :(得分:0)

如果有人需要,将 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");
    }
   }

 }