简体   繁体   English

使用Java打印到网络打印机

[英]printing to a networked printer using java

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. 我需要发送pdf文档以在Web应用程序的服务器端进行打印,打印机完全支持pdf打印等,它也可以联网到服务器。 The pdf is also stored on the server. pdf也存储在服务器上。

what i am trying to is on a button click, print out the pdf file, currently i have the code below : 我想要的是单击按钮,打印出pdf文件,目前我的代码如下:

//Server side printing
public class PrintDocument {

    public void printText(String text) throws PrintException, IOException {

        //Looks for all printers
        //PrintService[] printServices = PrinterJob.lookupPrintServices();

        PrintService service = PrintServiceLookup.lookupDefaultPrintService();
        InputStream is = new ByteArrayInputStream(text.getBytes("UTF8"));
        PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        pras.add(new Copies(1));

        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        Doc doc = new SimpleDoc(is, flavor, null);
        DocPrintJob job = service.createPrintJob();

        PrintJobWatcher pjw = new PrintJobWatcher(job);
        job.print(doc, pras);
        pjw.waitForDone();
        is.close();
    }
}

class PrintJobWatcher {

    boolean done = false;

    PrintJobWatcher(DocPrintJob job) {
        job.addPrintJobListener(new PrintJobAdapter() {
            public void printJobCanceled(PrintJobEvent pje) {
                allDone();
            }

            public void printJobCompleted(PrintJobEvent pje) {
                allDone();
            }

            public void printJobFailed(PrintJobEvent pje) {
                allDone();
            }

            public void printJobNoMoreEvents(PrintJobEvent pje) {
                allDone();
            }

            void allDone() {
                synchronized (PrintJobWatcher.this) {
                    done = true;
                    System.out.println("Printing has successfully completed, please collect your prints)");
                    PrintJobWatcher.this.notify();
                }
            }
        });
    }

    public synchronized void waitForDone() {
        try {
            while (!done) {
                wait();
            }
        } catch (InterruptedException e) {
        }
    }
}

But i have a few questions / issues, how do i put the pdf into the input stream for this to be printed out, can i select options such as duplex printing, and how can i call this from inside a JSF web app 但是我有几个问题/问题,如何将pdf放入要打印的输入流中,我可以选择诸如双面打印之类的选项,又如何从JSF Web应用程序内部调用它

Thanks 谢谢

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. 根据本文的介绍 ,应该可以使用PJL块开始打印作业(Wikipedia链接包括指向PJL参考文档的指针),然后是PDF数据。

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. 感谢PJL,您应该能够控制打印机必须提供的所有功能,包括双面打印等-博客文章甚至提到了装订2 pdf的组合打印输出。

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. 一定还要阅读文章上的评论,该专利的发明人列出了他的评论以及有关PJL命令的更多信息。

Take a look at this blog. 看看这个博客。 We had to print documents with duplex print option. 我们必须使用双面打印选项来打印文档。 Its not possible to duplex print directly in java. 无法直接在Java中进行双面打印。 However the work around is to use ghostscript and convert PDF to PS (Post script file). 但是,解决方法是使用ghostscript并将PDF转换为PS(后脚本文件)。 To that you can add either PJL Commands or Post script commands. 为此,您可以添加PJL命令或后脚本命令。

More info at 有关更多信息,请访问

http://reddymails.blogspot.com/2014/07/how-to-print-documents-using-java-how.html 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 使用javax.print库以属性(纸盘控制,双面打印等)进行打印

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. 阅读完此问答之后,我花了一段时间使用javax.print库,只是发现它与打印机选件支持不是很一致。 Ie even if a printer has an option like stapling, the javax.printer library showed it as "stapling not supported". 即,即使打印机具有类似装订的选项,javax.printer库也将其显示为“不支持装订”。

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: 因此,我然后使用普通的Java套接字试用了PJL命令,并且效果很好,在我的测试中,它的打印速度也比javax.print库快,它的代码占用空间小得多,而且最好的一点是根本不需要任何库:

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 . 有关尝试使用javax.print的更多信息,请参见此内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM