javawindowsjava-printwindows-driver

Java program to print raw data in Windows fails


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?


Solution

  • I found out something really interesting.

    The problem was linked to the HP driver i was using. My PC uses a driver called "HP Laser\Jet P4014/4015 PCL6 Class Driver".

    Repeating the test from an other Windows PC, i found out that the exact same code successfully printed. Exploring Printer configuration i found out that the second PC was using driver "HP Universal Printing PCL6".

    And there is more, considering that my code just raw prints data, i tried setting a generic driver like "Generic Text/Only" (that is unsuitable for PCL5 file i guess) and the print is succefull, on both PCs

    So, in conclusion, i think my original driver was probably something other than sending raw file data to the printer, something that broke the mechanism.

    Using other drivers, the file is correctly sent to printer in raw mode and everything works fine.