Java Pos Thermal Printer Example

Here it is, usb pos thermal printer solution. It is purely in java. The only condition is that your printer is recognized by the OS as a printer 🙂
That means it will show up in the printers list and queue. Getting the latest drivers should do it.
And as a bonus it works in Windows, Mac, Linux(yes tried in all of them). You are welcome Internet.

Note: If anyone knows how to draw stuff and change fonts with pos commands will be great. Make small tutorial in java.


  • Sebastian

    Hi,

    how can I print HTML code to the page?
    Thank you.

  • Rz

    What is “CP473” on printString (variable byte)? Can I do for image? Image to byte i mean.. Please kindly help. thank you

    • metin

      That is as I have commented, to display umlaut characters correctly , characters like ö,ü,ä. Image should be possible but I haven’t tried it yet myself.

  • bigmac

    Hey what printer did you use for this demo?

    • metin

      It was epson TM-t20II, thermal printer

  • gt

    not understanding this stuff. somebody to help me out

    • metin

      Which part or what is it that you don’t undertsand?

  • Erick Hernandez V.

    could you tell me where did you get the code for cut paper?

    • metin

      I googled it, can’t remember which site it was. I also downloaded the manuals of the printer but I definitely got it from a site.

  • Md. Syeful Islam

    How can I print silently without showing print preview and any dialogue from browser using servlet JSP

    • metin

      You should do that with Javascript instead of Servlet/Jsp. Your server machine doesn’t know what printers available in the client machine.
      What you can do is write a silent printer in javascript then include this javavscript on the pages you are serving with jsp and servlets.

  • khalil

    how can I print formatted text some text bold, other big small …. printString() doesn’t allow that, thanks

  • khalil

    I have tested to print graphic job.setPrintable(new PrinterService()); but thermal printer doesn’t print well

    • metin

      printString doesn’t allow it but printBytes certainly allows it. there are byte commands to tell the printer to print in certain font. You will have to find and send those bytes with print bytes. Cut paper is an example, same way the printer understands that byte to cut the paper will understand to change the font. I don’t know what those bytes are, you will have that yourself on the net or the manuals of your printer.

      • ankit

        has anyone found a solution to this?

  • How do i change page orientation for an image and set size

  • RoboNut

    Thank you very much for the code…

  • Rahul Chhangani

    Thankyou for code

  • ecs

    can i print pdf in epson TM-t20II?

  • i am using your’s code when i am run this code that time show success but thermal printer not printing. I checked printer “status” that is show blank and all things are fine. So How can i resolve. Please help

    • metin

      Make sure your printer is connected properly, check the status in the printer queue if you are on windows.

  • Jimmy Best

    hello , its very helpful code , i just want to print a jPanel with your code , why it is possible ?
    thanks in advance

  • Zacharia

    Hello, How can I print Arabic text in Epson TM-T88V-i.?

  • Diego

    Hello, I can cut the paper with your code, but I can’t print a string. When I run print.printString, the printer start to print a lot of texts, but never my text. I need to turn off the printer to stop printing.

    • metin

      I suggest you check the string you want to print, or your printer settings.

      • Diego

        Thanks for your reply, now I can print the string correctly. But I don’t know why I can’t cut the paper after printing. I need to choose between printing or cutting. After I printing for the first time, I need to turn off and turn on the printer to can cut again. Do you have some ideia why this issue occur?

        • metin

          Use the example in this post, print your string then print the bytes for cutting paper, you call 2 methods printString then printBytes. You don’t have to choose do one after another.

          • Diego

            Yes, I’m using exactly your example without change a line. Your code is printing, but don’t cut. If I remove the printString line, your code cut the paper. But it cant’t do the two at the same time. I don’t know why.. Your code look good. Thanks again

          • Diego

            I solved my problem converting the string into byte. This way I send two print.printByte commands. Thanks for your attention and for your code Metin.

  • Marko

    Thank you very much for the code! As mentioned in one of your comments, I’m trying to print a barcode on the receipt.
    The printer is connected and working fine.
    How would i do that? The barcode can be exported to JPG, GIF or PNG if that helps.

    Thanks very much!

    • metin

      Hi Marko,
      I will take a look, since there are also other questions. Might take a while.

  • rizwan

    how can i print list of contents from database and pass it to the java class to print that details.

    • metin

      Get your db content to a String and print that String with PrinterService. The Main class is an example of this.

      • Subhajit

        Is it possiable to iterate any loop in this printer???

  • Nabeel Ahmed

    Hi metin.
    I’m working on pos printer. I test your code but it show the error

    the Printer is not accepting job.
    javax.print.PrintException: Printer is not accepting job.
    at sun.print.Win32PrintJob.print(Win32PrintJob.java:327)
    at com.company.PrinterService.printString(PrinterService.java:200)
    at com.company.Main.main(Main.java:26)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
    javax.print.PrintException: Printer is not accepting job.
    at sun.print.Win32PrintJob.print(Win32PrintJob.java:327)
    at com.company.PrinterService.printBytes(PrinterService.java:224)
    at com.company.Main.main(Main.java:31)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

    I’m using the Tysso PRP-250 Thermal Printer
    If you have any id about this error than help me

    • metin

      Hmm never had this. Seems like a state error for your printer, have you tried printing from windows directly, like from notepad or some pdf. Clear you printer queue or restart it. There seems to be useful info for “Printer is not accepting job.” on the net.

  • Bob

    Hi,

    Just use the same printer as you, the Epson TM20II and got a null pointer Exception on windows 10, that’s strange because windows recognized the printer as a printer…
    By the way, do you know if this code works with CUPS?

    Thanks alot

    • Bob

      Finally got the problem solved, printer has a slightly different name

      thanks for the code!

      • metin

        Glad you solved your problem. As it seems there are some solutions to cups already. Look for cups java.

  • Aashik

    I tested this code with my HP Deskjet printer , but i am getting this error

    [Send To OneNote 2013, Microsoft XPS Document Writer, Microsoft Print to PDF, HP DeskJet 1110 series, Fax]
    Exception in thread “main” java.lang.NullPointerException
    at main.PrinterService.printString(PrinterService.java:83)
    at main.Main.main(Main.java:25)

    • Aashik

      Is there any error here ?

      public void printString(String printerName, String text) {

      // find the printService of name printerName
      DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
      PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();

      PrintService printService[] = PrintServiceLookup.lookupPrintServices(
      flavor, pras);
      PrintService service = findPrintService(printerName, printService);

      DocPrintJob job = service.createPrintJob();

      try {

      byte[] bytes;

      // important for umlaut chars
      bytes = text.getBytes(“CP437”);

      Doc doc = new SimpleDoc(bytes, flavor, null );

      job.print(doc, null);

      } catch (Exception e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }

      }

      • metin

        It seems like you are trying to print to multiple printers. My guess is either printService or service is null, check the null value on line 83.

  • Sawlye

    OMG! I love you. I would totally send you “coffee is on me” tip!

  • PaK

    Thank you! The code is working fine, except n at printString that are not interpreted. I have to check using the POS code for line feed.

  • suresh

    I want to generate barcode and print with my printer Start BSC10. can you help me on the same

  • Sachin Pk

    how do I implement the same in jsp web app.

  • Dejan Vrbaški

    Hi there, I tried to add a method to print an image from an InputStream with the INPUT_STREAM.JPEG flavor, also to print the image with the implemented print Graphics method, and the image gets printed but the paper cutter gets activated after this in both cases. Is there any way to disable this auto cutting so that some other stuff can be printed after the image on the same paper slip? Thanks! Btw, this is a good working example, it helped me a lot

Recent Posts

Recent Comments

Categories