PDF Java print : job sent in printer jobs queue but nothing prints

后端 未结 1 908
孤街浪徒
孤街浪徒 2021-01-14 18:06

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 prob

1条回答
  •  生来不讨喜
    2021-01-14 18:57

    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.

    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. 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).

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

    Hope this helps.

    0 讨论(0)
提交回复
热议问题