从pdf生成ZPL2代码(转换为png)

时间:2016-12-30 11:59:34

标签: java pdf zebra-printers zpl zpl-ii

正如您在标题中看到的,我在生成有效的zpl2代码时遇到问题。我有一台Zebra GC420D标签打印机。如果我使用我的函数生成代码,它可以在labelary.com上运行但是如果我将此代码发送到打印机没有任何反应(macos,添加打印机作为原始打印机),而在Windows上只有一个白色标签从打印机出来

首先是一段代码:

        DocPrintJob docPrintJob = prService.createPrintJob();
    File document = new File(printJobData.getDocumentFilePath());

    if (!document.exists())
    {
        throw new Exception("File not found!");
    }

    PDDocument pdDocument = PDDocument.load(document);
    PDFRenderer pdfRenderer = new PDFRenderer(pdDocument);

    for (int pageIndex = 0; pageIndex < pdDocument.getNumberOfPages(); pageIndex++)
    {
        BufferedImage bufferedImage = pdfRenderer.renderImageWithDPI(pageIndex, 203, ImageType.BINARY);
        //BufferedImage scaledImage = PlentyPrinterJavaHelper.scaleImage(bufferedImage, 456, 256);

        ZPLBuilder zplBuilder = new ZPLBuilder();
        zplBuilder.setBufferedImage(bufferedImage);

        String code = zplBuilder.build();

        Doc doc = new SimpleDoc(code.getBytes(), DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
        docPrintJob.print(doc, new HashPrintRequestAttributeSet());
    }

    pdDocument.close();

如您所见,我正在使用PDFBox(2.0.3)将我的pdf转换为单色png图像。

以下是我要打印的示例图片:

enter image description here

这是一个剪切生成zpl代码的地方:

    private int fieldOriginX = 0; // ^FOx,y
private int fieldOriginY = 0; // ^FOx,y

private int totalLength = 0;

private BufferedImage bufferedImage = null;

private static final String START_FORMAT = "^XA";
private static final String END_FORMAT = "^XZ";
private static final String FIELD_SEPARATOR = "^FS";
private static final String FIELD_ORIGIN = "^FO";
private static final String GRAPHIC_FIELD = "^GF";

public ZPLBuilder()
{
}

public ZPLBuilder(int fieldOriginX, int fieldOriginY, BufferedImage bufferedImage)
{
    this.fieldOriginX = fieldOriginX;
    this.fieldOriginY = fieldOriginY;
    this.bufferedImage = bufferedImage;
}

public String build() throws IOException
{
    if (bufferedImage == null)
    {
        throw new NullPointerException("Argument bufferedImage is null!");
    }

    String zplCommand = START_FORMAT + getFieldOrigin();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ImageIO.write(bufferedImage, "png", baos);

    byte[] bytes = baos.toByteArray();

    zplCommand += getGraphicField(bytes) + Hex.getString(bytes) + FIELD_SEPARATOR + END_FORMAT;

    return zplCommand;
}

private String getFieldOrigin()
{
    return FIELD_ORIGIN + fieldOriginX + "," + fieldOriginY;
}

private String getGraphicField(byte[] bytes)
{
    return GRAPHIC_FIELD + "A," + bytes.length + "," + bytes.length + "," + bytes.length / 8 + ",";
}

这是该功能的结果:

^XA^GFA,596,596,74,89504e470d0a1a0a0000000d49484452000001c80000010001000000001f61db9e0000021b4944415478daedda316fd3401407f004a166a99a81b5aad8bc4662304285fb04e43304557217862891ec96a4be4e2d0b2a1203432b3ab2b1584a0610718498a0f25cd12a8e8ae4a1124d6bf0b9e0dee312be807d02d1e1efed24ff74cff67b7f7b7089748f122424242424e4df969714f34cfa5c36eff26232a5819201cbf81d564c5efc9183943b83623299dcb2e665b02bddc2b2edd5bfca605fbaa2a08c1c6f7126bb9382d7199adee2cd2d2537b2827b9e99dee3e91d921b5450067dcf52d532b92e0a563b7af9c299977e47aea71cb30209f92fe4689f66732902ca7e955ec5a9ebe69dec69124444b1921db6d716cbddbc69f2feea6a2acf03aee4b123deade595cd5aadb1b250f9f1d9cdde2a49f79ee44ecdbeb96a254e7240d96ef9d020dbce9bd4dc64869588e421655fd8a7d7d2b672bf1dcc9a61cd7d37953c65a76fee870d9e4ffe147dc3b0a410cf283b61273b71783ba74c23d3306c7b266316ed0899f7795e4eab6d2f0871f04d758218850f5cccca35f81e3a979c3e6ac8948492c71af28262d911871a3299accc35864f75a4d36fd58f74aa8d9c5e6b35d391a1d9bbd1f475e459bfe7ba7b3a32f0cc565d4b8e9e3b5b8ffc239dee13da7dfb3f24320112328fac5669b3cbfca5b25a55364bcb6c4ce1a444dbdb1ffcb25caa5225adf86c38562757d3354ec3f11812121212121212121212121212121212121212121212121212121212121212b298c41f1c9090d753fe0682125d0a59f8246b0000000049454e44ae426082^XZ

这是labelary.com上的结果:labelary

正如你所看到它运作正常。但我的打印机没有任何反应。

This guy有同样的问题。但是没有解决方案。

我在网上找到了另一个可以正常工作的课程,但它生成了完全不同的代码:Link

如果没有~DY和~DG命令,有人知道这是如何工作的,只有我在上面发布的方式?是的,我必须转换pdf文件,我不能直接在zpl中生成标签。

编辑:

我在png文件中使用了字节数组,但它也不起作用。

示例代码:

        Path imagePath = Paths.get("/Users/deubel/Desktop/etikett_bw.png");

    DocPrintJob docPrintJob = printService.createPrintJob();

    try
    {
        BufferedImage image = ImageIO.read(imagePath.toFile());

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        baos.flush();

        ImageIO.write(image, "png", baos);

        baos.close();

        //byte[] binaryData = baos.toByteArray();
        //byte[] binaryData = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
        byte[] binaryData = Files.readAllBytes(imagePath);

        String command = "^XA^FO0,0^GFA," + binaryData.length + "," + binaryData.length + "," + binaryData.length / 8 + "," + Hex.getString(binaryData) + "^FS^XZ";

        Doc doc = new SimpleDoc(command.getBytes(), DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
        docPrintJob.print(doc, null);
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    catch (PrintException e)
    {
        e.printStackTrace();
    }

是否有可能打印png仅适用于~DG或~DD命令,并且直接打印图像我需要遍历所有像素并将它们转换为十六进制,就像在jcgonzalez上一样?

1 个答案:

答案 0 :(得分:1)

与此同时,我找到了一个解决方案,但只能使用~DGR命令。

这里剪了我的代码:

        // Get byte array of image content
    byte[] data = ((DataBufferByte) bufferedImage.getData().getDataBuffer()).getData();

    // Invert bytes because labels/images are inverted
    for (int i = 0; i < data.length; i++)
    {
        data[i] ^= 0xFF;
    }

然后构建ZPL代码:

~DGR:label,14592,57,{Hex-String of byte array}
^XA
^FO0,0
^XGR:label,1,1
^FS
^XZ

14592 =字节数组长度

57 = byte-array lenght / image-height

Hex-String - &gt;我在PDFBox中使用了Hex.getString(byte [] data)函数