Fedex ship label print to Thermal printer using Java - java

I am using Fedex ship web service to create a shipment. I am using a thermal printer to print the label (Java).
First I wanted to know what should be STOCKTYPE for printing to ZLPII printer, second question follows below.
When printing to the printer and empty label comes out but nothing print, when I use to print to PDF it works very well.
This is my Java code
PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.AUTOSENSE, null);
if (pss.length == 0)
System.out.println("FedExSmartPostServiceImpl::saveLabelToFile No printer services available.");
PrintService ps = null;
for (PrintService ps1 : pss) {
if (ps1.getName().indexOf("Zebra") >= 0) {
ps = ps1;
break;
}
}
System.out.println("FedExSmartPostServiceImpl::saveLabelToFile Printing to " + ps);
DocPrintJob job = ps.createPrintJob();
Doc doc = new SimpleDoc(fis, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
job.print(doc, null);
fis.close();
Thanks for the help in advance.

I could able to print the label with almost same code as above, with slight change of changing the SimpleDoc as below rather using the FileInputStream.
Doc doc = new SimpleDoc(byteArr, DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
Hope this helps.

Related

Java Printing with ZPL prints ZPL as text

I am trying to use a Zebra label printer using Java, and I'm having some issues:
I am trying the following code, which contains code taken from here: https://github.com/w3blogfr/zebra-zpl
public static void printZpl(String zpl, String printerName) throws ZebraPrintException {
try {
PrintService psZebra = null;
String sPrinterName = null;
PrintService[] services = PrintServiceLookup.lookupPrintServices(null, null);
for (int i = 0; i < services.length; i++) {
PrintServiceAttribute attr = services[i].getAttribute(PrinterName.class);
sPrinterName = ((PrinterName) attr).getValue();
if (sPrinterName.toLowerCase().indexOf(printerName.toLowerCase()) >= 0) {
psZebra = services[i];
break;
}
}
if (psZebra == null) {
throw new ZebraPrintNotFoundException("Zebra printer not found : " + printerName);
}
DocPrintJob job = psZebra.createPrintJob();
byte[] by = zpl.getBytes();
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
Doc doc = new SimpleDoc(by, flavor, null);
job.print(doc, null);
} catch (PrintException e) {
throw new ZebraPrintException("Cannot print label on this printer : " + printerName, e);
}
}
The string I am trying to print is this one:
^XA
^MMT
^FT0,110^FH\^FDTestString^FS
^XZ
The problem is that although I am able to connect to the printer and print something, the output is printed as plain text, it's as if the printer does not recognize that a ZDL command has been given.
The printer is a Zebra ZD220, and I have connected it to my Mac as a generic label printer, and selected the Zebra ZDL as the software to use it with.
Is there something I miss?
Note that I have been able to do this using NodeJS (so I know that this is working) using node-printer, so I know that it should work, but with Java, I cannot seem to make the printer understand that I am giving ZPL commands to it, and it prints plain characters.
I got this working by creating a socket connection with ZPL printer and writing ZPL content to output stream. Refer https://github.com/khpraful/ZPLPrint/blob/master/src/ZebraPrinter.java for more details. I tried with locally installed ZPL print emulator.

Java Printing - Spooling in Printing Queue

I want to print a pdf file to a printer using java-based software specifically Netbeans. When I ran my code below, I saw that the document is in printing queue with a file name "Java Document" with a Spooling status.
The questions are:
I want to know why my printer is not printing the document even if it is in queue?
Is it because of the code or is it because of my printer?
Here is my code:
public String gonnaGo(String newfile) throws FileNotFoundException, PrintException, IOException{
System.out.println("Hello!" + newfile);
InputStream is;
is = new BufferedInputStream(new FileInputStream(newfile)); //The new file will be the one to be printed
Doc doc = new SimpleDoc(in, DocFlavor.INPUT_STREAM.AUTOSENSE,null);
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
// Locate the default print service for this environment.
System.out.println("Default printer: " + PrintServiceLookup.lookupDefaultPrintService().getName());
//Check if the file directory of the file to be printed is included
PrintRequestAttributeSet params = new HashPrintRequestAttributeSet(); //ADD PRINTING PROPERTIES HERE
params.add(new Copies(1));
DocPrintJob printJob = service.createPrintJob(); //used to determine whether the print job is finished
printJob.addPrintJobListener(new JobCompleteMonitor());
service.createPrintJob().print(doc, params);
System.out.println("Exiting app");
is.close();
return null;
}
This is the output and it does not print anything. My printer is already connected. I use Canon ip2770 series, Epson Stylus TX117, and HP Deskjet 2510 series with the latest drivers.

Print ".pdf" file - PrinterJob

I'm having problems when trying to print a ".pdf". I printed Strings with this code without problems, but the pdf will not.
My program receives a byte[] with pdf and print it, I thought of recording a tempfile to print from InputStream, but also failed.
Follows the code I'm using to print:
    FileChannel fc = null;
    ByteBuffer bb = ByteBuffer.wrap(pdf);
    FileOutputStream fos = null;
    RandomAccessFile fis = null;
    try {
        File Tempfile = File.createTempFile("portfolios-temp", ".pdf");
        fos = new FileOutputStream(tempfile);
        fos.write(pdf);
        fos.close();
        FileInputStream psStream = new FileInputStream(tempfile);
        DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;
        MyDoc doc = new SimpleDoc(psStream, psInFormat, null);
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        aset.add (new Copies (1));
        aset.add (OrientationRequested.PORTRAIT);
        aset.add (Sides.ONE_SIDED);
        aset.add (MediaSizeName.ISO_A4);
        DocPrintJob job printService.createPrintJob =();
        try {
            job.print (myDoc, aset);
        } Catch (Exception pe) {
            pe.printStackTrace ();
        }
    } [...]
I get the Print Service this way:
PrintService printService = null;
for(PrintService printServiceCurrent : PrinterJob.lookupPrintServices()) {
if(printServiceCurrent.getName().equals(PRINTER_NAME)) {
printService = printServiceCurrent;
break;
}
}
He send a command to the printer, but comes like have no content. I checked the temporary file and it is being generated perfectly.
Any idea?
Thanks in advance.
First of all, instead of
PrintRequestAttributeSet aset HashPrintRequestAttributeSet = new();
it should be
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
Second you're not checking the printers that can print the format according to the instructions in the attribute set.
Check this link for more detailed information:
Java Print Service API

Printer Availablity

Here i have coded to get a list of devices and i will check each devices status
DocFlavor myFormat = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService[] services =PrintServiceLookup.lookupPrintServices(myFormat, aset);
System.out.println("The following printers are available");
for (int i=0;i<services.length;i++) {
PrintService printService = services[i];
PrintServiceAttributeSet printServiceAttributes = printService.getAttributes();
PrinterState printerState =
(PrinterState)printServiceAttributes.get(PrinterState.class);
if (printerState != null){
System.out.println(services[i].getName() + " is online");
} else {
System.out.println(services[i].getName() + " is offline");
}
}
But the problem is each and every time i got a status "Offline" even that printer is Switched on or Switched off
I recently had the same problem getting another attribute from a PrintService.
In fact, it always returns null because the method has never been implemented in the Java class, and that's the case for a lot of attributes.
If you really want to get these information, you will have to use the windows Print Spooler DLL or, if your printer is a net printer, query these informations via SNMP.

How to print PDF from java application

I have to print a PDF file using java application. I have tried such approach:
FileInputStream psStream = new FileInputStream("<path to file>");
PrintService service = getPrinterByName("some printer name");
if (service != null) {
DocPrintJob printJob = service.createPrintJob();
Doc document = new SimpleDoc(psStream, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
try {
printJob.print(document, null);
} catch (PrintException e) {
e.printStackTrace();
}
}
private PrintService[] getPrintersList() {
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService[] services = PrintServiceLookup.lookupPrintServices(flavor, null);
return services;
}
private PrintService getPrinterByName(String name) {
PrintService[] list = getPrintersList();
if (list.length > 0) {
for (PrintService service : list) {
if (service.getName().contains(name)) {
return service;
}
}
}
return null;
}
When I tested this on fake printer (I used PDFCreator as a printer) everything was OK, but when I tried to print on physical printer, nothing happend.
Then I used PDFBox. Document was printed, but with strange dots between words, in places where they should not be.
So, maybe someone have experience with printing PDF from java applications and can share this information?
Sending a PDF file directly to a printer will only work for printers that support the PDF format natively. This will be supported by any virtual PDF printer, but not by most of the hardware printers out-there. If you want to print a PDF file reliably, you need to use a library to render its content into the printer.
Take look then at this question in SO:
Which Java based PDF rendering library should I use for printing?
Update:
The link above is broken but there is no replacement for it other that doing a google search. Unfortunately stack-overflow owners decided that questions related to library recommendations are not welcome.

Categories