cancel
Showing results for 
Search instead for 
Did you mean: 

Splitting of a PDF file to multiple PDF Using Java Mapping / Custom Module in SAP PI

Former Member
0 Kudos

Hi,

I am new to PI. Hence kindly excuse me for any ignorance / mistakes.

My requirement is :

To pick a pdf file from a file server location, split it into multiple pdf files and place it in the Target file server location. Splitting of PDF is per page. i.e. if the PDF input file has 4 pages, then i need to create 4 PDF files for each page and place these files in the target location.

Would like to know if this is something acheivable in PI?

I have tried the below approach:

Custom Module Development

Pik up the pdf file from file location using file adapter, read it, get the number of pages, loop in for each page and create a pdf attachment, considering i will write a custom code to get the attachments at the receiver end.

I am able to create 4 pdf attachments along with the main payload but PDF file is not opening.

attachment.setContentType("application/pdf");

Also would like to know how i can get these attachment and write it to the Target directory.

Or please let me know how this can be acheived otherwise.

Second Approach:

Java Mapping was suggested by few people. Would like to know how to split the file and send all the splitted files to the receiver.

Thanks

Accepted Solutions (1)

Accepted Solutions (1)

gagandeep_batra
Active Contributor
0 Kudos

Hi Jessy,

As you are able to achieve payload with 4 attachment.

then you can go for multi mapping and create 4 messages with 1 attachment  & 1 main payload for each,

Then in Receiver CC you can use the swap module to swap attachment  with module.

Regards

Gagan

Former Member
0 Kudos

Hi Gagan

I dont have a message payload, just attachments. how do i create my message structure ? do i need to create a dummy structure. or Should i use Java mapping to acheive this. Can you explain and share the code / screenshots.

Alo my PDF is not created properly, am getting error while opening the file.

Using the below code to create attachment, but PDF is not opening.

Payload attachment = msg.createPayload();

attachment.setContentType("application/pdf");       
attachment.setContent(page.getBytes());

msg.addAttachment(attachment);

anupam_ghosh2
Active Contributor
0 Kudos

Hi Jessy,

                Lets take problem step by step. First are you able to split the PDF file into parts and write each part in target directory?? Lets forget about attachment for the time being.

Regards

Anupam

former_member181985
Active Contributor
0 Kudos

Hi Gagan,

Just fyi, with multi-mapping all the attachments will be considered for each child message and hence the payloadswap bean swaps the same pdf attachment and considers as main payload for each child message.

Best Regards,

Praveen Gujjeti

former_member181985
Active Contributor
0 Kudos

Hi Jessy,

I would recommend a custom adapter module instead of a java mapping which you already developed, since module can be configured via module configuration e.g. 1 page for each PDF file from incoming PDF file with atleast 1 page.

Can you provide us a bit more code snippet of your module code, like what API you are using and how you are splitting.

Once you are able to split proper PDF files, then use payloadzip bean to zip all attachments. Use script to unzip in the target folder

Best Regards,

Praveen Gujjeti

Former Member
0 Kudos

Hi Praveen / Anupam

I am using iTEXT API (itextpdf-5.4.1) to handle the PDF document. PI vesion is 7.31

Steps by Step Approach

1. File adapter pick up the pdf file

2. Read the PDF file

3. Get number of pages

4. Loop in for the total number of pages

5. For each page content, i am creating a payload attachment

6. Added the attachments to the msg

7. Used PayloadSwapBean in the receiver file adapter

7. Only one attachment is getting created in the target directory

Issue

1. PDF attachment is not opening properly.

2. Not able to get all attachment in the target folder

Tried with and without encoding when setting the content. Also tried with byte message.

Below is the module code snippet.

--------------------------------

public ModuleData process(ModuleContext moduleContext,
   ModuleData inputModuleData) throws ModuleException {
  AuditAccess audit = null;
  Object obj = null;
  Message msg = null;
  MessageKey key = null;
  Payload pload = null;
  String ErrorText = "";
 
  try {
   obj = inputModuleData.getPrincipalData();
   msg = (Message) obj;
   // To get Message Payload
   pload = msg.getMainPayload();

   if (pload == null) {
    throw new NullPointerException("Message object is null");
   }
  
   InputStream is = pload.getInputStream();
   PdfReader reader = new PdfReader(is);
   int n = reader.getNumberOfPages();
   String page = null;
             int i = reader.getNumberOfPages();
            for(int m = 1 ; m <= i ; m ++)
             {
                  page = PdfTextExtractor.getTextFromPage(reader, m);
                  Payload attachment = msg.createPayload();
                    attachment.setContentType("application/pdf");
                   attachment.setContent(page.getBytes());
                    msg.addAttachment(attachment);
              }
    inputModuleData.setPrincipalData(msg);  
}

--------------------------------

Pls let me know for any issues with the code / approach.

Thanks

former_member181985
Active Contributor
0 Kudos

Hi Jessy,

The code you have provided is not right. You are extracting text from each page and writing it as a normal file( though with extension as PDF) . You should write the content as a PDF file.

More over, since your requirement is to get each page content from input PDF file AS-IS and write it AS-IS as a new pdf file.

I will try to provide the required code snippet shortly

Regards,

Praveen Gujjeti

former_member181985
Active Contributor
former_member181985
Active Contributor
0 Kudos

try with this code snippet,

Use packages

import com.itextpdf.text.pdf.*;

import com.itextpdf.text.pdf.parser.*;

import com.itextpdf.text.Document;

------------- Code Snippet ------------------------

ByteArrayOutputStream baos;

PdfReader reader = new PdfReader(fis);

  int n = reader.getNumberOfPages();

  System.out.println ("Number of pages : " + n);

  int i = 0;           

  while ( i < n ) {

  baos = new ByteArrayOutputStream();

  Payload attachment = msg.createPayload();

  

  Document document = new Document(reader.getPageSizeWithRotation(1));

  PdfCopy writer = new PdfCopy(document, baos);

  document.open();

  PdfImportedPage page = writer.getImportedPage(reader, ++i);

  writer.addPage(page);

  document.close();

  writer.close();

  attachment.setContentType("application/pdf");

  attachment.setContent(baos.toByteArray());

  msg.addAttachment(attachment);

  }

Regards,

Praveen Gujjeti

Former Member
0 Kudos

Thanks Praveen and all for the response. We are facing some server issues and hence i couldn't test this till now. Will let you know once i test the same.

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi

Client has now come back and said they cannot handle zip file, and they need the splitted PDF files placed on the target file location seperately.

I could think of the File Adapter OS Command Line Feature to unzip the files , but that would require unzip installation. Would like to know the Pros and Cons of this approach.

Also let me know if there is any other approach to acheive this.

Thanks

markangelo_dihiansan
Active Contributor
0 Kudos

Hi Jessy,


Would like to know if this is something acheivable in PI?

Custom Module Development

Pik up the pdf file from file location using file adapter, read it, get the number of pages, loop in for each page and create a pdf attachment, considering i will write a custom code to get the attachments at the receiver end.

I am able to create 4 pdf attachments along with the main payload but PDF file is not opening.

Where are you opening the PDF file? Did you download it first before opening? Have you tried your code locally (without PI)? It should be possible via adapter module or Java mapping though. What is the adapter of the receiving system? Because your approach will be based on that.

Regards,

Mark

Former Member
0 Kudos

Hi Mark

Both Source & Target - File Adapter. Tried downloading the file and then also its not opening. When i created a text attachment, it is fine.

Also I need not to create an attachment. i just tried this way.

All i needed is to split the pdf into multiple and pass all the newly created PDF's to the target file directory.

Please let me know how this can be acheived.