0

I am planing to use Eclipse and a simple swing gui to print raw data. My bar code printer uses .prn files to print and all is encoded in the file so if I can manage to send the exact contents of the file to my printer as raw data, I can make it print the exact format I want. I can select the file and use StringBuffer to read its contents and write to a string. Now how can I manage to send this string as raw data to my printer?

Regards.

edit:

Maybe i should elaborate my question a bit; now it works,in windows using;

    int ch;

            FileInputStream fin = null;
            try {
                fin = new FileInputStream(prnfile);
                while ((ch = fin.read()) != -1)
                    strContent.append((char) ch);
                fin.close();
                JOptionPane.showMessageDialog(frame, strContent);
            } catch (Exception e1) {
                System.out.println(e1);
            }



            try {

                PrintService service = PrintServiceLookup.lookupDefaultPrintService();
                JOptionPane.showMessageDialog(frame, service);




                DocPrintJob job = service.createPrintJob();
                InputStream is = new ByteArrayInputStream(strContent.toString().getBytes());




                DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;

                Doc doc = new SimpleDoc(is, flavor, null);
                job.print(doc, null);

            is.close();

But when i try to run this in Ubuntu,i get only "printing cancelled,java printing " notification,and the printer does nothing (normally my printer works in ubuntu using cat xxx.prn | lpr from the terminal.Any ideas?

Sin5k4
  • 1,556
  • 7
  • 33
  • 57
  • 4
    You probably shouldn't be using a `String`, and how you send it to your printer depends upon the printer and what kind of interfaces it provides. So: what kind of printer is it and what interfaces does it provide? What have you tried so far? – beerbajay Apr 10 '12 at 12:14
  • I managed to get it working actually, but only in Windows,but when i run it in Ubuntu,it just says "printing canceled" "java printing" ... – Sin5k4 Apr 10 '12 at 12:25
  • The printer usually needs to be configured for raw printing via CUPS or the GUI. http://stackoverflow.com/a/38965500/3196753. Some Windows drivers auto-sense the data flavor (Epson and Zebra do this) which may describe the discrepancy. – tresf Sep 20 '16 at 06:21

1 Answers1

0

First, crate the printer as a raw printer as commented.

Second, as commented, read your file to a byte array, not to String to avoid decoding bytes to unicode.

And third, do not use the defaultPrintService(), look for a specific service specifying we will send a byte array to the printer (raw data) to ensure the target printer supports raw data:

DocFlavor.byteFlavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
PrintServiceAttributeSet aset = new HashPrintServiceAttributeSet();
aset.add(new PrinterName(sPrinterName, null)); 
PrintService[] services = PrintServiceLookup.lookupPrintServices(byteFlavor, aset);

services[0] should be a service capable to send your raw data. This had worked for me.

jmdiazlr
  • 11
  • 2