gpt4 book ai didi

java - 如何将字节数组数据打印到Zebra打印机?

转载 作者:太空宇宙 更新时间:2023-11-04 13:34:47 25 4
gpt4 key购买 nike

我想打印 PDF 文件到 Zebra printer使用 Java 应用程序。我已经使用 Jasper reports 生成了 PDF带有二维条形码。如果我在 Zebra 打印机上执行手动打印,它会打印该 PDF 文件,但是当我尝试使用 Java 应用程序打印相同的 pdf 文件时,作业会提交到打印机,但不会进行打印。

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.event.PrintJobAdapter;
import javax.print.event.PrintJobEvent;

public class ZebraPrintUtil {
public static void main(String[] args) throws IOException, InterruptedException {
byte[] content = convertFileToBytes("D://old files/test123.pdf");
print(content, "ZDesigner GX420t");
}

private static byte[] convertFileToBytes(String absoluteFilePath) throws IOException {
File file = new File(absoluteFilePath);
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];

try {
for (int readNum;
(readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum); // no doubt here is 0
}
} catch (IOException ex) {

} finally {
if (fis != null) {
fis.close();
}
}

byte[] bytes = bos.toByteArray();
return bytes;
}

private static boolean print(byte[] byteArray, String reqPrinterId)
throws IOException, InterruptedException {
boolean isPrintedSuccessfully = false;
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
PrintService[] services = PrintServiceLookup.lookupPrintServices(
flavor, null);

if (services.length > 0) {
PrintService myService = null;

for (PrintService service: services) {
if (service.getName().contains(reqPrinterId)) {
myService = service;
break;
}
}

DocPrintJob printJob = myService.createPrintJob();
JobCompleteMonitor monitor = new JobCompleteMonitor();
printJob.addPrintJobListener(monitor);
Doc document = new SimpleDoc(byteArray, flavor, null);

try {
printJob.print(document, null);
monitor.waitForJobCompletion();
System.out.println("-------------------- Print Completed " + monitor.completed);

isPrintedSuccessfully = true;
} catch (Exception e) {
e.printStackTrace();
}
}

return isPrintedSuccessfully;
}

private static class JobCompleteMonitor extends PrintJobAdapter {
private boolean completed = false;

@Override
public void printJobCanceled(PrintJobEvent pje) {
signalCompletion();
}

@Override
public void printJobCompleted(PrintJobEvent pje) {
signalCompletion();
}

@Override
public void printJobFailed(PrintJobEvent pje) {
signalCompletion();
}

@Override
public void printJobNoMoreEvents(PrintJobEvent pje) {
signalCompletion();
}

private void signalCompletion() {
synchronized(JobCompleteMonitor.this) {
completed = true;
JobCompleteMonitor.this.notify();
}
}

public synchronized void waitForJobCompletion() {
try {
while (!completed) {
wait();
}
} catch (InterruptedException e) {

}
}
}
}

最佳答案

对于Desktop集成:

File file = ...
if (Desktop.isDesktopAvailable()) {
Desktop.getDesktop().print(file);
}

关于java - 如何将字节数组数据打印到Zebra打印机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31807148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com