changeme
changeme

Reputation: 640

Printing a byte array to thermal printer Java

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.

Upvotes: 2

Views: 4554

Answers (1)

changeme
changeme

Reputation: 640

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.

Upvotes: 1

Related Questions