How to send raw data to a printer using Java

I am trying to create a simple program that sends a string to a printer for printing. This is my program as follows:

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;

public class PrinterTest {
  public static void main (String [] args) throws PrintException {
    DocPrintJob job = null;
    PrintService[] printServices = 
    PrintServiceLookup.lookupPrintServices(null, null);
    System.out.println("Number of print services: " + printServices.length);
    for (PrintService printer : printServices) {
        System.out.println("Printer: " + printer.getName());
        if (printer.getName().contains("ZM400")) {
            String hello = "Hello";
            DocFlavor flavor = DocFlavor.STRING.TEXT_PLAIN;
            Doc doc = new SimpleDoc(hello, flavor, null);
            job = printer.createPrintJob();
            job.print(doc, null);
        }
    }
  }
}

I export this as a jar file and run it on the command line (Windows) using:

java -jar PrinterTest.jar

The program starts and runs through all installed printers on the computer. But when it hits the printer I'm looking for, I get the following error:

Exception in thread "main" sun.print.PrintJobFlavorException: invalid flavor
  at sun.print.Win32PrintJob.print(Unknown Source)
  at PrinterTest.main(PrinterTest.java:21)

Not quite sure what I'm doing wrong here, as the printer I'm looking for does appear.

-The following link is used for the link: http://docs.oracle.com/javase/7/docs/technotes/guides/jps/spec/jpsOverview.fm4.html

-Changing the change DocFlavor flavor = DocFlavor.STRING.TEXT_PLAINto DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE, but we get an error IllegalArgumentException: data is not of declared type.

- Doc doc = new SimpleDoc(hello, flavor, null) Doc doc = new SimpleDoc(hello, null, null), , .

- , , , , .

, ? , ?

() . , :

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;

public class PrinterTest {
  public static void main (String [] args) throws PrintException, IOException {
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter the name of the printer: ");
    String printerName = bufferedReader.readLine();
    System.out.print("Enter a short message of what you would like to print here: ");
    String printerMessage = "PRINTER MESSAGE: " + bufferedReader.readLine();
    boolean printerCheck = false;
    DocPrintJob job = null;
    PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
    System.out.println("Number of print services: " + printServices.length);
    for (PrintService printer : printServices) {
        System.out.println("Printer: " + printer.getName());
        if (printer.getName().contains(printerName)) {
            InputStream inputStream = new ByteArrayInputStream(printerMessage.getBytes());
            DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
            Doc doc = new SimpleDoc(inputStream, flavor, null);
            job = printer.createPrintJob();
            job.print(doc, null);
            printerCheck = true;
        }
    }
    if (printerCheck == false) {
        System.out.println("The printer you were searching for could not be found.");
    }
  }
}

, , DocFlavor.STRING.TEXT_PLAIN DocFlavor.INPUT_STREAM.AUTOSENSE.

, , - . .

+6
1

.

1) -, :

 DocFlavor.STRING.TEXT_PLAIN;

:

"main" sun.print.PrintJobFlavorException:

, , :

public static final STRING TEXT_PLAIN =
    new STRING ("text/plain; charset=utf-16");

, , , , .
, :

if (printer.getName().contains("ZM400")) {
    String hello = "Hello";
    DocFlavor flavor = DocFlavor.STRING.TEXT_PLAIN;
    Doc doc = new SimpleDoc(hello, flavor, null);
    job = printer.createPrintJob();
    job.print(doc, null);
}

:

if (printer.getName().contains("ZM400")) {
    Arrays.stream(printer.getSupportedDocFlavors()).forEach(f->System.out.println(f.getMediaType() + ":" + f.getMimeType() + ":" + f.getRepresentationClassName()));
    String hello = "Hello";
    DocFlavor flavor = DocFlavor.STRING.TEXT_PLAIN;
    Doc doc = new SimpleDoc(hello, flavor, null);
    job = printer.createPrintJob();
    job.print(doc, null);
}

:

image: image/gif: [B : image/gif: java.io.InputStream

image: image/gif: java.net.URL image: image/jpeg: [B

image: image/jpeg: java.io.InputStream image: image/jpeg: java.net.URL

image: image/png: [B : /png: java.io.InputStream

: /PNG: java.net.URL

: /-Java-JVM- ObjectRef: java.awt.print.Pageable

: /-Java-JVM- ObjectRef: java.awt.print.Printable

: /-: [

: /-: java.net.URL

: /-: java.io.InputStream

,    "text/plain; charset=utf-16".

2) DocFlavor.INPUT_STREAM.AUTOSENSE, - DocFlavor.
, , DocFlavor.INPUT_STREAM.AUTOSENSE .
, , DocFlavor.INPUT_STREAM.AUTOSENSE:

IllegalArgumentException: .

DocFlavor INPUT_STREAM.AUTOSENSE :

    public static final INPUT_STREAM AUTOSENSE =
        new INPUT_STREAM ("application/octet-stream");

:

: /-: java.io.InputStream

:

String hello = "Hello";     
...
Doc doc = new SimpleDoc(hello, flavor, null);

InputStream, a String.

, , InputStream :

String hello = "Hello";     
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc doc = new SimpleDoc(new ByteArrayInputStream(hello.getBytes()),
                         flavor, null);

:

: /-: [

InputStream, a String :

:

DocFlavor.BYTE_ARRAY.AUTOSENSE

:

String hello = "Hello";     
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
Doc doc = new SimpleDoc(hello.getBytes(), flavor, null);
+4

All Articles