title: Base64 string to pdf in groovy
body: <p>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:</p>
  <ul>
  <li>Store the Base64 input in a string.</li>
  <li>Pass it through a Base64 Decoder(this is an inbuilt function)</li>
  <li>Use below code and pass the base64 string as an input to &quot;body&quot;:</li>
  </ul>
  <pre><code>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;
  }
  </code></pre>
  <p>what I get as an output looks something like below:</p>
  <pre class="lang-none prettyprint-override"><code>%PDF-1.7
  %????
  5 0 obj
  &lt;&lt;/Type/Font/Subtype/Type1/BaseFont/Helvetica/Encoding/WinAnsiEncoding&gt;&gt;
  endobj
  14 0 obj
  &lt;&lt;/Title(T....
  </code></pre>
  <p>and so on..</p>
  <p>Now when I save this output as a pdf file, it is either blank or it says &quot;There was an error opening this document. The file is damaged and could not be repaired.&quot;</p>
  <p>What am I doing wrong? Thanks in advance!</p>
  <p>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!</p>