Discussion
Corporation Service Company
US
Last activity: 27 Jan 2021 16:32 EST
Print PDF
I have a requirement to print PDF through a background job in the application. I am planning to set up a Job scheduler and invoke a activity. However this code only seems to connect to a printer but unable to print. any ideas?
java.net.Socket socket = null;
java.net.InetAddress addr = null;
java.net.InetSocketAddress socketAddress = null;
java.io.BufferedReader reader= null;
java.io.BufferedWriter writer= null;
java.io.InputStream input = null;
java.io.OutputStream output= null;
int port =9100;
int timeout=5000;
byte[] bodyBytes = null;
//byte[] modifiedBodyBytes = null;
//String bodyString = null;
try{
addr = java.net.InetAddress.getByName(hostName);
socket = new java.net.Socket(addr, port);
socket.setSoTimeout(timeout);
bodyBytes = new com.pega.pegarules.pub.util.Base64Util().decodeToByteArray(pyStream);
oLog.infoForced("TestPrinter bodyBytes length "+bodyBytes.length);
// BodyString = new String(bodyBytes, "US-ASCII");
// modifiedBodyBytes= BodyString.getBytes("US-ASCII");
output = socket.getOutputStream();
oLog.infoForced("TestPrinter output stream "+output);
input = socket.getInputStream();
oLog.infoForced("TestPrinter input stream "+input);
// kick it out
output.write(bodyBytes);
output.flush();
oLog.infoForced("TestPrinter output stream "+output);
socket.shutdownOutput();
reader = new java.io.BufferedReader(new java.io.InputStreamReader(input));
oLog.infoForced("TestPrinter read input stream "+reader);
printerResponse = reader.readLine();
oLog.infoForced("TestPrinter printer response "+printerResponse);
status = "success";
}
catch (Exception e) {
e.printStackTrace();
//oLog.error("TestPrinter Error in sending to printer ");
oLog.error("TestPrinter"+e);
status = "fail";
}
finally {
if (input != null) try { input.close();oLog.infoForced("TestPrinter closed input stream ");} catch (Exception e) {oLog.infoForced("TestPrinter Issue closing input stream "); }
if (socket != null) try { socket.close(); oLog.infoForced("TestPrinter closed socket connection ");} catch (Exception e) {oLog.infoForced("TestPrinter Issue closing socket "); }
if (output != null) try { output.close(); oLog.infoForced("TestPrinter closed output stream ");} catch (Exception e) {oLog.infoForced("TestPrinter Issue closing output stream ");}
}