[英]PDF Java print : job sent in printer jobs queue but nothing prints
I am trying to print a PDF document.我正在尝试打印 PDF 文档。
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.我知道现在回答有点晚了,但由于我遇到了同样的问题,我认为它可以帮助其他人发布我的解决方案。
I have faced this problem on Windows (7), but not on Linux (Fedora), so my first action was to check the drivers setup.我在 Windows (7) 上遇到过这个问题,但在 Linux (Fedora) 上没有遇到过这个问题,所以我的第一个动作是检查驱动程序设置。
Then, I saw that PDFs are not native processed by many printers.然后,我看到许多打印机都没有原生处理 PDF。 It is accepted but nothing is printed.它被接受但没有打印任何内容。 From this, several solutions can be chosen:从中可以选择几种解决方案:
I chose solution 2 and it works like a charm.我选择了解决方案 2,它就像一个魅力。 The nice thing in this is that it also uses PrintService, with attributes, so you can deal with pages, printer trays and many options.这样做的好处是它还使用带有属性的 PrintService,因此您可以处理页面、打印机托盘和许多选项。
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;
}
Hope this helps.希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.