Base64 string to pdf in groovy

2k views Asked by At

I am new to groovy and to this forum. I am using a middleware tool (SAP CPI) where I am getting a PDF in base64 string format. I need to send the PDF to another system. This is what I did:

  • Store the Base64 input in a string.
  • Pass it through a Base64 Decoder (this is an inbuilt function)
  • Use below code and pass the base64 string as an input to "body":
    import com.sap.gateway.ip.core.customdev.util.Message;
    import java.util.HashMap;
    def Message processData(Message message){
    def body = message.getBody(String.class);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream()
    outputStream.write(body.getBytes())
    message.setBody(outputStream.toString())
    return message;
    }
    

What I get as an output looks something like below:

%PDF-1.7
%????
5 0 obj
<</Type/Font/Subtype/Type1/BaseFont/Helvetica/Encoding/WinAnsiEncoding>>
endobj
14 0 obj
<</Title(T....

and so on...

Now when I save this output as a pdf file, it is either blank or it says

There was an error opening this document. The file is damaged and could not be repaired.

What am I doing wrong? Thanks in advance!

Note: Due to limitation of the middleware, I cannot create a whole project, but will rather have to use processData() function and can manipulate the string inside it. Thanks!

0

There are 0 answers