Skip to Content

Generate PDF from URL

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

XML URL.png (17.7 kB)
URL Explorador.png (27.2 kB)
Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Posted on Aug 13, 2016 at 04:38 AM

    Hi Emir!

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

    Regards, Evgeniy.

    Add a comment
    10|10000 characters needed characters exceeded

    • 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.

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.