Skip to Content
avatar image
Former Member

UDF to read PDF Attachment and convert to Base64

Hi Experts,

I have a requirement to send xml file in regular async mode and pdf file as an attachment using file sender adapter

XML- should be processed in normal async procedure and PDF attached file needs to be read using UDF to convert to Base64 string and send the base64 output to target field

I used additional file procedure in sender file channel and able to process xml successfully however unable to read pdf and convert to base64 using UDF

I have read few threads/blogs and tried with UDF's but unable to read and covert the pdf file

https://archive.sap.com/discussions/thread/3477611 https://archive.sap.com/discussions/thread/1341089

sender-file-confg.png

Issues:-

1) pdf attachment does show as <Filename>.pdf it shows as <FileName>.xml, how do I convert to <Filename>.pdf using file sender adapter

Shows as:-

pdf-attachment-asxml.png

Expected as:-

expected-pdf-file-attachment.png

2) tried with below blog UDF to read the <Filename>.pdf and convert to base64 but get errors

https://archive.sap.com/discussions/thread/3477611

Any one who has successfully used UDF to read PDF and converted to base64, please share the UDF code

Thank you

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

9 Answers

  • Best Answer
    Jul 19, 2017 at 12:37 PM

    Follow as below :

    1.In Sender channel make config as mentioned in below screenshot . This will Pick XML as Main Payload and PDF as attachment.

    2. Tick the Option " Read Attachment in OM"

    3. Use the below UDF in your Mapping .

    String Content = "";
    byte[] bt; String AttachmentID =""; GlobalContainer globalContainer = container.getGlobalContainer();
    InputAttachments inputAttachments = globalContainer.getInputAttachments(); if(inputAttachments.areAttachmentsAvailable())
    {      
    Collection<String> CollectionIDs = inputAttachments.getAllContentIds(true);      
    Object[] arrayObj = CollectionIDs.toArray();  
    int attachmentCount = arrayObj.length;   
    for(int i =0;i<attachmentCount;i++)      {
                   AttachmentID =(String) arrayObj[i];
                   Attachment attachments =inputAttachments.getAttachment(AttachmentID);
                   Content = Content + attachments.getBase64EncodedContent();
          }
    }
    return Content;

    Br,

    Manoj

    Add comment
    10|10000 characters needed characters exceeded

  • Jul 18, 2017 at 06:33 PM

    Hi Sravanthi!

    1. I don't have PI system in hands right now, but as far as I remember, file mask for attachment should look like *.xml=*.pdf

    2. Attachment interface doesn't have getName() method. If I'm not wrong, this method is available for Payload object in Adapter framework.

    Regards, Evgeniy.

    Add comment
    10|10000 characters needed characters exceeded

  • Jul 18, 2017 at 06:37 PM

    Sravathi,

    For reading attachment name add MultiPartHeaderBean in sender channel and read filename via dynamic udf.

    Br,

    Manoj

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 19, 2017 at 11:46 AM

    i tried this config and it comes as pdf260798.jpg

    Add comment
    10|10000 characters needed characters exceeded

  • Jul 18, 2017 at 12:36 PM

    Hi Sravanthi!

    1. Please share your sender file adapter configuration screenshots.

    2. What are the errors you get while executing your UDF?

    Regards, Evgeniy.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 18, 2017 at 06:21 PM

    Hi,

    I have attached sender file config screen shot , please see all screen shots attached in first post

    sender-file-confg.png

    pdf-attachment-shows-as-xml.png

    expected-pdf-file-attachment-something-like-this.png

    Below is the UDF i used to fetch pdf attachment and covert to base 64

    GlobalContainer globalContainer = container.getGlobalContainer();

    InputAttachments inputAttachments = globalContainer.getInputAttachments();

    AbstractTrace trace = container.getTrace();

    try

    {

    //check the availability of attachments in input message

    if(inputAttachments.areAttachmentsAvailable())

    {

    trace.addInfo("Input attachment available");

    //get attachmentIds and store it in an Object array

    Collection<String> CollectionIDs = inputAttachments.getAllContentIds(true);

    Object[] arrayObj = CollectionIDs.toArray();

    int attachmentCount = arrayObj.length;

    String[] Content = new String[attachmentCount];

    String[] AttachmentID = new String[attachmentCount];

    String[] AttachmentName = new String[attachmentCount];

    //Loops input attachments to get content of the attachment

    for(int i =0;i<attachmentCount;i++)

    {

    AttachmentID[i] =(String) arrayObj[i];

    Attachment attachments =inputAttachments.getAttachment(AttachmentID[i]);

    Content[i] = attachments.getBase64EncodedContent();

    //AttachmentName[i] = attachments.getContentType();

    AttachmentName[i] = attachments.getName();

    AttData.addValue(Content[i]);

    AttName.addValue(AttachmentName[i]);

    }

    }

    }

    catch (Exception e)

    {

    e.printStackTrace();

    }

    and it compiles with below error after adding attachment.getname() method.

    cannot find symbol
    symbol : method getName()

    location: interface com.sap.aii.mapping.api.Attachment

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 18, 2017 at 06:48 PM

    HI,

    I will try this however my initial issue was to read pdf attchment and convert to binay, after this i can look into reading the attachment name

    Thanks

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 18, 2017 at 07:23 PM

    Thank you

    I will try that also can you help me know why i dont see the attachment name with extension .pdf and see it as .xml even though my sender file channel says namepart = ".xml"=".pdf"

    Pleasee see the screen shots

    Add comment
    10|10000 characters needed characters exceeded

    • On your screenshot I see Test.xml=Test.pdf instead of .xml=.pdf

      Can you confirm that you've changed those settings? If yes - try to restart the channel.

      In your monitoring screenshots I see the files with different names and different sizes. Looks like you have your XML file as attachment. Or are there screenshots showing the different messages?

      Don't you use any additional modules like PayloadSwapBean in your channel?

      Regards, Evgeniy.

  • avatar image
    Former Member
    Jul 18, 2017 at 11:44 PM

    Hi,

    I have commented some lines in udf and able to activate with out error however when tested I am still unable to see the attachment as .pdf it still shows as .xml

    sender-file-config.png

    source-files.png

    attachment-content-type.png

    Thanks

    Add comment
    10|10000 characters needed characters exceeded