views:

329

answers:

0

I am having an issue printing to a specified printer in java.

I receive a message and it contains information that allows me to lookup the printer (from over 500). This works 99%+ of the time.. but from time to time, it just starts returning no printers.

Here is this code..

private void printOrder(OrderPrint orderPrint, String tempFileName) {
 DocFlavor flavor = javax.print.DocFlavor.INPUT_STREAM.PDF;
 PrintService psa[] = PrintServiceLookup.lookupPrintServices(flavor, null);
 log.debug("During lookup, I found " + psa.length + " print services available for this server that can print PDFs!");
 PrintService ps = null;
 for (int i = 0; i < psa.length; i++) {
  PrintService ref = psa[i];
  log.debug(ref.getName());
  if (ref.getName().trim().equalsIgnoreCase(orderPrint.getTty_id().trim())){
   ps = ref;
   log.debug("That is the one I will use");
   break;
  } 
 }

 if (ps != null){
  log.debug("Attempting to print to " + ps.getName() + " !");
  try {
   File pdfFile = new File((new StringBuilder(String.valueOf(System.getProperty("java.io.tmpdir")))).append("/")
     .append(tempFileName).append(".pdf").toString());
   DocPrintJob printerJob = ps.createPrintJob();
   javax.print.Doc simpleDoc = new SimpleDoc(pdfFile.toURI().toURL(), javax.print.DocFlavor.URL.AUTOSENSE, null);
   printerJob.print(simpleDoc, null);
  } catch (PrintException e) {
   sendEmail("PrintException while trying to print pdf", orderPrint, e);
  } catch (MalformedURLException e) {
   sendEmail("MalformedURLException while trying to print pdf", orderPrint, e);
  }

 }
}

Anyone got any ideas? Java 1.5 and RedHat..