cancel
Showing results for 
Search instead for 
Did you mean: 

XML to PDF conversion and create attachment in java mapping

Former Member
0 Kudos

Hello Experts,

I am trying to convert XML to PDF using XSLFO. The code works fine in local NWDS. I have uploaded the relevant jar files as well into PI (7.3 Version) and executed end to end scenario. The Scenario is from File to File. I am placing an XML File but the PDF File is is emptyin the target folder. So I have tried to create an attachment in SAP PI with below code.

     OutputAttachments outAttach = arg1.getOutputAttachments();

    Attachment attOut = outAttach.create("PDF_Output.pdf",outStream.toByteArray());

    outAttach.setAttachment(attOut);

I couldnt see any attachments in the moni/channel monitoring. The Mapping seem to work successfully without any failures.

Can you please help me out in creating attachments in SAP PI. I have already referred to below links

http://scn.sap.com/docs/DOC-26528

http://scn.sap.com/thread/1918880

http://scn.sap.com/thread/1953315

Regards,

N. Jayanth Kumar.

Accepted Solutions (0)

Answers (3)

Answers (3)

former_member181985
Active Contributor
0 Kudos

Can you attach the java program file? Since it is file to file scenario no need to go for attachments.

Check my other blogs which will collectively help in such situations

http://scn.sap.com/community/pi-and-soa-middleware/blog/2012/07/08/can-we-test-binary-files-in-inter...

http://scn.sap.com/community/pi-and-soa-middleware/blog/2012/08/05/coding-java-mapping-points-to-pon...

Regards,

Praveen Gujjeti

Former Member
0 Kudos

Hi Praveen,

I have used the below code. I am using XSLFO for XML to PDF conversion. It works well in local system, when run through NWDS, but not working in PI. It doesnt throw any error at all when executed in PI. When I saw the Log viewer, it is throwing error in Parsing. I have uploaded all relevant jar files onto PI (7.3 Version) ESR. Request you to please provide your valuable suggestions.

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;
import org.apache.fop.render.pdf.PDFRenderer;

import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;

public class xml_to_pdf extends AbstractTransformation {


  private Map param;

  BufferedReader docInput;
  String inputLine = "";
  private String finalOutput = "";
  StringBuffer inputData = new StringBuffer();
  StringBuffer outputData = new StringBuffer();
 
  InputStream fin_xsl = getClass().getResourceAsStream("/xslfo_xslt/XSLT.txt");
  InputStream inputStream = null;
  OutputStream outputStream = null;

     
  public xml_to_pdf() throws Exception
  {
   docInput = null;
  }

  public void setParameter(Map param) {
   // TODO Auto-generated method stub
   this.param = param;
   if (param == null) {
    this.param = new HashMap();
   }
  }
 
  public void transform(TransformationInput arg0, TransformationOutput arg1) throws StreamTransformationException
  {

  //An info message is added to trace. An instance of trace of object is obtained by calling
  // the getTrace method of class AbstractTransformation
  getTrace().addInfo("JAVA Mapping Called");
  try
  {
   inputStream = arg0.getInputPayload().getInputStream();
   outputStream = arg1.getOutputPayload().getOutputStream();
  }
  catch (Exception e)
  {
   throw new StreamTransformationException("Unable to parse input document - ".concat(e.getMessage()));
  }
   //the XSL FO file

// the XML file from which we take the name
  StreamSource source = new StreamSource(inputStream);
// creation of transform source
  StreamSource transformSource = new StreamSource(fin_xsl);
     
try
{
  // Initializing Trace
  docInput =  new BufferedReader(new InputStreamReader(inputStream));
}
catch (Exception e)
{
  throw new StreamTransformationException("Unable to parse input document - ".concat(e.getMessage()));
}

    
// create an instance of fop factory

getTrace().addInfo("create an instance of fop factory \n");

  FopFactory fopFactory = FopFactory.newInstance();
 
// a user agent is needed for transformation

getTrace().addInfo("a user agent is needed for transformation \n");
 
  FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
  //System.out.println("Renderer Override : "+ foUserAgent.getRendererOverride().toString());
// to store output
  ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    
Transformer xslfoTransformer;
//Setup XSLT
   


  getTrace().addInfo("Conversion beginning \n");
 
  try
  {
    xslfoTransformer = getTransformer(transformSource);
    Fop fop;
   try
   {
   
    PDFRenderer pdfRenderer = new PDFRenderer();
    pdfRenderer.setUserAgent(foUserAgent);
    foUserAgent.setRendererOverride(pdfRenderer);
   
    getTrace().addInfo("Starting conversion \n");
     
    fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, outStream);
       
    getTrace().addInfo("Before XSLT");
       
    getTrace().addInfo("After XSLT");
       
    //Set the value of a <param> in the stylesheet
    //transformer.setParameter("versionParam", "2.0");
           
    // Resulting SAX events (the generated FO)
    // must be piped through to FOP
    Result res = new SAXResult(fop.getDefaultHandler());
       
    // Setup input for XSLT transformation
    //transformer.transform(source, res);
    xslfoTransformer.transform(source,res);
 
    //Writing the content to Output Stream
      
    outputStream.write(outStream.toByteArray());
    outputStream.close();

   }
  catch (Exception e) {
   //throw e;
   System.out.println("Error caught "+e.toString());
   getTrace().addInfo("Error caught "+e.toString());
  }
}
catch (Exception e)
{
  //throw e;
  System.out.println("Error here"+e.toString());
  getTrace().addInfo("Error caught "+e.toString());
}
   
  System.gc();
  }
 
private Transformer getTransformer(StreamSource streamSource)
  {
   // setup the xslt transformer
   net.sf.saxon.TransformerFactoryImpl impl = new net.sf.saxon.TransformerFactoryImpl();

   try {
    return impl.newTransformer(streamSource);

   } catch (TransformerConfigurationException e) {
    System.out.println("Error caught in get transformer "+e.toString());
    //e.printStackTrace();
   }
   return null;
  } 
}

Regards,

N. Jayanth Kumar.

Former Member
0 Kudos

Hi Praveen,

I tried to execute with above code, but I am getting the below error in PI 7.3 Log Viewer.

org.apache.fop.fo.ElementMappingRegistry findFOMaker

WARNING: Unknown formatting object http://www.w3.org/1999/XSL/Format^root

Can you please let me know what could be the cause of this issue?

Regards,

N. Jayanth Kumar.

markangelo_dihiansan
Active Contributor
0 Kudos

Hello,

In your operation mapping, did you check the read attachments option?

Regards,

Mark

Former Member
0 Kudos

Hello Mark,

Yes I have checked the read attachments check box in the Operation Mapping. The scenario is we are not getting anything as attachments. We need to pick the XML Payload convert into PDF and then place it in another folder via FTP.

Former Member
0 Kudos

Hi Experts,

Just for an update, I would like to know if a PDF can be accessed as Payload to be placed in a target folder, if that is not possible, can we create as an attachment, use a payload swapbean to convert that into payload?

Regards,

N. Jayanth Kumar