SAP CPI can read the data received in multiple formats and convert to apt format as per the requirement. CPI provides built in message transformers to convert the data received in Json, XML, CSV, etc. However, CPI doesn’t have options in standard message transformers to convert binary format to PDF. This can be challenging for developers who need to convert binary data to PDF. Hence this blog is written to explain the custom method.
In this blog we will cover: "How to convert binary data to readable format using groovy script in SAP CPI".
- Many integration scenarios involve exchange of documents such as invoices, purchase orders and other business documents. In some cases, these documents may be received in binary format and need to be processed and converted to PDF format. Let’s consider a scenario where data is pushed out of an end system in binary format. Receiver system expects the data to be consumed in pdf format.
This can be achieved in CPI by following below steps.
- Sender System Configuration: HTTP adaptor can be used to configure and expose endpoint URL for CPI interface. This URL is passed down to sender system to push the data into CPI in binary format.
- Receiver System Configuration: The data is sent to receiver system in pdf format by placing the file in sftp folder. SFTP adaptor is used in CPI to place the file in destination folder.
- Groovy Script: Groovy script is a popular scripting language that can be used within SAP CPI environment to perform various tasks such as data transformations, message routing and manipulation.
In this example, we are using the groovy script to perform the binary to pdf conversion.
As explained earlier data is pushed to CPI in binary format. Below groovy script reads and transforms the data into string format.
Further, data which is in decoded format is written into PDF file using below script.
//Begin of importing Builtin Library
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import com.sap.gateway.ip.core.customdev.util.Message;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.PdfDocument;
//End of importing Builtin Library
def Message processData(Message message)
{
//Convert Binart Format to String Format
def binarydata = message.getBody(String) // Read the message body into a variable
byte[] decoded = binarydata.decodeBase64() // Decode the Binary Data
def text = new String(decoded) // Store the decoded data into String format
//Store String into a PDF File
OutputStream out = new ByteArrayOutputStream()
PdfWriter writer = new PdfWriter(out)
PdfDocument pdfDoc = new PdfDocument(writer)
Document document = new Document(pdfDoc)
document.add(new Paragraph(text))
document.close()
message.setHeader('Content-Type', 'application/pdf')
message.setBody(out.toByteArray())
return message
}
Summary
To summarize, this blog helps you to read the data saved in binary format from sender system and convert to readable format. Developers can implement the logic as per the project requirement to further process the converted data.