Imprimir desde Java

Iniciado por mapers, 12 Febrero 2015, 05:28 AM

0 Miembros y 1 Visitante están viendo este tema.

mapers

Buenas señores del foro necesito de su sabiduria; tengo una impresora epson tm h6000 qquisiera saber como poder mandar para que imprima los tickets.Si tuvieran un ejemplo de donde guiarme se lo agradeceria .

Usuario Invitado

Ejemplo:

Código (=java) [Seleccionar]
import java.io.*;
import javax.print.*;

public class PrintTest {
public static void main(String[] args) throws IOException {
  //we are going to print "printtest.txt" file which is inside working directory
  File file = new File("printtest.txt");
  InputStream is = new BufferedInputStream(new FileInputStream(file));
 
  //Discover the default print service. If you call PrintServiceLookup.lookupPrintServices
  //then it will return an array of print services available and you can choose a
  //printer from them
  PrintService service = PrintServiceLookup.lookupDefaultPrintService();
 
  //Doc flavor specifies the output format of the file (Mime type + Char encoding)
  //You can retrieve doc flavors supported by the printer, like this
  //DocFlavor [] supportedFlavors = service.getSupportedDocFlavors();
  DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_US_ASCII; 
 
 
  // Create the print job
  DocPrintJob job = service.createPrintJob();
  //Create the Doc. You can pass set of attributes(type of PrintRequestAttributeSet) as the
  //3rd parameter specifying the page setup, orientation, no. of copies, etc instead of null.
  Doc doc = new SimpleDoc(is, flavor, null);

  //Order to print, (can pass attributes instead of null)
  try {
   job.print(doc, null);
  } catch (PrintException e) {
   e.printStackTrace();
  } 

  //DocPrintJob.print() is not guaranteed to be synchronous. So it's better to wait on completion
  //of the print job before closing the stream. (See the link below)
  is.close();
  System.out.println("Printing done....");
}

}


Leer más: http://kalanir.blogspot.com/2010/10/how-to-print-from-java-jps-javaxprint.html
"La vida es muy peligrosa. No por las personas que hacen el mal, si no por las que se sientan a ver lo que pasa." Albert Einstein