I know itβs a little late to answer, but since I had the same problem, I think this can help others post my solution.
I ran into this problem on Windows (7), but not Linux (Fedora), so my first step was to check the driver settings.
Then I saw that PDF files are not proprietary, processed by many printers. This is accepted, but nothing is printed. Several solutions can be selected from this:
- Convert PDF to PS or something like that before sending it to the printer.
- Use a third-party library, such as Apache PdfBox (current version 2.0.2).
I chose solution 2 and it works like a charm. The best part is that it also uses a PrintService with attributes, so you can handle pages, trays for the printer, and many options.
Here is 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.
source share