Reputation: 1072
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
Upvotes: 10
Views: 14117
Reputation: 23
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.
Upvotes: 2
Reputation: 3756
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.)
Upvotes: 8
Reputation: 17047
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.
Upvotes: 0
Reputation: 1328712
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();
}
}
}
Upvotes: 0