0

I have got some printers well installed on my computer (Windows 7) and well displayed on the control panel.

I would like to send some special commands like: cut command, or barcode printing commands (ESC/POS commands).

Is it possible to do that using Java Print API ? or does Java Print API perform only printing services ?

Thanks in advance.

Mssm
  • 717
  • 11
  • 29
  • The Java Print API relies on the system’s underlying drivers. If [lookupPrintServices](https://docs.oracle.com/javase/10/docs/api/javax/print/PrintServiceLookup.html#lookupPrintServices%28javax.print.DocFlavor,javax.print.attribute.AttributeSet%29) returns a non-empty array for any of the PLAIN_TEXT DocFlavors (for instance, [DocFlavor.STRING.TEXT_PLAIN](https://docs.oracle.com/javase/10/docs/api/javax/print/DocFlavor.STRING.html#TEXT_PLAIN)), you may be able to embed those control sequences in the plain text. – VGR Aug 20 '18 at 23:22
  • Ahh great, yes the lookupPrintServices returns an array of all my printers... any good tutorial for this purpose ? ... like which functions to use etc .. ? – Mssm Aug 21 '18 at 06:47
  • The [package documentation](https://docs.oracle.com/javase/10/docs/api/javax/print/package-summary.html) provides a good summary. There’s also [the JPS specification](https://docs.oracle.com/javase/8/docs/technotes/guides/jps/spec/JPSTOC.fm.html) and [a short tutorial](https://docs.oracle.com/javase/tutorial/2d/printing/services.html). – VGR Aug 21 '18 at 12:41

2 Answers2

0

Problem solved : Thanks to VGR.

here's a code to help anyone having same problem:

private PrintService printer = ...; // init this using PrintService.lookupPrintServices();

if(this.printer != null) {
        String commandToSend = "Some command\n";

        Doc myDoc = new SimpleDoc(commandToSend.getBytes(), DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
        DocPrintJob job = this.printer.createPrintJob();

        try {
            job.print(myDoc, null);
        } catch (PrintException e) {
            e.printStackTrace();
        }
    }
Mssm
  • 717
  • 11
  • 29
0

Not sure if you were able to solve it but here is an example

final byte[] VALIDATION_MODE = new byte[]{27, 'c', '0', 4}; // Print in validation mode
final byte[]  PAPER_FULL_CUT = {0x1d,0x56,0x00}; // Full cut paper
final byte[]  PAPER_PART_CUT = {0x1d,0x56,0x01}; // Partial cut paper

public void print(String receiptContent, String printerName) throws IOException {
        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        
        DocPrintJob docPrintJob = selectedPrinter(printerName).createPrintJob();
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        
        outputStream.write(VALIDATION_MODE);
        outputStream.write(receiptContent.getBytes());
        outputStream.close();
                        
        ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
        Doc doc = new SimpleDoc(inputStream, flavor, null);
        
        try {
            docPrintJob.print(doc, null);
        } catch (PrintException e) {
            System.out.println("Error:" + e.getMessage());
        }
        System.out.println("Print Job Finished");
    }


rderoldan1
  • 3,517
  • 26
  • 33