web stats
Get PDF inside BLOB data from DB - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 02-12-2010, 02:50 AM
alan alan is offline
Mirth Newb
 
Join Date: Feb 2010
Location: Modena, Italy
Posts: 8
alan is on a distinguished road
Default Get PDF inside BLOB data from DB

Hi, I'm trying to read a blob data ("image" type from sql server database) which contains a pdf... the code is:

var dbConn = DatabaseConnectionFactory.createDatabaseConnection (Utils.DbDriver, Utils.DbName, Utils.DbUser, Utils.DbPass);

var query =
"SELECT PDF " +
"FROM GelabDevel.dbo.RFC_Events " +
"WHERE Id = '" + $('EventId').toString() + "'";

var result = dbConn.executeCachedQuery(query);
dbConn.close();

result.next();

try {
var pdf = result.getBlob('PDF');

var strOut = new java.lang.StringBuffer();
var aux = new java.lang.String();

var br = new java.io.BufferedReader(pdf.getCharacterStream());

while ((aux = br.readLine())!= null)
strOut.append(aux);

} catch (ex) {
channelMap.put('ex', ex.toString());
}

It gives me the exception "TypeError: Cannot find function getCharacterStream". What am I missing? I really need to get the stream of this data, since the pdf is encrypted and i need to xor from char to char before send him.

Help would be very appreciated, thanks
Reply With Quote
  #2  
Old 02-12-2010, 07:52 AM
alan alan is offline
Mirth Newb
 
Join Date: Feb 2010
Location: Modena, Italy
Posts: 8
alan is on a distinguished road
Default

I finally managed through this!

The problem is the incompatibility of data format. If you get a Blob object you have a binary data, which use an InputStreamReader instead of a BufferedReader; the BufferedReader works well with Clob object, a character data.

So, if you have to manage a sql "image" data, the code must be like:

var blob = result.getBlob([image field]);
var is = new java.io.InputStreamReader(blob.getBinaryStream());

if you have a sql "text" field instead, use a code like

var clob = result.getClob([text field]);
var br = new java.io.BufferedReader(clob.getCharacterStream());

my problem was solved with a combination of both codes:

try {
// get the pdf blob from sql
var pdf = result.getBlob('PDF');

// declare string and buffer objects
var strOut = new java.lang.StringBuffer();
var aux = new java.lang.String();

// get the binary input stream
var is = new java.io.InputStreamReader(pdf.getBinaryStream());

// convert the binary stream to a character stream
// There are better methods to do this!!
var br = new java.io.BufferedReader(is);

// read each line of the character stream
while ((aux = br.readLine())!= null) {
// appendeach result to a regular string
strOut.append(aux);
}

// put the string on a channelMap
channelMap.put('ok', strOut);

} catch(ex) {
// ...unfortunately something goes wrong...
channelMap.put('ko', ex.toString());
}

hope it helps!
Reply With Quote
  #3  
Old 02-19-2010, 03:27 AM
alan alan is offline
Mirth Newb
 
Join Date: Feb 2010
Location: Modena, Italy
Posts: 8
alan is on a distinguished road
Default

I've improved the code a little; I had a problem reading some bytes, expecially the "return carriage" ones, and sometimes the read() method would return more than a byte. This was a problem for me, since I had to decrypt the pdf with a xor operation for each byte. What I needed was a method that would return me exactly one byte at a time. My code:

try {
// get the pdf blob from sql
var pdfEncrypted = result.getBlob('PDF');

// declare a string buffer object
var strOut = new java.lang.StringBuffer();

// CHANGED HERE! No more InputStreamReader
// get the binary stream
var isr = pdfEncrypted.getBinaryStream();

var decimalRead = 0;

// Read exactly a byte at a time!
while ((decimalRead = isr.read()) >= 0) {

// get the char corresponding to the byte readed
charOut = java.lang.Character(decimalRead);
// append the char to the string buffer
strOut.append(charOut);
}

// test: write the result on a file
FileUtil.write("[some path]/test.pdf", false, strOut);

} catch(ex) {
// get the exception
channelMap.put('blob', ex.toString());
}


Of course, you can do what you want with the code in the "while" statement; in my case I had to xor the decimal value readed with the byte position, just because the pdf was encrypted to ensure the respect of the privacy laws.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 05:27 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Mirth Corporation