You need to sign in to do that
Don't have an account?
Saran
Converting Blob to String
Hi All,
I have a problem in converting the body of pdf file to a string.
I tried something like this
Say for example the pdf file is stored in static resource :
My code looks like:
blob b = [select body from staticResource where name = 'testDoc'].body;
string s = b.tostring(); // here i am getting an error like "blob is not a valid UTF-8"
I found what is the casue but I am not able to overcome that.
The issue is that the body of pdf will have some special character like " ê , ç , ã ".
So that it is showing such error I think. Can anyone help me out !!
Thanks in advance.
I have a problem in converting the body of pdf file to a string.
I tried something like this
Say for example the pdf file is stored in static resource :
My code looks like:
blob b = [select body from staticResource where name = 'testDoc'].body;
string s = b.tostring(); // here i am getting an error like "blob is not a valid UTF-8"
I found what is the casue but I am not able to overcome that.
The issue is that the body of pdf will have some special character like " ê , ç , ã ".
So that it is showing such error I think. Can anyone help me out !!
Thanks in advance.
Again I cannot confirm on the data that is present in your file. But in order to get the text out of a blob, you can try various EncodingUtil methods. Here is what Salesforce suggests : https://help.salesforce.com/apex/HTViewSolution?urlname=How-do-you-convert-a-Blob-to-string-1327108626373&language=en_US (https://help.salesforce.com/apex/HTViewSolution?urlname=How-do-you-convert-a-Blob-to-string-1327108626373&language=en_US)
Again its not always the fact that you can convert a pdf file to String. Chances are very high that you might not get expected result due to encoding in the pdf creation itself and it might not be supported in Salesforce.
Thanks
Darshan
All Answers
The Blob variable b contains data in binary form, therefor you are getting UTF-8 error. So you might encode it before reading the data. Please use the following:
Thanks
But here the problem is even all the text is converted to binary data.
When i used the debug log to check the value i am getting something like this.
JVBERi0xLjQKJeLjz9MKMyAwIG9iaiA8PC9MZW5ndGggNTc5L0ZpbHRlci9GbGF0ZURlY29kZT4+c3RyZWFtCnicjZRNU6QwEIbv+RV91AsmQPjQU2TaIVNAkI/RmnUP7A7rjjUOCupW+es3gF+zpcUWB5ok3W/nqbe5J6cFoeD6JhRrggU5J/eEGtRyOfwhJiz05g1hFGLy7TuFNbEccL
I think everything is getting converted to binary form
Any other alternative way?????
Thanks
Again I cannot confirm on the data that is present in your file. But in order to get the text out of a blob, you can try various EncodingUtil methods. Here is what Salesforce suggests : https://help.salesforce.com/apex/HTViewSolution?urlname=How-do-you-convert-a-Blob-to-string-1327108626373&language=en_US (https://help.salesforce.com/apex/HTViewSolution?urlname=How-do-you-convert-a-Blob-to-string-1327108626373&language=en_US)
Again its not always the fact that you can convert a pdf file to String. Chances are very high that you might not get expected result due to encoding in the pdf creation itself and it might not be supported in Salesforce.
Thanks
Darshan
This thread is pretty old and also has best answer but link in best answer is dead, hence providing solution it may help someone.
Below code should work.
string s = b.tostring()
s = EncodingUtil.base64Decode(EncodingUtil.base64Encode(b)).toString();
Thanks,
Jayesh W.
just to help you with some actual working code:
To show/display/embed PDF on a salesforce page, from base64 string or from an external PDF url:
1) create a Visualforce page called HelloWorld: 2) create the controller DownloadPDF This will give you a salesforce page with an embedded PDF from a base64 source, showing a PDF with HelloWorld.
In case you copy and paste this code, make sure you fix the indentation (under Debug>>Fix indentation.