In this scenario I am converting all kinds of files(txt,xml,json etc) into the pdf file.
Step1: Create the Iflow with the desired name.
Step2. Connect the sender and integration process with the HTTP Sender Adapter and give the preferred address.
Step3. Now Upload the itextpdf JAR file which will be used in the groovy script in the iflow by going to references>archive.
Here is the link to download(https://jar-download.com/artifacts/com.itextpdf/itextpdf/5.5.13/source-code)
Step 4: Create one groovy script and write the below given script.
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.io.font.FontConstants;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import java.io.File;
def Message processData(Message message) {
def body = message.getBody(java.lang.String) as String;
OutputStream out = new ByteArrayOutputStream()
PdfWriter writer = new PdfWriter(out)
// Creating a PdfDocument object
PdfDocument pdfDoc = new PdfDocument(writer);
// Creating a Document object
Document doc = new Document(pdfDoc);
// Adding text to the document
Text text1 = new Text(body);
// Creating a paragraph 1
Paragraph para1 = new Paragraph(text1);
// Adding paragraphs to the document
doc.add(para1);
// Closing the document
doc.close();
message.setHeader('Content-Type', 'application/pdf')
message.setBody(out.toByteArray())
return message
}
Step5. After writing the groovy script, deploy the iflow and copy the endpoint URL from the SAP CPI Message Monitoring Section.
Step6. To test we can use the Postman Tool.
Here Create the New request with the Endpoint URL and in the Authorization, section use Basic Auth with username and password as Client ID and Client Secret from the BTP Cockpit.
Then in the body change type to binary and upload any kind of your file, after that Click on the send button.
After the completion of the request, we get the below screen, now click on the right side 3 dots, and save the response as file and see the pdf file in the system download location.