Printer Availablity - java

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.

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.

How to Know the printer name (Not default printer name) using java?

I have added two printers (using Ricoh universal printer Driver) on same port number,it is working fine.I have written a java program to read the stream from the socket and write into postscript file .As I am running two printers on same port I am able to print the file through both the printers .But my question is, How can I know that from which printer the print/Stream is coming?
I know that we can get the default printer name as below
PrintServiceLookup.lookupDefaultPrintService().
import javax.print.*;
class Test {
public static void main (String [] args)
{
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
System.out.println("Number of print services: " + printServices.length);
for (PrintService printer : printServices)
System.out.println("Printer: " + printer.getName());
}
}
Use above code to get list of printers then check for state of every printer, if it is busy in job then your stream is coming from that printer
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
AttributeSet attributes = printService.getAttributes();
String printerState = attributes.get(PrinterState.class).toString();
String printerStateReason = attributes.get(PrinterStateReason.class).toString();
System.out.println("printerState = " + printerState);
if(printerState.equals(javax.print.attribute.standard.PrinterState.PROCESSING)){
//this is printer from where stream is coming
}

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.

Fedex ship label print to Thermal printer using 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.

Categories