I'm using the approach mentioned in this thread, which looked promising and the author of the answer also stated it works on both Windows and Linux for them.
The code is the following:
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();
}
Whenever, I want to print an HTML file the file is added to the queue of the printer and gets removed immediately (without any error/warning/etc.). This happens on both Windows 10 as well as 8.1 and the printer is an HP laser printer.
Does this code work for anyone? Did anything change in the API so the code is not working properly anymore? Are the problems perhaps related to the printer and/or OS?
I have also tried to print non-HTML files with this code and it does not print any of them as well (e.g., PDF).
Edit: I could do a workaround by using Desktop.getDesktop().print()
and set Internet Explorer as standard program to open .html files. It does not work when the user has Google Chrome or Firefox as their standard program for html-Files.