|
@@ -0,0 +1,102 @@
|
|
|
+package org.robert.printer.common;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Locale;
|
|
|
+import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
+
|
|
|
+import javax.print.DocFlavor;
|
|
|
+import javax.print.DocPrintJob;
|
|
|
+import javax.print.PrintException;
|
|
|
+import javax.print.SimpleDoc;
|
|
|
+import javax.print.attribute.HashPrintRequestAttributeSet;
|
|
|
+import javax.print.attribute.PrintRequestAttributeSet;
|
|
|
+import javax.print.attribute.standard.JobName;
|
|
|
+import javax.print.event.PrintJobEvent;
|
|
|
+import javax.print.event.PrintJobListener;
|
|
|
+
|
|
|
+public class PrinterProcessor {
|
|
|
+
|
|
|
+ @SuppressWarnings("unused")
|
|
|
+ private PrinterDataType dataType;
|
|
|
+
|
|
|
+ public PrinterProcessor() {
|
|
|
+ this.dataType = PrinterDataType.RAW;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ *
|
|
|
+ * @param printerName
|
|
|
+ * @throws PrintException
|
|
|
+ */
|
|
|
+ public void print(String printerName, String commands) throws PrintException {
|
|
|
+ if (commands == null || commands.length() == 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ SimpleDoc document = new SimpleDoc(commands.getBytes(), DocFlavor.BYTE_ARRAY.AUTOSENSE, null);
|
|
|
+
|
|
|
+ PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
|
|
|
+ attributes.add(new JobName("Printing raw", Locale.getDefault()));
|
|
|
+
|
|
|
+ DocPrintJob job = PrintersDiscovery.getPrintService(printerName).createPrintJob();
|
|
|
+
|
|
|
+ final AtomicBoolean finished = new AtomicBoolean(false);
|
|
|
+ job.addPrintJobListener(new PrintJobListener() {
|
|
|
+ @Override
|
|
|
+ public void printJobRequiresAttention(PrintJobEvent pje) {
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void printJobNoMoreEvents(PrintJobEvent pje) {
|
|
|
+ finished.set(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void printJobFailed(PrintJobEvent pje) {
|
|
|
+ finished.set(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void printJobCompleted(PrintJobEvent pje) {
|
|
|
+ finished.set(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void printJobCanceled(PrintJobEvent pje) {
|
|
|
+ finished.set(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void printDataTransferCompleted(PrintJobEvent pje) {
|
|
|
+ finished.set(true);
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ job.print(document, attributes);
|
|
|
+
|
|
|
+ while (!finished.get()) {
|
|
|
+ try {
|
|
|
+ Thread.sleep(200);
|
|
|
+ } catch (Exception e) {
|
|
|
+ // Ignorar excepcion
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @throws PrintException
|
|
|
+ *
|
|
|
+ */
|
|
|
+ public void print(String commands) throws PrintException {
|
|
|
+ this.print("", commands);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws IOException {
|
|
|
+ PrinterProcessor test = new PrinterProcessor();
|
|
|
+ try {
|
|
|
+ test.print("EPSON_TM-P2.01", "Hola mundo\nOtra linea");
|
|
|
+ } catch (PrintException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|