Created
July 27, 2020 16:41
-
-
Save scorredoira/53a4a2bd82f1aa083747aa89a049da98 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://www.mets-blog.com/java-pos-thermal-printer-example/ | |
https://stackoverflow.com/questions/17505070/printing-reciepts-with-thermal-printer-in-java/39381849 | |
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. | |
PrinterService.java | |
import java.awt.Font; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
import java.awt.print.PageFormat; | |
import java.awt.print.Printable; | |
import java.awt.print.PrinterException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.print.Doc; | |
import javax.print.DocFlavor; | |
import javax.print.DocPrintJob; | |
import javax.print.PrintService; | |
import javax.print.PrintServiceLookup; | |
import javax.print.SimpleDoc; | |
import javax.print.attribute.HashPrintRequestAttributeSet; | |
import javax.print.attribute.PrintRequestAttributeSet; | |
public class PrinterService implements Printable { | |
public List<String> getPrinters(){ | |
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE; | |
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); | |
PrintService printServices[] = PrintServiceLookup.lookupPrintServices( | |
flavor, pras); | |
List<String> printerList = new ArrayList<String>(); | |
for(PrintService printerService: printServices){ | |
printerList.add( printerService.getName()); | |
} | |
return printerList; | |
} | |
@Override | |
public int print(Graphics g, PageFormat pf, int page) | |
throws PrinterException { | |
if (page > 0) { /* We have only one page, and 'page' is zero-based */ | |
return NO_SUCH_PAGE; | |
} | |
/* | |
* User (0,0) is typically outside the imageable area, so we must | |
* translate by the X and Y values in the PageFormat to avoid clipping | |
*/ | |
Graphics2D g2d = (Graphics2D) g; | |
g2d.translate(pf.getImageableX(), pf.getImageableY()); | |
/* Now we perform our rendering */ | |
g.setFont(new Font("Roman", 0, 8)); | |
g.drawString("Hello world !", 0, 10); | |
return PAGE_EXISTS; | |
} | |
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(); | |
} | |
} | |
public void printBytes(String printerName, byte[] bytes) { | |
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 { | |
Doc doc = new SimpleDoc(bytes, flavor, null); | |
job.print(doc, null); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private PrintService findPrintService(String printerName, | |
PrintService[] services) { | |
for (PrintService service : services) { | |
if (service.getName().equalsIgnoreCase(printerName)) { | |
return service; | |
} | |
} | |
return null; | |
} | |
} | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | |
53 | |
54 | |
55 | |
56 | |
57 | |
58 | |
59 | |
60 | |
61 | |
62 | |
63 | |
64 | |
65 | |
66 | |
67 | |
68 | |
69 | |
70 | |
71 | |
72 | |
73 | |
74 | |
75 | |
76 | |
77 | |
78 | |
79 | |
80 | |
81 | |
82 | |
83 | |
84 | |
85 | |
86 | |
87 | |
88 | |
89 | |
90 | |
91 | |
92 | |
93 | |
94 | |
95 | |
96 | |
97 | |
98 | |
99 | |
100 | |
101 | |
102 | |
103 | |
104 | |
105 | |
106 | |
107 | |
108 | |
109 | |
110 | |
111 | |
112 | |
113 | |
114 | |
115 | |
116 | |
117 | |
118 | |
119 | |
120 | |
121 | |
122 | |
123 | |
import java.awt.Font; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
import java.awt.print.PageFormat; | |
import java.awt.print.Printable; | |
import java.awt.print.PrinterException; | |
import java.util.ArrayList; | |
import java.util.List; | |
import javax.print.Doc; | |
import javax.print.DocFlavor; | |
import javax.print.DocPrintJob; | |
import javax.print.PrintService; | |
import javax.print.PrintServiceLookup; | |
import javax.print.SimpleDoc; | |
import javax.print.attribute.HashPrintRequestAttributeSet; | |
import javax.print.attribute.PrintRequestAttributeSet; | |
public class PrinterService implements Printable { | |
public List<String> getPrinters(){ | |
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE; | |
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); | |
PrintService printServices[] = PrintServiceLookup.lookupPrintServices( | |
flavor, pras); | |
List<String> printerList = new ArrayList<String>(); | |
for(PrintService printerService: printServices){ | |
printerList.add( printerService.getName()); | |
} | |
return printerList; | |
} | |
@Override | |
public int print(Graphics g, PageFormat pf, int page) | |
throws PrinterException { | |
if (page > 0) { /* We have only one page, and 'page' is zero-based */ | |
return NO_SUCH_PAGE; | |
} | |
/* | |
* User (0,0) is typically outside the imageable area, so we must | |
* translate by the X and Y values in the PageFormat to avoid clipping | |
*/ | |
Graphics2D g2d = (Graphics2D) g; | |
g2d.translate(pf.getImageableX(), pf.getImageableY()); | |
/* Now we perform our rendering */ | |
g.setFont(new Font("Roman", 0, 8)); | |
g.drawString("Hello world !", 0, 10); | |
return PAGE_EXISTS; | |
} | |
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(); | |
} | |
} | |
public void printBytes(String printerName, byte[] bytes) { | |
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 { | |
Doc doc = new SimpleDoc(bytes, flavor, null); | |
job.print(doc, null); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
private PrintService findPrintService(String printerName, | |
PrintService[] services) { | |
for (PrintService service : services) { | |
if (service.getName().equalsIgnoreCase(printerName)) { | |
return service; | |
} | |
} | |
return null; | |
} | |
} | |
Main.java | |
public class Main { | |
public static void main(String[] args) { | |
PrinterService printerService = new PrinterService(); | |
System.out.println(printerService.getPrinters()); | |
//print some stuff | |
printerService.printString("EPSON-TM-T20II", "\n\n testing testing 1 2 3eeeee \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); | |
// cut that paper! | |
byte[] cutP = new byte[] { 0x1d, 'V', 1 }; | |
printerService.printBytes("EPSON-TM-T20II", cutP); | |
} | |
} | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
public class Main { | |
public static void main(String[] args) { | |
PrinterService printerService = new PrinterService(); | |
System.out.println(printerService.getPrinters()); | |
//print some stuff | |
printerService.printString("EPSON-TM-T20II", "\n\n testing testing 1 2 3eeeee \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); | |
// cut that paper! | |
byte[] cutP = new byte[] { 0x1d, 'V', 1 }; | |
printerService.printBytes("EPSON-TM-T20II", cutP); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment