简体   繁体   English

使用Java将PDF文件发送到网络打印机

[英]Send PDF file to network printer in Java

I'm trying to send a PDF/DOC/ODT file to a printer connected in LAN using javax.print, but it doesn't even send the job to the print queue. 我正在尝试将PDF / DOC / ODT文件发送到使用javax.print在LAN中连接的打印机,但是它甚至没有将作业发送到打印队列。 I've tried to print the file "normally" (using Adobe Reader /Open Office) and works perfectly, so the printer is well connected. 我尝试“正常”打印文件(使用Adobe Reader / Open Office),并且工作正常,因此打印机连接良好。 I've also tried to send it to a virtual printer (PDFCreator) from code and it worked. 我还尝试将其从代码发送到虚拟打印机(PDFCreator),并且可以正常工作。

Here is the code I'm using: 这是我正在使用的代码:

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;
}

The printer is connected using LPR protocol. 使用LPR协议连接打印机。

Thanks in advance 提前致谢

Edit: I've also tried using jLpr, as suggested in other posts ( Java printing directly to a Postscript network printer ). 编辑:我也尝试过使用jLpr,如其他帖子(将Java直接打印到Postscript网络打印机 )中所建议的那样。 It didn't work either, no error messages though, the job doesn't appear in the printer's queue. 它也不起作用,尽管没有错误消息,但该作业没有出现在打印机的队列中。

What Adobe Reader version do you use?. 您使用哪个Adobe Reader版本? Could be a Adobe Reader security issue, depending on its version. 取决于其版本,可能是Adobe Reader安全问题。

Regards 问候

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

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