我想使用Java应用程序将PDF文件打印到Zebra printer。我使用带有二维条码的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) {
}
}
}
}
答案 0 :(得分:0)
Desktop整合:
File file = ...
if (Desktop.isDesktopAvailable()) {
Desktop.getDesktop().print(file);
}
答案 1 :(得分:0)
我使用了Pramod代码但在此之前我必须传递我的打印机ID,这是打印机的名称。我从我的数据库中获取字节数组。
Get_Line
这不是答案,这段代码将调用Pramod方法,这是精确的解决方案。 感谢