Java 直接打印到 Postscript 网络打印机

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/315209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 11:54:44  来源:igfitidea点击:

Java printing directly to a Postscript network printer

javanetworkingprintingpostscript

提问by David Jaquay

I've got Postscript code/data (?) in memory (in a Java Tomcat webapp) that I'd like to send directly to a networked PS printer. Is there an easy way (i.e. just popping open a port and sending the text) to print this, bypassing all of the O/S-specific drivers and stuff (and hopefully not even requiring extra jars)? A link to example code showing how to do this?

我在内存中(在 Java Tomcat webapp 中)有 Postscript 代码/数据(?),我想直接发送到联网的 PS 打印机。有没有一种简单的方法(即只是打开一个端口并发送文本)来打印这个,绕过所有 O/S 特定的驱动程序和东西(希望甚至不需要额外的 jars)?显示如何执行此操作的示例代码的链接?

Thanks, Dave

谢谢,戴夫

采纳答案by Tim Williscroft

open a TCP socket to the LPR port on the target printer.

打开一个 TCP 套接字到目标打印机上的 LPR 端口。

send your data; as long as the printer comprehends it, you're cool.

发送您的数据;只要打印机理解它,你就很酷。

don't forget a Line feed when you're done.

完成后不要忘记换行。

(then close the port.)

(然后关闭端口。)

回答by fdubs

You can send it directly to a network printer on port 9100. I wrote a blog post about this here:

您可以将它直接发送到端口 9100 上的网络打印机。我在这里写了一篇关于此的博客文章:

http://frank.zinepal.com/printing-directly-to-a-network-printer

http://frank.zinepal.com/printing-directly-to-a-network-printer

The problem is that most laser printers do not support PostScript. You usually have to get a printer add-on for it.

问题是大多数激光打印机不支持 PostScript。您通常必须为其安装打印机插件。

回答by VonC

I am not sure you can do it without extra library.

我不确定如果没有额外的图书馆你能做到这一点。

This exampleshows you how to send the file to a network printer, but requieres an adobe library (based on commercial J2EE Livecycle ESthough, so not a generic "free" solution...).

示例向您展示了如何将文件发送到网络打印机,但需要一个 adobe 库(虽然基于商业 J2EE Livecycle ES,因此不是通用的“免费”解决方案......)。

import com.adobe.livecycle.output.client.*;
import java.util.*;    
import java.io.File;    
import java.io.FileInputStream;    
import com.adobe.idp.Document;    
import com.adobe.idp.dsc.clientsdk.ServiceClientFactory;

public class SendToPrinter {

    public static void main(String[] args) {
        try{
            //Set LiveCycle ES service connection properties                            
            Properties ConnectionProps = new Properties();
            ConnectionProps.setProperty("DSC_DEFAULT_EJB_ENDPOINT", "jnp://localhost:1099");
            ConnectionProps.setProperty("DSC_TRANSPORT_PROTOCOL","EJB");          
            ConnectionProps.setProperty("DSC_SERVER_TYPE", "JBoss");
            ConnectionProps.setProperty("DSC_CREDENTIAL_USERNAME", "administrator");
            ConnectionProps.setProperty("DSC_CREDENTIAL_PASSWORD", "password");
            //Create a ServiceClientFactory object
            ServiceClientFactory myFactory = ServiceClientFactory.createInstance(ConnectionProps);
            //Create an OutputClient object
            OutputClient outClient = new OutputClient(myFactory); 
            //Reference XML data that represents form data
            FileInputStream fileInputStream = new FileInputStream("C:\Adobe\Loan_data.xml"); 
            Document inputXML = new Document(fileInputStream);
            //Set print run-time options
            PrintedOutputOptionsSpec printOptions = new PrintedOutputOptionsSpec(); 
            printOptions.setPrinterURI("\\Printer1\Printer");
            printOptions.setCopies(2);

            //Send a PostScript print stream to printer
            OutputResult outputDocument = outClient.generatePrintedOutput(
                    PrintFormat.PostScript,
                    "Loan.xdp",
                    "C:\Adobe",
                    "C:\Adobe",
                    printOptions,
                    inputXML); 

            //Write the results of the operation to OutputLog.xml
            Document resultData = outputDocument.getStatusDoc();
            File myFile = new File("C:\Adobe\OutputLog.xml");
            resultData.copyToFile(myFile);
        }
        catch (Exception ee)
        {
            ee.printStackTrace();
        }
    }
}

回答by nsayer

Check out java.awt.print. It is the generic printing API in java.

查看 java.awt.print。它是 java 中的通用打印 API。

Unfortunately, it's not oriented around dealing with postscript content you already have. It's designed to let you "draw" on a piece of paper with the java 2d graphics APIs.

不幸的是,它并不是针对处理您已有的附言内容。它旨在让您使用 java 2d 图形 API 在一张纸上“绘图”。