I am working on a project developing a kiosk web app, the project is a demo of kiosk devices, so i need to send to print a receipt when user clicks a button. I made a java jar that already print the receipt but when it is called from php exec('print.jar', $output) PrintServiceLookup.lookupDefaultPrintService() throws nullpointerexception. I have tried also with system() and shell_exec() and so on. I hope somebody can help cause I have looking for days.
This is the jar code public class PrintText {
public static void main(String[] args) throws PrintException, IOException {
String defaultPrinter = PrintServiceLookup.lookupDefaultPrintService().getName();
System.out.println("Default printer: " + defaultPrinter);
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
PrinterOptions p=new PrinterOptions();
p.resetAll();
p.initialize();
p.feedBack((byte)2);
p.color(0);
p.alignCenter();
p.setText("PANTALLAS LINCE");
p.newLine();
p.setText("MUPI INTERACTIVO");
p.newLine();
p.addLineSeperator();
p.newLine();
p.alignCenter();
p.setText("Tu Cuenta");
p.newLine();
p.addLineSeperator();
p.newLine();
p.alignLeft();
p.setText("Folio No \t: 2001 \tVenta directa \t: E511");
p.newLine();
p.setText("Fecha \t: " + "01/01/1801 22:59");
p.newLine();
p.setText("Metodo de Pago \t: Tarjeta");
p.newLine();
p.setText("Suc \t: Altabrisa Mérida");
p.newLine();
p.addLineSeperator();
p.newLine();
p.alignCenter();
p.setText(" - -Recibo de Compra - ");
p.newLine();
p.alignLeft();
p.addLineSeperator();
p.newLine();
p.setText("Id \tDesc\t\tPrecio\tQty");
p.newLine();
p.addLineSeperator();
p.newLine();
p.setText("1" + "\t" + "Aliens Everywhere" + "\t" + "100" + "\t" + "5"); p.newLine();
p.setText("1" + "\t" + "Aliens Everywhere" + "\t" + "100" + "\t" + "5"); p.newLine();
p.setText("1" + "\t" + "Aliens Everywhere" + "\t" + "100" + "\t" + "5"); p.newLine();
p.setText("1" + "\t" + "Aliens Everywhere" + "\t" + "100" + "\t" + "5"); p.newLine();
p.setText("1" + "\t" + "Aliens Everywhere" + "\t" + "100" + "\t" + "5"); p.newLine();
p.setText("1" + "\t" + "Aliens Everywhere" + "\t" + "100" + "\t" + "5"); p.newLine();
p.addLineSeperator();
p.feed((byte)3);
p.finit();
p.feed((byte)15);
// InputStream is = new ByteArrayInputStream(Header.getBytes("UTF8")); InputStream is = new ByteArrayInputStream(p.finalCommandSet().getBytes());
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
Doc doc = new SimpleDoc(is, flavor, null);
DocPrintJob job = service.createPrintJob();
PrintJobWatcher pjw = new PrintJobWatcher(job);
job.print(doc, pras);
pjw.waitForDone();
is.close();
}
}
this is php
$output = exec('print.jar 2>&1'); var_dump($output);
the problem is that PrintServiceLookup.lookupDefaultPrintService() throws null when run with php exec() but works ok if it is called from windows explorer double clicking