-2

I am using Apache wicket 6.19.0, pdfbox 1.8.8, and Java 8. The problem I am facing is I get the print dialog on screen when I deploy my application on a Windows machine, but when deployed on the Linux server it doesn't show the print dialog on screen when invoked the print functionality from UI.

Code:

public static PrintService choosePrinter() {
    PrinterJob printJob = PrinterJob.getPrinterJob();
    if(printJob.printDialog()) {
        return printJob.getPrintService();          
    }else {
        return null;
    }
}

@Override
public File getObject() {
    File file = new File("document.pdf");
    file.deleteOnExit();
    PDDocument document = new PDDocument();
    //prepare the pdf here...
    PrinterJob job = PrinterJob.getPrinterJob();
    PrintService service = choosePrinter();
    if(service != null){
        job.setPrintService(service);
        document.silentPrint(job);
    }
    document.close();
    } catch (Exception e) {
        LOGGER.error("Exception: "+e);
    }
    return file;
}
Lachlan Goodhew-Cook
  • 1,101
  • 17
  • 31
avinash chavan
  • 729
  • 2
  • 11
  • 27

1 Answers1

1

PrinterJob is a class from AWT, i.e. a desktop feature. You cannot use it at the server. Apache Wicket is a web framework so I assume your users will reach the application thru a browser. In this case you have two options:

  1. render a good looking HTML and use JavaScript's window.print() to print it

  2. render a PDF and stream it to the browser so that it either:

    2.1. show it by using Content-Disposition: Inline response header (if the browser has PDF plugin)

    2.2. ask the user to save it, by using Content-Disposition: Attachment

I am not sure whether there is a way to print the PDF with JavaScript.

martin-g
  • 17,243
  • 2
  • 23
  • 35
  • so, you mean to say that the printDialog wont work with Linux? and will work with Windows? – avinash chavan May 07 '15 at 07:28
  • No. I'm saying that it will work on localhost but it won't work at the server. At localhost you have a desktop UI (aka 'X' in Linux) while usually at the server you don't have it. Even if PrinterJob works at the server (with running X and properly exported $DISPLAY env) it will show the dialog at the server, not at the user's machine. – martin-g May 07 '15 at 10:02