cancel
Showing results for 
Search instead for 
Did you mean: 

Generate PDF from URL

emir_morillo2
Explorer
0 Kudos

Hi SCN, i have the next scenario.

Is possible generate one PDF file from URL?.

This is the XML

If i copy this url in Firefox, i have the next result

Really i need generate a PDF file o a attachment from this url address.

Thank for your collaboration

Regards,

Emir Morillo

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member190293
Active Contributor
0 Kudos

Hi Emir!

You could use java code to get file by URL and add it to your message as attachment.

Regards, Evgeniy.

emir_morillo2
Explorer
0 Kudos

Hi Evgeniy, thansk for you response.

I not found a java code that perform this transformation.

you have a java code that I can be useful?

Regards.

former_member190293
Active Contributor
0 Kudos

Hi Emir!

You can use following method in your java mapping or UDF (though I didn't test it myself as UDF) to get byte array from URL link:

     public byte[] getByteArrayFromURL(String url) {

          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) {

               e.printStackTrace();

          }

          return os.toByteArray();

     }

Regards, Evgeniy.

former_member190293
Active Contributor
0 Kudos

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.