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: 
I have experience of about 6 years in SAP PI/PO/CPI. Currently working as a CPI Developer. CPI has many inbuilt features like converting CSV to XML , XML to CSV but creating PDF is not available till now. In this blog we will be exploring how we can create PDF and also send that as an attachment in zipped content .

Background

We have a requirement where we have to zip a pdf and a csv file and send that as an attachment in mail. In this scenario, we will be getting an xml file from where we have to pick certain fields that we will write in a csv file and in that same xml in a particular field we will get one encoded string, which we have to decode and write in a pdf file, after which we will zip the csv and pdf file and send as an attachment.

I am assuming people have basic knowledge of creating an IFlow and using the various steps from design palette .

For this scenario we need to use PDFBox Library for creating PDF’s . For Downloading Jar files use the link https://pdfbox.apache.org/download.cgi#20x and download the required Jars



We can add JAR files by clicking on Resources tab. We will get an Add dropdown menu from where we can select Archive as shown below:



Click on Archive, then select the Jars which are downloaded



After adding the Jars we can use the PDFBox Library methods. In this code I have shown how to create CSV and PDF file and zip them.

Note: Content of PDF and CSV file we can take from header, property, body as per our requirement

import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

import org.apache.pdfbox.pdmodel.common.PDStream;

import org.apache.pdfbox.pdmodel.PDDocument;

import org.apache.pdfbox.pdmodel.PDPage;

import org.apache.pdfbox.pdmodel.PDPageContentStream;

import org.apache.pdfbox.pdmodel.font.PDType1Font;

import java.io.IOException;

import java.util.Arrays;

import java.util.zip.ZipEntry;

import java.util.zip.ZipInputStream;

import java.util.zip.ZipOutputStream;

import org.apache.camel.impl.DefaultAttachment

import javax.mail.util.ByteArrayDataSource

 

def Message processData(Message message) {

//Body

def body = message.getBody(java.lang.String) as String; // this value is used for the CSV file

byte[] b = body.getBytes(); // converting body to byteArray

ByteArrayOutputStream byteArrayOutputStreamcsv = new ByteArrayOutputStream();

byteArrayOutputStreamcsv.write(b,0,b.length);

// Creating PDF

PDDocument doc = new PDDocument();

PDPage Page = new PDPage();

//Adding the blank page to the document

doc.addPage(Page);

PDPageContentStream contentStream = new PDPageContentStream(doc, Page);

contentStream.beginText();

contentStream.newLineAtOffset(20, 450);

contentStream.setFont(PDType1Font.TIMES_BOLD_ITALIC, 14);

String text = "My First PDF Document."; // this value we can take from body, property or header as per our requirement

contentStream.showText(text);

contentStream.endText();

contentStream.close();

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

doc.save(byteArrayOutputStream);

doc.close();

// Zipping Content

ByteArrayOutputStream zipbyteArrayOutputStream = new ByteArrayOutputStream();

ZipOutputStream zos = new ZipOutputStream(zipbyteArrayOutputStream);

ZipEntry ze1 = new ZipEntry("ZipEntry1.pdf"); // give pdf file name as per naming convention

//illustrating putNextEntry method

ZipEntry ze2 = new ZipEntry("ZipEntry1.csv"); // give csv file name as per naming convention

//illustrating putNextEntry method

zos.putNextEntry(ze1);

byte[] bytes =  byteArrayOutputStream.toByteArray() // putting PDF content in byteArray

zos.write(bytes, 0, bytes.length);

zos.putNextEntry(ze2);

byte[] bytescsv =  byteArrayOutputStreamcsv.toByteArray() // putting CSV content in byteArray

zos.write(bytescsv, 0, bytescsv.length);

zos.closeEntry();

zos.close();

def dataSource = new ByteArrayDataSource(zipbyteArrayOutputStream.toByteArray(),   'application/zip')

//  Construct a DefaultAttachment object

def attachment = new DefaultAttachment(dataSource)

//    Add the attachment to the message

message.addAttachmentObject('hello-world.zip', attachment) // give zipfile name as per naming convention

return message;

}

 

Then create a mail adapter and tick the checkbox “Add Message Attachments” as shown in the screenshot below.



We will get the mail output as shown below:



When we open the zip we will get the content as shown below:



 

Final Note

Similarly, we can send PDF and Image files as per our need. In receiver mail adapter also we can send ZIP file as attachment but sending PDF is still not available. If any bugs are encountered please feel free to raise your query in the comment section.

That is it for this blog post. Try this in your scenarios! Reward points if you like it!

References

https://www.javatpoint.com/pdfbox-tutorial

https://blogs.sap.com/2017/10/03/adding-cloud-integration-attachments-in-code/

 
4 Comments
Labels in this area