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 August 14, 2016 at 6:51 am

Hi,

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

Rz September 9, 2016 at 7:00 am

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

    metin September 9, 2016 at 7:46 am

    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 September 30, 2016 at 12:21 pm

Hey what printer did you use for this demo?

    metin October 4, 2016 at 10:09 am

    It was epson TM-t20II, thermal printer

gt October 12, 2016 at 10:24 am

not understanding this stuff. somebody to help me out

    metin October 12, 2016 at 11:17 am

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

Erick Hernandez V. November 16, 2016 at 11:53 pm

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

    metin November 17, 2016 at 9:30 am

    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 November 22, 2016 at 9:22 am

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

    metin November 22, 2016 at 11:15 am

    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 November 22, 2016 at 4:00 pm

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

khalil November 22, 2016 at 4:47 pm

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

    metin November 23, 2016 at 9:01 am

    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 February 2, 2017 at 1:13 am

      has anyone found a solution to this?

robert November 30, 2016 at 11:33 am

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

RoboNut December 9, 2016 at 3:39 pm

Thank you very much for the code…

Rahul Chhangani December 10, 2016 at 3:11 am

Thankyou for code

ecs December 13, 2016 at 6:09 pm

can i print pdf in epson TM-t20II?

naveengour January 6, 2017 at 8:58 am

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 January 9, 2017 at 1:13 pm

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

Jimmy Best January 23, 2017 at 1:07 am

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

Zacharia February 14, 2017 at 10:51 am

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

Diego March 3, 2017 at 12:18 am

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 March 3, 2017 at 8:43 am

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

      Diego March 3, 2017 at 11:35 pm

      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 March 4, 2017 at 12:40 am

        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 March 4, 2017 at 2:25 pm

          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 March 4, 2017 at 3:16 pm

          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 March 7, 2017 at 3:42 pm

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 March 7, 2017 at 3:54 pm

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

rizwan April 22, 2017 at 9:32 am

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

    metin April 22, 2017 at 11:17 am

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

      Subhajit April 28, 2017 at 7:48 am

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

Nabeel Ahmed June 13, 2017 at 1:45 pm

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 June 21, 2017 at 8:10 am

    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 June 19, 2017 at 9:53 am

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 June 20, 2017 at 2:13 pm

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

    thanks for the code!

      metin June 21, 2017 at 8:23 am

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Recent Posts

Recent Comments

Categories