Automatic print is not working in Java

后端 未结 2 1067
深忆病人
深忆病人 2021-02-19 20:40

I have a requirement to print pdf files in two different ways -one through web page where user will see the print preview and choose the printer and print it. Second way is to a

相关标签:
2条回答
  • 2021-02-19 21:23

    Have you tried JPadel to print PDF files:

    Excerpt from Sample codes

    final PdfBook pdfBook = new PdfBook(pdfDecoder, printJob.getPrintService(), attributes);
    pdfBook.setChooseSourceByPdfPageSize(false);
    
    final SimpleDoc doc = new SimpleDoc(pdfBook, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
    
    // used to track print activity
    printJob.addPrintJobListener(new PDFPrintJobListener());
    
    try {
        printJob.print(doc, attributes);
    }
    catch (final Exception e) {
        LogWriter.writeLog("Exception " + e + " printing");
        // <end-demo>
    }
    

    In addition to this you can provide the printer name and add a listner PDFPrintJobListener as well.

    0 讨论(0)
  • 2021-02-19 21:33

    I've recently had the same task and the answer is not so straight forward for the second printing option (also tried JPedal, Samba, and other solutions..). The most simple way to print (which I ultimately tried) was to simply place the file in the printer queue, a.k.a the root location. Ex : MY_SERVER\PRINTER_NAME\

    The problem now becomes an environnement/O.S one, and not a Java one. Through an app installed on a Windows machine, you could possibly access that folder, copy the file you want printed, and voila. Furthermore, using the methods you use, you could also give the print job a name, number of copies, etc.

    However, once the app is installed on the server, it's a whole different ball game, especially if you are working with Linux servers.

    First of all you would have to translate the Windows adresses to Linux ones to even try to copy the file/print it.

    Second of all, and this is key, it is very rare/difficult for a printer to just "accept a file" for printing, if it is not part of a more complicated/proprietary Stream of data. For example you could find out the printers "communication language" with the server or even your machine when you do "Ctrl+P"..this will be mostly .xml files or some other format.

    But/and in order to "figure" out this format you would have to develop (ultimately in Java) an applet that would call that printer.

    You could of course, also try to install Cups4j on that server or have a printer server setup (assuming where you work this isn't in place), but this will cause problems when printers will change, be added to the network, etc.

    Ultimately either you would stick to the "Ctrl+P" approach, make a little JS script that calls "CTRL+P" in the browser or start re-creating the wheel, which is not a bad thing (since there are people who choose this approach also..but I haven't found an exemple anywhere), but would take you probably more time than you have.

    Hope I helped in some way (sorry for the long post..but it's a subject I've searched and worked on for a good period of time).

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