0

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?

Mirai
  • 31
  • 1
  • 4
  • 1
    can you show the entire stacktrace? – Stultuske Jan 20 '23 at 10:11
  • i have shown everything appering on the console – Mirai Jan 20 '23 at 10:23
  • that 's only a very small part of a stacktrace ... how did you get it to print only that? – Stultuske Jan 20 '23 at 10:28
  • i have edited the code including my catch clauses, but really, everything i get is the exception i have shared in the question – Mirai Jan 20 '23 at 10:34
  • It’s not surprising that the error doesn’t happen on Linux, since it is triggered in Win32PrintJob. The source is at https://github.com/frohoff/jdk8u-jdk/blob/master/src/windows/classes/sun/print/Win32PrintJob.java , with that message at line 478. Unfortunately there’s no clue as to the cause beyond that the native method `printRawData` failed. – racraman Jan 20 '23 at 10:36
  • That's exactly the point i reached, seeing the problem is in a native method of Win32PrintJob. But how can i success i printing what i must print? I can use some tools to print pdf e ps files, like Ghostscript but what about file written in specific languages like PCL or other printer specific language? – Mirai Jan 20 '23 at 10:39
  • I wonder if there might be something recorded in the Windows Event Viewer? https://www.digitalcitizen.life/how-start-event-viewer-windows-all-versions/ – racraman Jan 20 '23 at 10:56
  • I did not know this tool, however i checked using your link, the error is indeed registered: "Impossibile stampare il documento Stampa documento di proprietà di darmon sulla stampante hp4015. Provare di nuovo a stampare il documento o riavviare lo spooler di stampa. Tipo di dati: RAW. Dimensione del file di spooling in byte: 70. Numero di byte stampati: 70. Numero totale di pagine nel documento: 1. Numero di pagine stampate: 0. Computer client: \\BLPC156. Codice di errore Win32 restituito dal processore di stampa: 2152796175" sorry, language is Italian – Mirai Jan 20 '23 at 11:07
  • Fortunately Google Translate was up to the job :D. I then found that someone else had a chat with Microsoft about this exact error … so I hope it helps but I will have to leave it there - best of luck :) https://social.technet.microsoft.com/Forums/windowsserver/en-US/0e72ba1d-8c80-4e76-88a7-8d48335f78d5/windows-server-2012-printing-error?forum=winserverprint – racraman Jan 20 '23 at 11:25
  • 1
    Oh, of you have questions about installing the right driver, probably the best place to ask would be at StackOverflow’s sister site https://superuser.com/ – racraman Jan 20 '23 at 11:33
  • My program must be able to print PDF files and file containing printer specific languages. In the first case i am using PDFBox to print, for the second one i have to find some solution because the raw printing is not working. I don't know Ghostscript PDL, i am using Ghostscript through Ghost4J Java wrapper, i don't know if i can use Ghostscript PDL the same way – Mirai Jan 20 '23 at 13:17

1 Answers1

0

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.

Mirai
  • 31
  • 1
  • 4