I have a kiosk application that runs under Windows. I would like to be able to print to a Windows printer connected via USB from a Java app. I can't use the JavaCOMM library as the printer is not serial or USB. I've read about the Java print API but it looks like it only prints images, I would like to be able to send raw text to the printer.
Asked
Active
Viewed 8,256 times
1 Answers
1
The Java Print API can do much more as just print images. Have a look at the SDK Printing tutorial, there is everything you need.
What about this, you just provide a char array with your chars:
char[] printdata = "hello world\n".toCharArray();
DocFlavor flavor = DocFlavor.CHAR_ARRAY.TEXT_PLAIN;
PrintService pservice = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob pjob = pservice.createPrintJob();
Doc doc= new SimpleDoc(printdata, flavor, null);
job.print(doc, null);

Nachtfrost
- 447
- 2
- 5
-
Thank you, I have a quick look but did not find a means to simply print a set of ASCII characters. For my application I need to print receipts, and the existing software gives me the receipt info as ASCII text which in the current implementation is sent straight to the printer on the parallel port via JavaCOMM. – fred basset Mar 07 '11 at 19:18
-
Sorry, I had to add the additional code to my first post, code in comments looks a bit scary. – Nachtfrost Mar 07 '11 at 19:30
-
Thanks for the help, I'll try that code snippet out. I wonder if it'll print on the printer in text mode or graphics mode? I'd really like it to print in text mode with the printer's native characters and fonts, as that's usually the fastest; rendering & printing graphics is a bit slower... – fred basset Mar 07 '11 at 19:48
-
I tried out the code, I'm getting an exception however "Exception in thread "main" sun.print.PrintJobFlavorException: invalid flavor". Any ideas? – fred basset Mar 07 '11 at 21:48
-
This means that your printer doesn't support text based representation. Have a look at this JavaWorld article [here](http://www.javaworld.com/javaworld/jw-07-2005/jw-0725-print.html?page=1), page 4 and 5 are interesting regarding the flavor issue. – Nachtfrost Mar 07 '11 at 22:18