I writ java code for Thermal Printing in java i have tested it on local machine only using microsoft xps document writer and work perfectly ,but when i use Xprinter XP-F900 printer i get the next error
Printer is not accepting job on
full code:
public class printThisBill {
public static void printCard(final String bill) {
Printable contentToPrint = new Printable() {
#Override
public int print(Graphics graphics, PageFormat pageFormat, int page)
throws PrinterException {
if (page > 0) {
return NO_SUCH_PAGE;
}
pageFormat.setOrientation(PageFormat.LANDSCAPE);
Graphics2D g2d = (Graphics2D) graphics;
g2d.translate(pageFormat.getImageableX(),
pageFormat.getImageableY());
g2d.setPaint(Color.black);
g2d.setFont(new Font("Arial", Font.BOLD, 10));
int y = 15;
Font f = new Font(Font.SANS_SERIF, Font.PLAIN, 8);
graphics.setFont(f);
for (int i = 0; i < Tbill.length; i++) {
graphics.drawString(Tbill[i], 5, y);
y = y + 15;
}
return PAGE_EXISTS;
}
};
PrinterService ps = new PrinterService();
PrintService pss = null;
PrinterJob job = null;
// get the printer service by printer name
// first test if printer defind by the use search on db
String query = "SELECT * FROM printer WHERE id_printer=1";
conn = DBconnect.connectDB();
if (db.TestIFex(query, conn)) {
Statement sqlState = null;
ResultSet rows = null;
try {
sqlState = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
rows = sqlState.executeQuery(query);
String printer_name = rows.getString(2);
pss = ps.getCheckPrintService(printer_name);
job = PrinterJob.getPrinterJob();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
sqlState.close();
rows.close();
} catch (Exception e) {
}
}
} else {
job = PrinterJob.getPrinterJob();
pss = job.getPrintService();
}
try {
job.setPrintService(pss);
} catch (PrinterException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
job.setPrintable(contentToPrint);
try {
job.print();
} catch (PrinterException e) {
System.err.println(e.getMessage());
}
}
}
My first guess is that the printer is not recognized by the computer. When i was working on thermal printing with Java I would have this problem only to realize that the printer wasn't connected to my computer or I was calling upon the incorrect printer in my code.
My second guess is that you need to use byte commands if you are attempting to print a Graphics2D object or an image. Thermal printers can be really tedious to use as there isn't really any standard and one will very likely have to look at the documentation to see how to print barcodes/cut/change font and so forth.
The name of the printer might be quite different from what you may think it is. So I Would recommend first testing to use System.out.println() to see what printer you get, i would also recommend setting the thermal printer you wish to use as the default one if you haven't done that.
DocPrintJob job = PrintServiceLookup.lookupDefaultPrintService().createPrintJob();
System.out.println(job + " <- printer");
Start by seeing what you get out of this. It may be quite different from what you think.
Here is a quick example to see if you can print a String with your printer with a method i made that should be universal to thermal printers.
public void PrintString(String s) throws Exception{
DocPrintJob job = PrintServiceLookup.lookupDefaultPrintService().createPrintJob();
//Get's the default printer so it must be set.
System.out.println(job + " <- printer");
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
byte[] b = s.getBytes("CP437");
//Get's the bytes from the String(So that characters such as å ä ö may be printed).
Doc doc = new SimpleDoc(b, flavor, null);
//Includes the content to print(b) and what kind of content it is (flavor, in this case a String turned into a byte array).
job.print(doc, null);
}
Simply call upon it with whatever String you wish to use as the argument and see what the result is. Again you need to set the thermal printer you wish to use as your default(You could do this in the control panel if you are using windows and i can't imagine that it is very difficult do to on other operating systems).
If you are able to print the String but unable to print the graphics option. Then you need to go here and download the "Programmer manual latest version" or the manual that fits your needs. You may also want to go to the manufacturers website to read more. Developing with thermal printers is not easy at all and requires a whole lot of work if you want to print anything fancy.
Try printing the String though and say what results you got. You did not say weather or not you tested printing a simple String first before going over to advanced stuff such as Graphics2D.
In addition here are some other questions/answers on stackoverflow that may help you get the most out of your thermal printer.
(How to get the printer to print more swiftly)
How to improve speed with Receipt printer and ESC/POS commands in Java
(How to print a String that has been converted to a Graphics object)
Printing reciepts with thermal printer in java
Hope this gave you the help you needed..Sincerely..
//Orville Nordström
Related
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.
I have used tess4j but not getting correct result.
below is my code.
public static String crackImage(String filePath) {
BufferedImage img = null;
try {
img = ImageIO.read(new File(filePath));
} catch (IOException e) {
}
ITesseract instance = new Tesseract();
instance.setLanguage("eng");
// instance.setPageSegMode((3));
img= ImageHelper.convertImageToGrayscale(img);
instance.setDatapath("C:\\tessdata");
try {
String result = instance.doOCR(img);
return result;
} catch (TesseractException e) {
System.err.println(e.getMessage());
return "Error while reading image";
}
}
I attached sample image.
MY output is:
arm m manner: a; man
mfl/Vemmnh 1951 mm 8221 11m 3521|\|\|II\IIIIIIHIIIIIHIIIH
scum—WWW
%‘
Please suggest how can I get correct result
here is the best practice,
you need to do image processing prefer to use (OpenCV) before running that tess4j command.
https://github.com/tesseract-ocr/tesseract/wiki/ImproveQuality
or you can choose Google Ml KIT
https://firebase.google.com/docs/ml-kit/recognize-text
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();
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.
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.