as you can see in the title I have a problem with generating a valid zpl2 code. I have a Zebra GC420D label printer. If I generate a code with my function it works on labelary.com but if I send this code to the printer nothing happens (macos, added printer as raw printer) and on windows there is only a white label coming out of the printer.
At first a snippet of code:
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();
As you can see I am using PDFBox (2.0.3) for converting my pdfs to monochrome png images.
Here is an example image I want to print:
Here is a snipped where the zpl-code will be generated:
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 + ",";
}
This is the result of the function:
^XA^GFA,596,596,74,89504e470d0a1a0a0000000d49484452000001c80000010001000000001f61db9e0000021b4944415478daedda316fd3401407f004a166a99a81b5aad8bc4662304285fb04e43304557217862891ec96a4be4e2d0b2a1203432b3ab2b1584a0610718498a0f25cd12a8e8ae4a1124d6bf0b9e0dee312be807d02d1e1efed24ff74cff67b7f7b7089748f122424242424e4df969714f34cfa5c36eff26232a5819201cbf81d564c5efc9183943b83623299dcb2e665b02bddc2b2edd5bfca605fbaa2a08c1c6f7126bb9382d7199adee2cd2d2537b2827b9e99dee3e91d921b5450067dcf52d532b92e0a563b7af9c299977e47aea71cb30209f92fe4689f66732902ca7e955ec5a9ebe69dec69124444b1921db6d716cbddbc69f2feea6a2acf03aee4b123deade595cd5aadb1b250f9f1d9cdde2a49f79ee44ecdbeb96a254e7240d96ef9d020dbce9bd4dc64869588e421655fd8a7d7d2b672bf1dcc9a61cd7d37953c65a76fee870d9e4ffe147dc3b0a410cf283b61273b71783ba74c23d3306c7b266316ed0899f7795e4eab6d2f0871f04d758218850f5cccca35f81e3a979c3e6ac8948492c71af28262d911871a3299accc35864f75a4d36fd58f74aa8d9c5e6b35d391a1d9bbd1f475e459bfe7ba7b3a32f0cc565d4b8e9e3b5b8ffc239dee13da7dfb3f24320112328fac5669b3cbfca5b25a55364bcb6c4ce1a444dbdb1ffcb25caa5225adf86c38562757d3354ec3f11812121212121212121212121212121212121212121212121212121212121212b298c41f1c9090d753fe0682125d0a59f8246b0000000049454e44ae426082^XZ
and this the result on labelary.com: labelary
As you can see it works fine. But nothing happens on my printer.
This guy has the same problem. But there is no solution for it.
I found another class on web which works fine but it generates completely different code: Link
Does someone have an idea how this works without ~DY and ~DG command, only the way I posted above? And yes, I have to convert the pdf file, I can not generate the label in zpl directly.
Edit:
I played around with byte-array from png file, but it doesn't work,too.
Sample code:
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();
}
Is it possible, that printing a png only works with ~DG or ~DY commands and for directly printing an image I need to go through all pixels and convert them to hex like on jcgonzalez?
In the meantime I found a solution but only with the ~DGR command.
Here a snippet of my code:
Then build ZPL code:
14592 = byte-array length
57 = byte-array length / image-height
Hex-String -> I used
Hex.getString(byte[] data)
function from PDFBox