Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Modify PDF content fetched as a blob from SQL server  RSS feed

 
Amandeep Aggarwal
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am fetching a PDF file stored as a BLOB in SQL Server Database. Before streaming the PDF in the browser, I want to make a few changes to some of the URL (links) existing in the PDF.
I am using the following code:
pdfBlob is a Blob object. It contains the PDF image fetched from database.

try {
byte [] bytePDF = pdfBlob.getBytes(1,(int)pdfBlob.length());

String pdfImageAsStr = new String(bytePDF);
pdfImageAsStr = pdfImageAsStr.replaceAll("asp","jsp");
bytePDF= pdfImageAsStr.getBytes();

ByteArrayInputStream byteArrIS = new ByteArrayInputStream(bytePDF);
response.setContentType("application/pdf");
response.setContentLength(bytePDF.length);
while(byteArrIS.available() > 0){
try {
int b = byteArrIS.read();
response.getOutputStream().write(b);
} catch (IOException e1) {
e1.printStackTrace();
return "noPDF";
}
}

} catch (SQLException e) {
e.printStackTrace();
return "noPDF";
}


I want to replace the string "asp" with "jsp" inside the PDF. I get a blank PDF on my browser.


A similar conversion is possible in C#.net using simple byte To Str conversions:
string m_DataConvert = System.Text.Encoding.Default.GetString(m_Data);
m_DataConvert = m_DataConvert.Replace("bill_view_image.asp", "bill_view_image.jsp");
m_Data = System.Text.Encoding.Default.GetBytes(m_DataConvert);

Can this be done in java using the standard java APIs?
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"java pro", please check your private messages for an administrative message.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a read of ReadDoesntDoWhatYouThinkItDoes and AvailableDoesntDoWhatYouThinkItDoes. And please fix your screen name before your next post.
 
Paul Clapham
Sheriff
Posts: 22374
42
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And also BeForthrightWhenCrossPostingToOtherSites, as I see this same question has been running on the Sun forum for a couple of days.
 
Amandeep Aggarwal
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had a put the same post on java forums but didn't get solution.
I changed the way I was using read() method as per the article but the problem still remains.A blank PDF comes up.

Also if PDF is not changed, then it gets loaded correctly(with data). So I suspect the problem is either when bute array is being converted to String or when String is getting converted back to byte array. Do I need to put in some specific charset.

try {
byte [] bytePDF = pdfBlob.getBytes(1,(int)pdfBlob.length());
String pdfImageAsStr = null;
pdfImageAsStr = new String(bytePDF);
String pdfImageAsStr2 = pdfImageAsStr.replaceAll(".asp",".jsp");
byte[] bytePDF2 = null;
bytePDF2 = pdfImageAsStr2.getBytes();

ByteArrayInputStream byteArrIS = new ByteArrayInputStream(bytePDF2);
response.setContentType("application/pdf");
response.setContentLength(bytePDF2.length);
byte []buffer = new byte[1024];
int bytesRead;
try {
while((bytesRead = byteArrIS.read(buffer))!= -1){

response.getOutputStream().write(buffer,0,bytesRead);
}
} catch (IOException e1) {
e1.printStackTrace();
return "noPDF";
}


} catch (SQLException e) {
e.printStackTrace();
return "noPDF";
}
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do I need to put in some specific charset.

Most likely you do. You're not specifying any encoding, so the platform default encoding is used - which is bound to be the wrong one.

You're better off not converting the PDF to a String, but to perform the replacing on the byte array. Luckily, the characters in question are all part of ASCII, so you don't need to worry about encodings with them.
 
Amandeep Aggarwal
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for your reply. It worked when I did byte by byte comparison.
Also an alternative way would be to use a reversible charset like ISO8859_1.

the by default charset in Java (Cp1252) is not reversible i.e. if a byte array converted to String and then if the same strign is converted to a byte array, then the two byte arrays would not be the same. This is not the case in J#,C# and thus the default encoding was working in J# and C#.

thanks,
Amandeep
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!