PDF Java打印:在打印机作业队列中发送的作业但没有打印 [英] PDF Java print : job sent in printer jobs queue but nothing prints

查看:181
本文介绍了PDF Java打印:在打印机作业队列中发送的作业但没有打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印PDF文档。

我可以看到打印机队列中的作业,然后我看到它消失了,就像打印机已完成其工作一样。

I am trying to print a PDF document.
I can see the job in the printer queue, and then I see it disappear, like if the printer had finished its job.

但问题是什么都没有打印。
我无法弄清楚我的代码有什么问题。

But the problem is that nothing is printing. I can't figure out what is wrong in my code.

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());
}

如果有人可以帮我这个......

If anyone can help me on this...

推荐答案

我知道回答有点迟,但由于我遇到了同样的问题,我认为它可以帮助其他人发布我的解决方案。

I know it is a bit late to answer but since I had the same problem I think it can help others to post my solution.

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

I have faced this problem on Windows (7), but not on Linux (Fedora), so my first action was to check the drivers setup.

然后,我看到许多打印机都没有对PDF进行本机处理。它被接受但没有打印。由此可以选择几种解决方案:

Then, I saw that PDFs are not native processed by many printers. It is accepted but nothing is printed. From this, several solutions can be chosen:


  1. 在将PDF发送到打印机之前将其转换为PS或类似的东西。

  2. 使用第三方库,例如 Apache PdfBox (当前版本为2.0。 2)。

  1. Transform the PDF to a PS or something like that before sending it to the printer.
  2. Use third-party lib, like Apache PdfBox (current version is 2.0.2).

我选择了解决方案2,它就像一个魅力。这里的好处是它还使用带有属性的PrintService,因此您可以处理页面,打印机托盘和许多选项。

I chose solution 2 and it works like a charm. The nice thing in this is that it also uses PrintService, with attributes, so you can deal with pages, printer trays and many options.

这是我的代码的一部分:

Here is a part of my code:

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;
}

希望这有帮助。

这篇关于PDF Java打印:在打印机作业队列中发送的作业但没有打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆