Printing an HTML file in java

爷,独闯天下 提交于 2019-12-01 23:42:52
user1123530

I ended up not using java.awt.Desktop, it simply wouldn't work. Instead, I followed the instructions in this IBM tutorial, http://www.ibm.com/developerworks/java/library/j-mer0322/ . To be precise, the code I'm now using is the following (and it runs flawlessly on both Linux and Windows!):

PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
PrintService service = ServiceUI.printDialog(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(), 200, 200,
                      printService, defaultService, flavor, pras);
if (service != null) {
    DocPrintJob job = service.createPrintJob();
    FileInputStream fis = new FileInputStream(doc);
    DocAttributeSet das = new HashDocAttributeSet();
    Doc document = new SimpleDoc(fis, flavor, das);
    job.print(document, pras);
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!