【问题标题】:printing to a networked printer using java使用 java 打印到网络打印机
【发布时间】:2013-01-22 08:34:06
【问题描述】:

我需要发送一个 pdf 文档在 web 应用程序的服务器端打印,打印机完全支持 pdf 打印等,它也与服务器联网。 pdf 也存储在服务器上。

我想要的是点击按钮,打印出 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) {
        }
    }
}

但是我有几个问题/问题,我如何将 pdf 放入输入流中以便打印出来,我可以选择双面打印等选项,以及如何从 JSF Web 应用程序中调用它

谢谢

【问题讨论】:

  • 那是 HP 打印机吗?
  • 是的,它是 8600 pro plus

标签: java pdf web-applications printing


【解决方案1】:

根据this article,应该可以使用PJL 块(维基百科链接包括指向PJL 参考文档的指针)开始打印作业,然后是PDF 数据。

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

请务必同时阅读文章中的 cmets,该专利的发明人也发表了评论,并提供了有关 PJL 命令的额外信息。

【讨论】:

    【解决方案2】:

    看看这个博客。我们必须使用双面打印选项打印文档。 在java中直接双面打印是不可能的。然而,解决方法是使用 ghostscript 并将 PDF 转换为 PS(后脚本文件)。为此,您可以添加 PJL 命令或 Post 脚本命令。

    更多信息在

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

    也读过类似的问题

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

    【讨论】:

    【解决方案3】:

    阅读完此问答后,我花了一段时间使用 javax.print 库,却发现它与打印机选项支持不太一致。 IE。即使打印机具有装订等选项,javax.printer 库也会将其显示为“不支持装订”。

    然后我使用普通的 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);
        }
    }
    

    this for more info on attempts with javax.print

    【讨论】:

      猜你喜欢
      • 2011-04-12
      • 1970-01-01
      • 2017-04-21
      • 2014-02-10
      • 2023-03-13
      • 2012-05-28
      • 2011-04-13
      • 2011-06-24
      • 1970-01-01
      相关资源
      最近更新 更多