Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

This wiki is in extension to the blog Trouble writing out a PDF in XI/PI?

The below class uses the iText library to create PDF files. The code is neither part of a java mapping nor an adapter module but is a sample that will help the reader as a reference when he/she is required to custom code in XI/PI.

Assumptions:

1. MessageTransformBean converts the target XML to a FLAT format. The field separator is comma and the end of each record can be identified by the string literal 'END'

2. The iText library is downloaded and imported for the project.

Eg. Input format:100,Anakin Skywalker,EAS,ConsultantEND200,Darth Vader,Java,Sr. ProgrammerEND300,Obi-Wan Kenobi,MSTechmologies,Project ManagerEND Here there are 3 records and 4 fields per record.

The output PDF will have a table which will have a Main Header, followed by the header for each column of the data followed by the data itself. I have tried to experiment with some colors. Forgive my taste when it comes to being an aesthetic (smile)

 The below is the code that will try capture the above requirements,

/*
 This is a java class to create a PDF document out of a given string
 Created By Shabarish Vijayakumar on May 17' 2009
 */
import java.awt.Color;
import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class createPDF {

    public static void main(String[] args) {

        //        Assume the below is the input file format
        String input =
            "100,Anakin Skywalker,EAS,ConsultantEND200,Darth Vader,Java,Sr. ProgrammerEND300,Obi-Wan Kenobi,MSTechnologies,Project ManagerEND";

        // creation of a document-object
        Document document = new Document();
        try {

            // create a writer
            PdfWriter.getInstance(
            // that listens to the document
            document,
            // and directs a PDF-stream to a file
            new FileOutputStream("output.pdf"));
            // open the document
            document.open();
            // add a table to the document
            PdfPTable table = new PdfPTable(4);
            PdfPCell cell =
                new PdfPCell(
                    new Paragraph("Employee Details for XYZ Organization"));
            cell.setColspan(4);
            cell.setBackgroundColor(Color.red);
            cell.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(cell);

            //Set Header Text for the Table
            cell = new PdfPCell(new Paragraph("EMP NO"));
            cell.setBackgroundColor(Color.blue);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("NAME"));
            cell.setBackgroundColor(Color.blue);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("DEPARTMENT"));
            cell.setBackgroundColor(Color.blue);
            table.addCell(cell);

            cell = new PdfPCell(new Paragraph("DESIGNATION"));
            cell.setBackgroundColor(Color.blue);
            table.addCell(cell);

            //Fill data to the table                       

            String inputArray[] = input.split("END");

            for (int i = 0; i < inputArray.length; i++) {

                String fieldValuesArray[] = inputArray[i].split(",");

                for (int j = 0; j < fieldValuesArray.length; j++) {

                    table.addCell(fieldValuesArray[j].toString());

                }
            }

            document.add(table);
        } catch (DocumentException de) {
            System.err.println(de.getMessage());
        } catch (IOException ioe) {
            System.err.println(ioe.getMessage());
        }

        // close the document
        document.close();

    }
}
 




So now this is what the final output will look like;



Note: When coding for the module, write out to a Outputstream instead of a FileOutputStream

1 Comment

  1. Former Member

    /*
    * Created on May 26, 2014
    *
    * To change the template for this generated file go to
    * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
    */
    package packme;

    /**
    * @author ashutosh.a.upadhyay
    *
    * To change the template for this generated type comment go to
    * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
    */
    import java.io.BufferedReader;
    import java.io.ByteArrayOutputStream;
    import java.io.InputStreamReader;
    import javax.ejb.CreateException;
    import javax.ejb.SessionBean;
    import javax.ejb.SessionContext;
    import com.lowagie.text.Document;
    import com.lowagie.text.PageSize;
    import com.lowagie.text.Paragraph;
    import com.lowagie.text.pdf.PdfWriter;
    import com.sap.aii.af.mp.module.Module;
    import com.sap.aii.af.mp.module.ModuleContext;
    import com.sap.aii.af.mp.module.ModuleData;
    import com.sap.aii.af.mp.module.ModuleException;
    import com.sap.aii.af.ra.ms.api.Message;
    import com.sap.aii.af.ra.ms.api.MessageDirection;
    import com.sap.aii.af.ra.ms.api.XMLPayload;
    import com.sap.aii.af.service.auditlog.Audit;
    import com.sap.aii.af.service.auditlog.AuditDirection;
    import com.sap.aii.af.service.auditlog.AuditLogStatus;
    import com.sap.aii.af.service.auditlog.AuditMessageKey;


    /**
    * @ejbHome <{com.sap.aii.af.mp.module.ModuleHome}>
    * @ejbLocal <{com.sap.aii.af.mp.module.ModuleLocal}>
    * @ejbLocalHome <{com.sap.aii.af.mp.module.ModuleLocalHome}>
    * @ejbRemote <{com.sap.aii.af.mp.module.ModuleRemote}>
    * @stateless
    */

    public class TextToPDF implements SessionBean, Module{
    private SessionContext myContext;
    AuditMessageKey amk;
    public void ejbRemove(){
    }
    public void ejbActivate(){
    }
    public void ejbPassivate(){
    }
    public void setSessionContext(SessionContext context){
    myContext = context;
    }
    public void ejbCreate() throws CreateException {
    }

    public ModuleData process(ModuleContext moduleContext,ModuleData inputModuleData)throws ModuleException {
    String text = "";
    String line = "";
    Object obj = inputModuleData.getPrincipalData();
    Message msg = null;
    XMLPayload xp = null;
    msg = (Message) obj;
    try {

    if (msg.getMessageDirection()== MessageDirection.INBOUND)
    amk = new AuditMessageKey(msg.getMessageId(),AuditDirection.INBOUND);
    else
    amk = new AuditMessageKey(msg.getMessageId(),AuditDirection.OUTBOUND);

    xp = msg.getDocument();
    Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"XML payload object received.");

    if (xp != null){
    Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Converstion started....");
    InputStreamReader in = new InputStreamReader(xp.getInputStream());
    BufferedReader bin = new BufferedReader(in);

    while((line = bin.readLine()) != null){
    System.out.println(line);
    text = text+line;
    text = text+"\n";
    }
    in.close();
    bin.close();
    byte by [] = text.getBytes();
    Document document = new Document(PageSize.A4, 36, 72, 108, 180);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    PdfWriter.getInstance(document,output);
    document.open();
    document.add(new Paragraph(text));
    System.out.println("Text added");
    document.close();
    byte byt[] = output.toByteArray();
    xp.setContent(byt);
    Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Conversion Done Successfully.");
    }
    inputModuleData.setPrincipalData(msg);
    Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Principle data is set successfully.");
    }
    catch (Exception e){
    Audit.addAuditLogEntry(amk, AuditLogStatus.SUCCESS,"Module Exception Caught .");
    ModuleException me = new ModuleException(e);
    throw me;
    }
    return inputModuleData;
    }
    }