function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Conversion from Hex to Blob in apex class

I am working on SF - Jasper integration using webservice API in apex. Here, what I want to do is - get the Jasper report from webservice response and display the report on visualforce page.


I got the response for requested report from Jasper which includes an image and HTML part for that image. 


I tried to handle the response as String i.e.. HttpResponse.getBody(). But, I found some data loss of image. Hence, I converted response into Hex i.e. EncodingUtil.convertToHex(HttpResponse.getBodyAsBlob()). 


Response is multipart/related response. To get the image and HTML, I splitted it by boundary string and got the png image in Hex. For displaying this on visualforce, I need to convert it to Base64 so that, I could show it by embedding into Html i.e. <img src="data:image/png;base64,base64_string" alt=""/>. Here, I am not finding anything in Salesforce apex, to convert Hex to Blob so that, I could convert Blob to Base64.


Please, help me over this.


Can you have the response-part be base-64, hex, or URL encoded? If you can control the server's output, your life will be far easier. UTF-8 strings can't include binary data, which is what happens if you use raw binary and try to use getBody(), which will be a UTF-8 string. If you can't control its output, you'll have a rough time of it. In fact, I don't think you'll easily be able to extract binary data from the response at all, unless the server conveniently places the image on a triples boundary (e.g. the first byte of binary string is at Math.mod( index, 3 ) == 0). Try to make the server return a string-encoded binary value.


I can not get response as Base64 encoded.