【问题标题】:Send PDF file to network printer in Java用 Java 将 PDF 文件发送到网络打印机
【发布时间】:2012-11-22 10:09:30
【问题描述】:

我正在尝试使用 javax.print 将 PDF/DOC/ODT 文件发送到连接到 LAN 中的打印机,但它甚至没有将作业发送到打印队列。我尝试“正常”打印文件(使用 Adob​​e Reader /Open Office)并且工作正常,因此打印机连接良好。我还尝试将其从代码发送到虚拟打印机(PDFCreator),并且成功了。

这是我正在使用的代码:

public Boolean Imprimir (String filePath){
    Boolean correct = true;

    FileInputStream psStream = null;  
    try {  
        psStream = new FileInputStream(filePath);  
    } catch (FileNotFoundException ex) {  
        ex.printStackTrace();
        correct = false;
    }
    if (psStream != null) {  

        DocFlavor psInFormat = DocFlavor.INPUT_STREAM.AUTOSENSE;  
        Doc myDoc = new SimpleDoc(psStream, psInFormat, null);    
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();            
        PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset);

        PrintService myPrinter = null;  
        for (int i = 0; i < services.length; i++){  
            String svcName = services[i].toString();             

            if (svcName.contains("Xerox")){  
                myPrinter = services[i];                       
                break;  
            }  
        }

        if (myPrinter != null) {              
            DocPrintJob job = myPrinter.createPrintJob();
            try{                    
                job.print(myDoc, aset);  
            }
            catch(PrintException ex){
                ex.printStackTrace();
                correct = false;
            }
        } else {  
            System.out.println("No printer services found");  
            correct = false;
        }
    }
    else{
        correct = false;
    }

    return correct;
}

打印机使用 LPR 协议连接。

提前致谢

编辑:我也尝试过使用 jLpr,正如其他帖子 (Java printing directly to a Postscript network printer) 中所建议的那样。它也不起作用,但没有错误消息,作业没有出现在打印机的队列中。

【问题讨论】:

  • 我发现问题可能是打印机使用LPR协议连接的。我尝试将打印作业发送到与 RAW 协议连接的打印机,它工作正常。我不回答这个问题,因为我仍然不知道如何处理 LPR 协议。
  • 您的问题解决了吗?如果有的话,您介意分享解决方案吗?谢谢

标签: java networking printing


【解决方案1】:

您使用什么 Adob​​e Reader 版本?可能是 Adob​​e Reader 安全问题,具体取决于其版本。

问候

【讨论】:

    猜你喜欢
    • 2017-11-24
    • 2013-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 2014-02-10
    相关资源
    最近更新 更多