i have been stuck with this problem for a long time.
I am trying to print a file in Windows sending its raw bytes to printer. Here is the code i am using
PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null, null);
PrintService defaultPrintService = PrintServiceLookup.lookupDefaultPrintService();
PrintService ps = ServiceUI.printDialog(null, 150, 150,
printServices, defaultPrintService, null, null);
if(ps!=null) System.out.println("selected printer:" +ps.getName());
else
System.exit(0);
try {
File test = new File(filename);
byte[] file_bytes = Files.readAllBytes(test.toPath());
DocFlavor flavor = DocFlavor.BYTE_ARRAY.AUTOSENSE;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
aset.add(new Copies(1));
Doc doc = new SimpleDoc(file_bytes,
flavor, null);
DocPrintJob job = ps.createPrintJob();
job.print(doc, aset);
} catch(IOException e) {
e.priprintStackTrace();
} catch (PrintException ex) {
ex.printStackTrace();
}
However, the print gets stuck in status "sent to printer" (checking from windows Printers view).
In addition, trying to print with a different printer, i get the following exception
javax.print.PrintException: Problem while spooling data at java.desktop/sun.print.Win32PrintJob.print(Win32PrintJob.java:460) at directportprintingtest.DirectPortPrintingTest.(DirectPortPrintingTest.java:91) at directportprintingtest.DirectPortPrintingTest.main(DirectPortPrintingTest.java:45)
I am having this problem only on Windows, when running same code from my Linux PC the print is succefull (with same printers!) .
The files i am trying to print are PDF, PS and file containing specific printer language like PCL5. All of them are printed correctly in Linux/MAC, failing on Windows.
I really can't undestand what i am missing, has anyone any clue about this problem and how i can solve it?