Java printing PDF with options (staple, duplex, etc)

前端 未结 1 876
无人共我
无人共我 2021-01-16 16:06

I have a java program that prints PDFs. It uses Apache PDFBox to create a PDDocument object (from a pdf document or from a stream in some cases) and then sends

相关标签:
1条回答
  • 2021-01-16 17:10

    Reverting to Java socket printing makes PJL a thing:

    // this works, it also printed faster than javax.print when tested
    private static void print(File document, String printerIpAddress, boolean staple)
    {
        try (Socket socket = new Socket(printerIpAddress, 9100))
        {
            DataOutputStream out = new DataOutputStream(socket.getOutputStream());
            byte[] bytes = Files.readAllBytes(document.toPath());
    
            out.write(27); //esc
            out.write("%-12345X@PJL\n".getBytes());
            out.write("@PJL SET DUPLEX=ON\n".getBytes());
    
            if (staple) 
            {
                out.write("@PJL SET STAPLEOPTION=ONE\n".getBytes());
            }
            out.write("@PJL ENTER LANGUAGE=PDF\n".getBytes());
            out.write(bytes);
            out.write(27); //esc
            out.write("%-12345X".getBytes());
            out.flush();
            out.close();
        }
        catch (Exception e)
        {
            System.out.println(e);
        }
    }
    

    The needed PJL commands came from this Xerox datasheet.

    It should be noted that the same PJL commands worked for two different Xerox models and a Lexmark printer, that's all I had handy to test with. Dunno if other models will want something different.

    Do not need the Apache PDFBox library anymore. Or any external libraries at all.

    This might work for other types of documents, aside from PDFs.

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