Skip to Content

Base64 XML to PDF - PI7.3

Hi

I have a scenario whereby a ABAP Proxy is being used to send a PDF from ECC6 to PI in the form of a XML containing a Base64 string within a pair of XML tags.

The XPath to the Base64 String is /ns0:Base64Transmission/Row/Data

I have used the following code taken from other blogs on the same subject, to try and write the PDF using the file adapter:

package domDemo;

import java.io.ByteArrayInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.apache.commons.codec.binary.Base64;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
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 JMDecodeDetails extends AbstractTransformation {

public void transform(TransformationInput arg0, TransformationOutput arg1)

throws StreamTransformationException {

  try {

   DocumentBuilderFactory factory = DocumentBuilderFactory

   .newInstance();

   DocumentBuilder builder = factory.newDocumentBuilder();

   Document docOld = builder.parse(arg0.getInputPayload()

     .getInputStream());

   // XPath to tag containing Base64 PDF is:
   // /ns0:Base64Transmission/Row/Data

   NodeList details = docOld.getElementsByTagName("Data");

   String data = details.item(0).getChildNodes().item(0)
   .getNodeValue();

   byte[] decodedBytes = Base64.decodeBase64(data.getBytes());

   Document docNew = builder.parse(new ByteArrayInputStream(
     decodedBytes));

   TransformerFactory transformerFactory = TransformerFactory

   .newInstance();

   Transformer transformer = transformerFactory.newTransformer();

   DOMSource source = new DOMSource(docNew);

   StreamResult result = new StreamResult(arg1.getOutputPayload()

     .getOutputStream());

   transformer.transform(source, result);

  } catch (Exception e) {

   e.printStackTrace();

  }

}

}

The mapping executes successfully, but the result is a 0-byte sized file.

When I execute the test tool in the ESR, I get an error "Unable to display tree view; Error when parsing an XML document (Premature end of file.)"

Im not sure if this is relevant, or just because the result of the mapping is not an XML document.

Can anyone suggest what the issue is here please?

Many thanks in advance.

Steve

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Best Answer
    Jul 08, 2015 at 02:12 AM

    Stephen,

    PDF can not be written to output as XML.

    Here is updated code. Please test the interface end to end.

        public void transform(TransformationInput arg0, TransformationOutput arg1) throws StreamTransformationException {
            try {
                Document docOld = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(arg0.getInputPayload().getInputStream());
                // XPath to tag containing Base64 PDF is: /ns0:Base64Transmission/Row/Data
                NodeList details = docOld.getElementsByTagName("Data");
                String data = details.item(0).getChildNodes().item(0).getNodeValue();
                byte[] decodedBytes = Base64.decodeBase64(data.getBytes());
    
    
                //Write transformed PDF to outputsteam.
                arg1.getOutputPayload().getOutputStream().write(decodedBytes);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
    Add comment
    10|10000 characters needed characters exceeded

    • Hi

      I tried your modified code, but I still get a zero byte file when tested end-to-end.

      In the OM test tool I now get a different error though, "Unable to display tree view; Error when parsing an XML document (Content is not allowed in prolog.)"

      Regards

      Steve

  • Jul 08, 2015 at 05:42 AM

    Hi Stephen,

    You can actually test binary in operation mapping 😊

    Can we test Binary Files in Interface/Operation Mapping???

    Correct me if I'm wrong, but I still think that you need a pdf writer library to write your outputstream data as a pdf.

    Regards,

    Mark

    Add comment
    10|10000 characters needed characters exceeded

  • Jul 08, 2015 at 10:53 AM

    ok, so using the revised code, and the OM testing 'trick', everything looks good.
    When I extract the results of the OM and rename to .pdf, I see the PDF document.

    However, in the end-to-end test, which is definately using the same OM, I am still getting a zero-byte file.

    Add comment
    10|10000 characters needed characters exceeded