Quantcast
Viewing all articles
Browse latest Browse all 3019

Re: Generate PDF from URL

Here is a sample code for transformation class:

 

import java.io.ByteArrayOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.URL;

import java.nio.ByteBuffer;

import java.nio.channels.Channels;

import java.nio.channels.ReadableByteChannel;

import java.nio.channels.WritableByteChannel;

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.w3c.dom.Document;

import org.w3c.dom.Element;

import com.sap.aii.mapping.api.AbstractTrace;

import com.sap.aii.mapping.api.AbstractTransformation;

import com.sap.aii.mapping.api.Attachment;

import com.sap.aii.mapping.api.OutputAttachments;

import com.sap.aii.mapping.api.StreamTransformationException;

import com.sap.aii.mapping.api.TransformationInput;

import com.sap.aii.mapping.api.TransformationOutput;

 

public class TransformerWithURLAttachmentClass extends AbstractTransformation {

    OutputAttachments outAttachments = null;

 

    public void transform(TransformationInput inMessage, TransformationOutput outMessage) throws StreamTransformationException{

          this.outAttachments = outMessage.getOutputAttachments();

 

          this.executeMapping(inMessage.getInputPayload().getInputStream(), outMessage.getOutputPayload().getOutputStream());

     }

 

    public void executeMapping(InputStream in, OutputStream out) throws StreamTransformationException {

 

          AbstractTrace trace = getTrace();

          trace.addInfo("Transformation started.");

 

          DocumentBuilderFactory bldFactory=DocumentBuilderFactory.newInstance();

          bldFactory.setIgnoringElementContentWhitespace(true);

          bldFactory.setNamespaceAware(true);

 

          try {

              Document inputDoc = bldFactory.newDocumentBuilder().parse(in);

              Element docRoot = inputDoc.getDocumentElement();

 

              String attURL = docRoot.getElementsByTagName("AttachmentURL").item(0).getTextContent();

              byte[] attBytes = getBytesFromURL(attURL);

              Attachment newAttachment = outAttachments.create("URLAttachment","image/gif", attBytes);

              outAttachments.setAttachment(newAttachment);

 

              Transformer tr = TransformerFactory.newInstance().newTransformer();

              tr.transform(new DOMSource(inputDoc), new StreamResult(out));

          }

          catch (Exception e) {

              throw new StreamTransformationException(e.getMessage());

          }

    }

 

    public byte[] getBytesFromURL(String url) throws StreamTransformationException {

          ByteArrayOutputStream os=new ByteArrayOutputStream();

          try {

              URL httpURL = new URL(url);

              ReadableByteChannel rbc = Channels.newChannel(httpURL.openStream());

              WritableByteChannel osc=Channels.newChannel(os);

 

              ByteBuffer byteBuf=ByteBuffer.allocate(65536);

              while (rbc.read(byteBuf) != -1) {

                    byteBuf.flip();

                    osc.write(byteBuf);

                    byteBuf.clear();

              }

          }

          catch (Exception e) {

              throw new StreamTransformationException(e.getMessage());

          }

          return os.toByteArray();

    }

 

    public static void main(String[] args) {

    }

 

}

 

Regards, Evgeniy.


Viewing all articles
Browse latest Browse all 3019

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>