給你一個奇怪的問題。最近將我的舊項目從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,它也會以純文本格式打印文檔。
任何建議/幫助將是偉大的。我迷失在做什麼。我真的不想將它轉換成圖像並打印出來。
所以你的建議工作。我可以使用lpr -l打印格式良好的紙張。但是,當從java打印時,它仍然打印文本表示。我甚至嘗試將FileInputStream轉換爲ByteArrayInputStream,但得到了相同的文本表示。還有其他建議嗎? – jonnysteals
對不起,Java不是我的領域。我只能假設它在開始時丟棄了額外的PJL,並沒有放入它自己的版本,但我不能給你任何線索爲什麼。我想這實際上只會成爲一個Java唯一的問題。 – KenS
對不起肯,昨晚團隊錯誤地修改了我的代碼。我正確地打了補丁並正確打印。感謝您的幫助。 – jonnysteals