PDF Java打印:已在打印机作业队列中发送作业,但未打印任何内容

PDF Java打印:已在打印机作业队列中发送作业,但未打印任何内容,java,pdf,printing,Java,Pdf,Printing,我正在尝试打印PDF文档。 我可以看到打印机队列中的作业,然后我看到它消失了,就像打印机完成了它的作业一样 但问题是没有任何东西在打印。 我无法找出我的代码中的错误 PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null,null); PrintService service = null; for (String imprimante : listImprimantes){ for( Prin

我正在尝试打印PDF文档。
我可以看到打印机队列中的作业,然后我看到它消失了,就像打印机完成了它的作业一样

但问题是没有任何东西在打印。 我无法找出我的代码中的错误

PrintService[] printServices = PrintServiceLookup.lookupPrintServices(null,null);
PrintService service = null;
for (String imprimante : listImprimantes){
    for( PrintService printService : printServices ) {
        Attribute[] attrs = printService.getAttributes().toArray();
        for (int j=0; j<attrs.length; j++) {
            String attrName = attrs[j].getName();
            String attrValue = attrs[j].toString();
            if (attrName.equals("printer-info")){
                if (attrValue.equals(imprimante)){
                    service = printService;
                    DocFlavor[] flavors = service.getSupportedDocFlavors();
                    break;
                }
            }
        }
    }
}
InputStream fi = new ByteArrayInputStream(baos.toByteArray());

DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
DocPrintJob printJob = service.createPrintJob();
Doc doc = new SimpleDoc(fi, flavor, null);
try {
    if (doc != null) {
        printJob.print(doc, null);
    }
} 
catch (PrintException e1) {
    log.debug(e1.getMessage());
}
PrintService[]printServices=PrintServiceLookup.lookupPrintServices(null,null);
PrintService=null;
for(字符串改进:listImprimantes){
用于(打印服务打印服务:打印服务){
Attribute[]attrs=printService.getAttributes().toArray();

对于(int j=0;j我知道现在回答有点晚,但因为我有同样的问题,我认为它可以帮助其他人发布我的解决方案

我在Windows(7)上遇到过这个问题,但在Linux(Fedora)上没有,所以我的第一个操作是检查驱动程序的设置

然后,我看到PDF不是由许多打印机本地处理的。它被接受,但没有打印任何内容。因此,可以选择几种解决方案:

  • 在将PDF发送到打印机之前,将其转换为PS或类似的格式
  • 使用第三方库,如(当前版本为2.0.2)
  • 我选择了解决方案2,它工作起来很有魅力。好处是它还使用带有属性的PrintService,因此您可以处理页面、打印机托盘和许多选项

    以下是我代码的一部分:

    private boolean print(PrintService printService, InputStream inputStream, PrintRequestAttributeSet attributes)
        throws PrintException {
    
        try {
            PDDocument pdf = PDDocument.load(inputStream);
            PrinterJob job = PrinterJob.getPrinterJob();
            job.setPrintService(printService);
            job.setPageable(new PDFPageable(pdf));
            job.print(attributes);
            pdf.close();
        } catch (PrinterException e) {
            logger.error("Error when printing PDF file using the printer {}", printService.getName(), e);
            throw new PrintException("Printer exception", e);
        } catch (IOException e) {
            logger.error("Error when loading PDF from input stream", e);
            throw new PrintException("Input exception", e);
        }
        return true;
    }
    

    希望这能有所帮助。

    你有没有想过这个问题?我也有同样的问题……没有。问题仍然存在。我面临着和你一样的情况(Windows 8)java 8,我尝试了使用apache PDF box的@teemoo answer,它可以工作,但它只适用于PDF格式。我面临着同样的问题,有解决方案吗?别忘了关闭你的PDDocument对象。请同时提及你正在使用的PDFBox版本。(希望是2.0.2)