We are trying to implement a silent print for PDF that is generated using adobe. To accomplish this, we are using the following solution. Want to check with you to see if you anticipate any problems with this approach. Any help will be really appreciated. Thanks.
Issue - Trying to send the PDF to the default printer on the client machine without opening the document in Adobe 7.0.
The logic used is:
1. Search for /Type /Catalog in the byte stream coming to the web server from the app server.
2. Insert the following Acrobat Java Script code at this point into the stream to trigger the silent print without opening up the PDF.
Java script being inserted -
/*Check to validate if the script for silent print is inserted, so that the script is written only once*/
boolean isInserted = false;
/*Search for the string Type Catalog in the byte stream. */
String strSearchString = new String("/Type /Catalog");
byte[] byteArray = new byte[BYTE_SIZE];
FileInputStream fin = new FileInputStream(new File(file));
int byteLength = fin.read(byteArray, 0, byteArray.length);
/* Convert to string for searching */
String strPDF = new String(byteArray);
while( byteLength != -1 )
{
int searchInd = strPDF.indexOf(strSearchString);
if ((searchInd != -1) && !(isInserted))
{
isInserted = true;
isWritten = true;
StringBuffer strBufPDF = new StringBuffer(strPDF);
StringBuffer strBufReplace = new StringBuffer();
/*The script for silent print*/
strBufReplace = strBufReplace.append("\n/OpenAction <<");
strBufReplace = strBufReplace.append("\n/Type /Action");
strBufReplace = strBufReplace.append("\n/S /JavaScript");
strBufReplace = strBufReplace.append("\n/JS (this.print\\({bUI: false, bSilent: false, bShrinkToFit: true}\\);)");
strBufReplace = strBufReplace.append("\n>>\n");
strBufPDF.insert(searchInd+14,strBufReplace);
byte[] byteInsertArray = new byte[strBufPDF.toString().length()];
byteInsertArray = strBufPDF.toString().getBytes();
res.getOutputStream().write(byteInsertArray ,0 ,byteInsertArray.length);
}
else
{
res.getOutputStream().write(byteArray ,0 ,byteLength);
}
byteLength = fin.read(byteArray, 0, byteArray.length);
strPDF = new String(byteArray);
searchInd = strPDF.indexOf(strSearchString);
}
res.flushBuffer();// flush the buffer so that content type is sent right away.
fin.close();