UDF to set base64 PDF string to PDF attachment in SAP PO message mapping

We’ve recently had a requirement to set base64 PDF to PDF attachment in message mapping. When looking online for an existing UDF to set PDF attachment in message mapping, most UDF’s found were using JAVA mapping or were created for other requirements (for example changing PDF filename when using sender file adapter). This UDF is what we’ve created and  used.

This UDF can be useful when:converting base64 to PDF attachment (for example when using IDOC extended with base64 to send PDF) but can also be used more broadly to set other attachment types or dynamically set attachment name (in this case, remove base64 encoder and/or change content type).

UDF

The UDF requires two input variables:

  • base64content: base64 input string to set PDF attachment
  • filename: PDF attachment file name

The function uses all values of a context execution type to be able to set multiple PDF attachments when multiple inputs are used.

Code

GlobalContainer globalContainer = container.getGlobalContainer(); for (int i = 0; i < base64content.length; i++) { try { OutputAttachments outputAttachments = globalContainer.getOutputAttachments(); byte[] b = Base64.getDecoder().decode(base64content[i]); //in case of non-base64 content, use byte[] b = base64content[i].getBytes(); Attachment newAttachment = outputAttachments.create(filename[i],"application/pdf", b); //in case of different attachment type, change "application/pdf" to different content type outputAttachments.setAttachment(newAttachment); } catch (Exception e) { throw new StreamTransformationException("Error: " + e.getMessage()); }
}

Example input structure

Input example/output result

Hopefully, this UDF will be a useful function when transforming base64 PDF to attachment or setting any other attachment in message mapping.