2017-08-01 122 views
0

給你一個奇怪的問題。最近將我的舊項目從java 7(jdk1.7.0_10)改爲java 8(1.8.0.91.x86_64)。在Java 7中,它打印了沒有問題的帖子腳本文件,現在它將純文本打印爲postscript文件而不是轉換文件。這是在redhat linux環境下。簡單地說,我試圖打印一個包含文件本身的後腳本文件的字符串。Java 8問題打印PS到網絡打印機

這裏是我的原代碼

DocFlavor flavor = DocFlavor.INPUT_STREAM.POSTSCRIPT; 
    PrintService pService = PrintServiceLookup.lookupDefaultPrintService(); 
// In a field environment, send to the printer 
    if (System.getenv("USER_DEFINED_RELTOP") == null || pfr.exists()) { 
     if (pService.getName().isEmpty()) { 
      LOGGER.error("No printer selected"); 
     } else { 
      LOGGER.info("Printing to " + pService.getName()); 
      DocPrintJob pj = pService.createPrintJob(); 
      try { 
       InputStream is = new ByteArrayInputStream(data.getBytes("UTF8")); 
       Doc doc = new SimpleDoc(is, flavor, null); 
       PrintJobWatcher pjw = new PrintJobWatcher(pj); 
       pj.print(doc, null); 
       pjw.waitForDone(); 
       is.close(); 

      } catch (PrintException | IOException e) { 
       LOGGER.error(e); 
      } // try block 

     } // no printer selected 
     // Otherwise, send to a file 
    } else { 

,在Java 7中工作得很好,我把它更新到這裏找到適合的Java 8 https://docs.oracle.com/javase/8/docs/api/javax/print/PrintService.html#createPrintJob--

https://docs.oracle.com/javase/8/docs/technotes/guides/jps/spec/printing.fm6.html

 DocFlavor psFlavor = DocFlavor.INPUT_STREAM.POSTSCRIPT; 
     PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet(); 
     attrs.add(MediaSizeName.ISO_A4); 


PrintService[] pservices = PrintServiceLookup.lookupPrintServices(psFlavor, 
                attrs); 
    File pfr = new File(PFR_INDICATOR); 

    // In a field environment, send to the printer 
    if (System.getenv("USER_DEFINED_RELTOP") == null || pfr.exists()) { 
     //Check we have a printer capable of post script 
    if (pservices.length > 0) { 
      LOGGER.info("Printing to " + pservices[0].getName()); 
      DocPrintJob pj = pservices[0].createPrintJob(); 
      try { 
        InputStream fis = new ByteArrayInputStream(data.getBytes("UTF8")); 

//byte[] ba =data.getBytes("UTF8"); 
     Doc doc = new SimpleDoc(fis, psFlavor, null); 
      LOGGER.info("Doc Flavor " + doc.getDocFlavor()); 
        PrintJobWatcher pjw = new PrintJobWatcher(pj); 
      LOGGER.info("PrintJob Attributes : " + pj.getAttributes()); 
     pj.print(doc, attrs); 
     pjw.waitForDone(); 
     fis.close(); 
      } catch (IOException e) { 
       LOGGER.error(e); 
       NotificationDialog.show(NotificationDialog.NOTICE_TYPE.ERROR, PRINT_ERROR); 
    } catch (PrintException e) { 
       LOGGER.error(e); 
      } 

     } else { // no printer selected 

甲骨文規範這給我一個錯誤java.awt.print.PrinterIOException:java.io.IOException:/ usr/bin/lpr:它看起來沒有找到lpr 。

如果我保留原來的樣子(不寫入文件),即使添加檢查以檢查打印機是否具有後腳本功能,它也會將純文本打印爲postscript。如果我使用新的打印文件的方式,我會得到一個lpr未找到的錯誤。如果我使用命令lpr打印PS文檔,它會按預期轉換並打印正常。如果我使用不格式化的lpr -l,它也會以純文本格式打印文檔。

任何建議/幫助將是偉大的。我迷失在做什麼。我真的不想將它轉換成圖像並打印出來。

回答

0

在猜測我會說你的打印機是惠普或至少PCL + PS打印機,而不是純PostScript打印機。

在這種情況下,您通常需要在語言選擇PJL字符串前添加PostScript。如果你不這樣做,那麼它通常默認爲PCL,並且如果你不發送任何PCL命令(全部以0x1B開頭),那麼一切都被視爲純ASCII文本。這可以解釋爲什麼你的應用程序和lpr -l最終都會寫文本,但是lpr本身不會(可能會增加PJL)。

你可以嘗試在前面加上PostScript文件的東西,如:

%[email protected] JOB 
@PJL ENTER LANGUAGE=POSTSCRIPT 

NB的第一個字節出現之前,應該%是一個0x1b ESC的性格,但我不能隨便貼二進制....

嘗試使用lpr -l發送文件,如果有效的話可以嘗試舊的打印方法。

+0

所以你的建議工作。我可以使用lpr -l打印格式良好的紙張。但是,當從java打印時,它仍然打印文本表示。我甚至嘗試將FileInputStream轉換爲ByteArrayInputStream,但得到了相同的文本表示。還有其他建議嗎? – jonnysteals

+0

對不起,Java不是我的領域。我只能假設它在開始時丟棄了額外的PJL,並沒有放入它自己的版本,但我不能給你任何線索爲什麼。我想這實際上只會成爲一個Java唯一的問題。 – KenS

+0

對不起肯,昨晚團隊錯誤地修改了我的代碼。我正確地打了補丁並正確打印。感謝您的幫助。 – jonnysteals