Skip to content

Instantly share code, notes, and snippets.

@samuelsonbrito
Created May 2, 2017 19:46
Show Gist options
  • Save samuelsonbrito/6d6d36d391602d77779ddbe50093bc74 to your computer and use it in GitHub Desktop.
Save samuelsonbrito/6d6d36d391602d77779ddbe50093bc74 to your computer and use it in GitHub Desktop.
Impressão no Java
import java.io.ByteArrayInputStream;
import java.io.InputStream;
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;
/**
*
* @author Samuelson Brito
*/
public class Impressora {
public static void imprimir(String texto) {
DocFlavor df = DocFlavor.INPUT_STREAM.AUTOSENSE;
InputStream pis = new ByteArrayInputStream(texto.getBytes());
Doc d = new SimpleDoc(pis, df, null);
PrintService p = PrintServiceLookup.lookupDefaultPrintService();
if (p != null) {
DocPrintJob job = p.createPrintJob();
try {
job.print(d, null);
} catch (PrintException e) {
System.err.println("Erro impressora:"+e);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment