I did the following code to read a PDF file from disk and send it to a print spool.
public boolean sendToSpool(File pdf, int copies) {
InputStream is;
try {
is = new FileInputStream(pdf);
} catch (FileNotFoundException e) {
return false;
}
try {
SimpleDoc doc = new SimpleDoc(is, DocFlavor.INPUT_STREAM.AUTOSENSE, null);
DocPrintJob printJob = printService.createPrintJob();
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(copies));
pras.add(MediaSizeName.ISO_A4);
pras.add(new RequestingUserName("print-service", Locale.US));
pras.add(new JobName("PRINT-SERVICE-" + pdf.getAbsolutePath(), Locale.US));
printJob.print(doc, pras);
} catch (PrintException e) {
return false;
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
This code works for the most of printers I have tested, but some doesn't. doPDF 8 is an example. It get stucked as shown in the image below.
If I open this document with Abobe Reader or Foxit Reader and request to print, a pop up is showed to me to save the new file on disk.
When I send it to PDF Creator it get printed too.
Has anyone seen something like this before?
UPDATE
I got it using a third part library (PDF Box).
@Override
public void print(File pdf, PrintService printService) {
PDDocument document = null;
try {
document = PDDocument.load(pdf);
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setJobName(pdf.getName());
printJob.setPrintService(printService);
printJob.setPageable(new PDPageable(document, printJob));
printJob.print();
} catch (PrinterException | IOException e) {
LOG.error(e.getMessage(), e);
} finally {
if (document != null ) {
try {
document.close();
} catch (IOException e) {
LOG.warn(e.getMessage(), e);
}
}
}
}