Skip to Content
Technical Articles
Author's profile photo Manojkumar Dugyala

Converting the normal PDF file to Password Protected File using SAP CPI

In this scenario we are going to change normal pdf file into password protected file using  SAP CPI.

Step 1: Create the Iflow with the desired name. In this blog I am giving the Iflow name as Dummy(As shown in the below picture).

Step 2: Connect the sender and integration process with the HTTP Sender Adapter and give the preferred HTTP address.

Step 3: Now add the itextpdf JAR file which will be used in the groovy script in the Iflow by going to references tab – archive.

Here is the link to download the JAR (https://jar-download.com/artifacts/com.itextpdf/itextpdf/5.5.13/source-code).

 

Step 4: Create one groovy script and write the script below.

 

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import com.itextpdf.text.Document
import com.itextpdf.text.Paragraph
import com.itextpdf.text.pdf.PdfWriter
import com.itextpdf.text.pdf.PdfReader
import com.itextpdf.text.pdf.PdfStamper
def Message processData(Message message) {
    def inputPayload = message.getBody();
    def name1 = "username";
    def pass1 = name1.substring(0,4);
    def name2 = "password"
    def pass2 = name2.substring(0,4);
    def userPassword = pass1+pass2;
    def reader = new PdfReader(inputPayload.getBytes());
    def outputStreamProtected = new ByteArrayOutputStream();
    def stamper = new PdfStamper(reader, outputStreamProtected);
    stamper.setEncryption(userPassword.getBytes(), null, PdfWriter.ALLOW_PRINTING, PdfWriter.ENCRYPTION_AES_128);
    stamper.close();
    message.setBody(outputStreamProtected.toByteArray());
    return message;
}

 

Step 5: 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.

In the Postman tool create 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 the type to binary and upload your pdf, after that Click on the send button.

 

After the completion of this request, we get the below screen, now click on the right side 3 dots , and save response to file.

 

Now you can open the pdf file and check whether the password is set or not to the given pdf.

 

Assigned Tags

      3 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Chanchal Agrawal
      Chanchal Agrawal

      Thank you for sharing.

       

      Author's profile photo Mastan Vali Meduturu
      Mastan Vali Meduturu

      nice blog, but we cannot expose the password in script directly 🙂

      anyway we can encapsulate password in script?

       

      Regards,

      Mastan

       

       

       

      Author's profile photo Manojkumar Dugyala
      Manojkumar Dugyala
      Blog Post Author

      just for practice i exposed it ,we can hide or pass it during the runtime with the help of headers or property