Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
Nischitha03
Explorer
 

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.



 

  1. 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.


 

  1. 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.


 

  1. 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.

 

 
3 Comments
sahithi_moparthi
Contributor
0 Kudos
Hello,

 

Thank you for sharing. I have tried the same scenario but getting error at the import statements:
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.PdfDocument

error message:

om.sap.it.rt.adapter.http.api.exception.HttpResponseException: An internal server error occured: startup failed:
Script3.groovy: 19: unable to resolve class PdfWriter
@ line 19, column 15.
PdfWriter writer = new PdfWriter(out)
^

Script3.groovy: 19: unable to resolve class PdfWriter
@ line 19, column 24.
PdfWriter writer = new PdfWriter(out)
^

Script3.groovy: 20: unable to resolve class PdfDocument
@ line 20, column 17.
PdfDocument pdfDoc = new PdfDocument(writer)
^

Script3.groovy: 20: unable to resolve class PdfDocument
@ line 20, column 26.
PdfDocument pdfDoc = new PdfDocument(writer)
^

Script3.groovy: 21: unable to resolve class Document
@ line 21, column 14.
Document document = new Document(pdfDoc)
^

Script3.groovy: 21: unable to resolve class Document
@ line 21, column 25.
Document document = new Document(pdfDoc)

 

please help.

 

 
Saurabh_Kabra
Participant
0 Kudos
Hi,

I hope it helps someone else who faces exactly the same error...

Since "com.itextpdf*" is an external library, hence you need to import its JAR along with dependent JARs to the iflow's resources section. The easiest way is to search over the internet and you will find it at https://jar-download.com/?search_box=com.itextpdf.kernel . Download this along with all dependent and import to the CPI iflow resource section.


 

Once imported then you won't get any error with the import statements.

 

Best

Saurabh
prathmesh009
Explorer
0 Kudos
Hello Nishchitha,

 

I am working on a scenario where i have PDF file converted in HEX format, I want to convert the HEX into Binary File in groovy and then password protect the same pdf in cpi.

I need groovy script to do this,

 

Thanks & Regards,

Prathmesh
Labels in this area