Java - 打印HTML文件无法打印

时间:2017-07-20 11:52:23

标签: java

我正在使用this thread中提到的方法,该方法看起来很有前途,答案的作者也表示它适用于Windows和Linux。

代码如下:

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(file);
    DocAttributeSet das = new HashDocAttributeSet();
    Doc doc = new SimpleDoc(fis, flavor, das);
    job.print(doc, pras);
    fis.close();
}

每当我想要打印HTML文件时,文件就会被添加到打印机的队列中并立即被删除(没有任何错误/警告等)。这种情况发生在Windows 10和8.1以及打印机都是HP激光打印机上。

此代码适用于任何人吗? API中是否有任何更改,因此代码不再正常工作?问题可能与打印机和/或操作系统有关吗?

我还尝试使用此代码打印非HTML文件,但它也不会打印任何文件(例如,PDF)。

编辑:我可以使用Desktop.getDesktop().print()做一个解决方法,并将Internet Explorer设置为打开.html文件的标准程序。当用户使用Google Chrome或Firefox作为html-Files的标准程序时,它不起作用。

0 个答案:

没有答案