How to Know the printer name (Not default printer name) using java? - 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
}

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 doesn't use Paper Size Defined on printer settings on Windows

I have defined a custom paper size on windows printer settings that is is being ignored by the following java code:
public class Test {
final static String fileName = "C:\\test.txt";
public static void main(String[] args) throws FileNotFoundException, PrintException {
java.util.Hashtable printers = new java.util.Hashtable();
PrintService ps = PrintServiceLookup.lookupDefaultPrintService();
printers.put("##DefaultPrinter##", ps);
String printerToUse = "##DefaultPrinter##";
printDoc(fileName, ps);
}
private static void printDoc(String fileName, PrintService ps) throws FileNotFoundException, PrintException {
FileInputStream fInput = null;
DocFlavor[] docFlavor = ps.getSupportedDocFlavors();
DocFlavor flavor = null;
if (fileName.endsWith("txt")) {
flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_8;
if (!ps.isDocFlavorSupported(flavor)) {
File bomFile = new File(fileName);
}
}
if (flavor == null || !ps.isDocFlavorSupported(flavor)) {
flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
}
fInput = new FileInputStream(fileName);
DocPrintJob pj = ps.createPrintJob();
Doc doc = new SimpleDoc(fInput, flavor, null);
pj.print(doc, null);
}
}
When I print the content of "C:\test.txt" using this code the content is printed with default paper size A4 . When the same file is printed using notepad++ or notepad chosing the same printer that java gets, the content is printed obeying the custom paper size defined on printer.
The custom paper size has a height of 7,5 cm. The content is thirty lines each with word test, when printed on java the content is printed continuously one underneath the other not respecting page division, when is printed with notepad++ the content is splited is 3 pages respecting page definition on printer settings.
What changes I have to do in this java code to use custom paper size defined on printer ?

How to call an Epson TM-U220PD in Java to print USB port

I'm trying to print on a EPSON TM-U220PD in windows 7, I', using Java for do It.
I'm developing a software for print orders in a restaurant. I did the software in linux, when I connect the printer on linux, printer works excellent. But when I connect the printer on windows It's not work.
My drivers are good, I know because I can print test page, but when I'm going to the software, printer not works.
Printer is configurated in a port "USB001"
my code is here:
public void printLocalOrder(ArrayList<String> orderArray, int n) {
try {
FileWriter file = new FileWriter("USB001"); //Here is the problem
BufferedWriter buffer = new BufferedWriter(file);
PrintWriter ps = new PrintWriter(buffer);
ps.write(0x1B);
ps.write("M");
ps.write(1);
for (String orderArray1 : orderArray) {
ps.write(orderArray1);
}
ps.write(0x1B);
ps.write("d");
ps.write(4);
ps.close();
} catch (IOException e) {
System.out.println(e);
}
}
I have tried put the printer name so:
FileWriter file = new FileWriter("Ticketeadora"); //Name printer
But It's not works.
I hope you can help me. Thanks.
escpos-coffee is a Java library for ESC/POS printer commands. Can send text, images and barcodes to the printer. All commands are send to one OutputStream, then you can redirect to printer, file or network.
Code example:
PrintService printService = PrinterOutputStream.getPrintServiceByName("printerName");
PrinterOutputStream printerOutputStream = new PrinterOutputStream(printService);
EscPos escpos = new EscPos(printerOutputStream);
escpos.writeLF("Hello Wold");
escpos.feed(5);
escpos.cut(EscPos.CutMode.FULL);
escpos.close();

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.

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.

Categories