views:

3147

answers:

3

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

A: 

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

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.

nsayer
+1  A: 

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

Tim Williscroft
Actually there is a little more to the lpr/lpd protocol than just piping the data, I wrote a Java implementation some time ago at http://sourceforge.net/projects/jlpr/
Tony Edgecombe