Printing a byte array to thermal printer Java

不打扰是莪最后的温柔 提交于 2019-12-10 22:24:42

问题


I am using java application to make web service call to FedEx and trying to print the label to a local USB connected thermal printer. I will get byte array from FedEx as response and want to print this to client machine where the thermal printer connected

DocAttributeSet das = new HashDocAttributeSet();
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();

PrintService ps = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob job = ps.createPrintJob();
Doc doc = new SimpleDoc(image, DocFlavor.BYTE_ARRAY.AUTOSENSE, das);    
job.print(doc, pras);

Your help is highly appreciated, or suggest me/redirect me to a link where I can find solution.

UPDATE: I want to Print the FedEx label onto a locally USB connected thermal printer from a web based application.

I have the server code written as above, I am facing issue to print the label locally.

The above code is looking for a printer connected on the application server.

When client is clicking on "Print Label" button, the FedEx webservice call is success and returning the ZPLII format byte array correctly but "want to push this byte array to client machine and print to the thermal Printer".

Only until getting the byte array back from FedEx is working after that nothing I could able to implement to bring the byte array back to printer to print on thermal printer.


回答1:


I used jZebra for printing this to a local USB connected thermal printer

Here is my code goes.

In Action class

byte[] imageArr = ShipmentReply.getImage();
out = ServletActionContext.getResponse().getOutputStream();
out.write(imageArr);
out.flush();

In jQuery

$.post("printFedexLabel", function(imageArr){
    printZebra(imageArr);
});

In JSP

<applet name="jZebra" code="jzebra.PrintApplet.class" archive="${pageContext.request.contextPath}/jzebra.jar" width="1" height="1">
     <param name="printer" value="zebra">
</applet>

<script type="text/javascript">
   function printZebra(data) {
       var applet = document.jZebra;
       if (applet != null) {
          applet.append(data);
          applet.print();
    }
   }
</script>

Thats it.... it worked awesome. Please let me know if someone need help in any further information on this implementation.



来源:https://stackoverflow.com/questions/11481390/printing-a-byte-array-to-thermal-printer-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!