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: 
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://sourceforge.net/projects/itext/)


 

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.

5 Comments
mastanvalim
Participant
0 Kudos
Hi Manoj,

when I download the jar I am getting only single jar where as I see you have 31 jars.

also i am getting error when i run the iflow

script1__Script.groovy: 4: unable to resolve class com.itextpdf.kernel.font.PdfFontFactory @ line 4, column 1. import com.itextpdf.kernel.font.PdfFontFactory;

Am i missing anything here?

 

Mastan
0 Kudos
Hi Mastan Vali,

yes vali,kernal jar file is missing in your script ,you can download the all the jar files for here (https://sourceforge.net/projects/itext/)
mastanvalim
Participant
Hi Manoj,

its working now, i suggest you update this URL in the blog, as earlier mentioned URL doesn't contain all JARs required to run the iflow.

Mastan
jeetendra1211
Discoverer
0 Kudos
Hi Manoj,

 

I have a requirement to convert a .ps format file to pdf. But postman does not return response in my case using above Iflow. It gets stuck to 'sending response' everytime.

Do I need to make any specific changes to groovy script or anywhere else to make it work?

PS: In case of a xml file, it is working fine.

0 Kudos
Hi Jeetendra,

Once try this.

In .ps file contains both text and image, but the above groovy script takes only text. So first try to encode the entire content of the .ps file before the groovy script, then after decode the content in the groovy script with decodeBase64() method .

def body1 = message.getBody(java.lang.String) as String;
def body =body1.decodeBase64();
OutputStream out = new ByteArrayOutputStream()
PdfWriter writer = new PdfWriter(out)
Labels in this area