简体   繁体   English

Java 在 Windows 中打印原始数据的程序失败

[英]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.我正在尝试打印 Windows 中的文件,将其原始字节发送到打印机。 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).但是,打印卡在“已发送到打印机”状态(从 windows 打印机视图检查)。

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) 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.) .我只在 Windows 上遇到这个问题,当从我的 Linux PC 运行相同的代码时,打印成功(使用相同的打印机)。

The files i am trying to print are PDF, PS and file containing specific printer language like PCL5.我尝试打印的文件是 PDF、PS 和包含特定打印机语言(如 PCL5)的文件。 All of them are printed correctly in Linux/MAC, failing on Windows.所有这些都在 Linux/MAC 中正确打印,在 Windows 上失败。

I really can't undestand what i am missing, has anyone any clue about this problem and how i can solve it?我真的无法理解我所缺少的东西,有没有人对这个问题有任何线索以及我该如何解决它?

I found out something really interesting.我发现了一件非常有趣的事情。

The problem was linked to the HP driver i was using.问题与我使用的 HP 驱动程序有关。 My PC uses a driver called "HP Laser\Jet P4014/4015 PCL6 Class Driver".我的电脑使用名为“HP Laser\Jet P4014/4015 PCL6 Class 驱动程序”的驱动程序。

Repeating the test from an other Windows PC, i found out that the exact same code successfully printed.从另一台 Windows PC 重复测试,我发现完全相同的代码成功打印。 Exploring Printer configuration i found out that the second PC was using driver "HP Universal Printing PCL6".在探索打印机配置时,我发现第二台 PC 使用的是驱动程序“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还有更多,考虑到我的代码只是原始打印数据,我尝试设置一个通用驱动程序,如“Generic Text/Only”(我猜这不适合 PCL5 文件)并且打印成功,在两台 PC 上

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.使用其他驱动程序,文件以原始模式正确发送到打印机,一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM