printing to a networked printer using java

前端 未结 3 378
有刺的猬
有刺的猬 2021-01-07 13:01

i require to send a pdf document to print on the server side of a web app, the printer fully supports pdf printing etc, it is networked as well to the server. The pdf is als

相关标签:
3条回答
  • 2021-01-07 13:13

    According to this article it should be possible to start a print job with a PJL block (Wikipedia link includes pointers to the PJL reference documentation), followed by the PDF data.

    Thank to PJL you should be able to control all features the printer has to offer including duplex, etc - the blog article even mentions stapling of a combined printout of 2 pdfs.

    Be sure to read the comments on the article as well, there is a comment from the guy who's listed as inventor on the patent as well with extra information on the PJL commands.

    0 讨论(0)
  • 2021-01-07 13:31

    After reading through this Q&A I spent awhile working with the javax.print library only to discover that it is not very consistent with printer option support. I.e. even if a printer has an option like stapling, the javax.printer library showed it as "stapling not supported".

    So I then tried out PJL commands using a plain java socket and it worked great, in my tests it also printed faster than the javax.print library, it has a much smaller code footprint and best part is no libraries are needed at all:

    private static void print(File document, String printerIpAddress)
    {
        try (Socket socket = new Socket(printerIpAddress, 9100))
        {
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            String title = document.getName();
            byte[] bytes = Files.readAllBytes(document.toPath());
    
            out.write(27);
            out.write("%-12345X@PJL\n".getBytes());
            out.write(("@PJL SET JOBNAME=" + title + "\n").getBytes());
            out.write("@PJL SET DUPLEX=ON\n".getBytes());
            out.write("@PJL SET STAPLEOPTION=ONE\n".getBytes());
            out.write("@PJL ENTER LANGUAGE=PDF\n".getBytes());
            out.write(bytes);
            out.write(27);
            out.write("%-12345X".getBytes());
            out.flush();
            out.close();
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
    

    See this for more info on attempts with javax.print.

    0 讨论(0)
  • 2021-01-07 13:36

    Take a look at this blog. We had to print documents with duplex print option. Its not possible to duplex print directly in java. However the work around is to use ghostscript and convert PDF to PS (Post script file). To that you can add either PJL Commands or Post script commands.

    More info at

    http://reddymails.blogspot.com/2014/07/how-to-print-documents-using-java-how.html

    Also read similar question

    Printing with Attributes(Tray Control, Duplex, etc...) using javax.print library

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