Java printing directly to a Postscript network printer

蓝咒 提交于 2019-12-18 12:28:56

问题


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?

Thanks, Dave


回答1:


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

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.)




回答2:


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

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.




回答3:


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

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

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



回答4:


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

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.



来源:https://stackoverflow.com/questions/315209/java-printing-directly-to-a-postscript-network-printer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!