10

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

David Jaquay
  • 1,032
  • 1
  • 8
  • 11

4 Answers4

8

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
  • 3,705
  • 24
  • 37
  • 3
    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 Sep 21 '09 at 08:30
  • If you really want to use LPR protocol on port 515, then the Tony's code above is needed and worked for me. However, for printers suporting raw port 9100, you really just open a socket and dump the data. Can be as simple as (on linux) nc [ip-address] 9100 – Franjo Markovic Feb 26 '20 at 21:46
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.

fdubs
  • 23
  • 2
0

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
  • 1,262,500
  • 529
  • 4,410
  • 5,250
0

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
  • 16,925
  • 3
  • 33
  • 51