Skip to Content

Base 64 Decoding And Sending as PDF File

Dear Scn Users,

In a web-service response in a particular field PDF is sent as encoded string.

Requirement is to decode this string and write the PDF as file in the output directory.

Can this be accomplished using Java mapping or any other approach?

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

5 Answers

  • Best Answer
    Jul 17, 2017 at 09:41 AM


    If you don't want to send any response to original sender then why not use File-to-File with SOAP lookup this will be much easier and convenient .

    Here is the sample code you may need to re-tweak it bit lazy day today :P

    package com;
    import sun.misc.BASE64Decoder;
    public class PdftoBase64 {
    public static void main(String[] args) {
    try {
    FileInputStream in = new FileInputStream("C:\\Users\\mkhavatkopp\\Desktop\\PI\\Test\\encoded.txt");
    FileOutputStream out = new FileOutputStream("C:\\Users\\mkhavatkopp\\Desktop\\PI\\Test\\decoded.pdf");
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    int rd;
    byte[] data = new byte[16384];
    while ((rd =, 0, data.length)) != -1)
    buffer.write(data, 0, rd);
    BASE64Decoder decoder = new BASE64Decoder();
    byte[] decodedBytes = decoder.decodeBuffer(buffer.toString());
    catch (Exception e){
    catch (Exception e){



    Add comment
    10|10000 characters needed characters exceeded

    • Thanks Manoj for the idea.

      Completed the scenario modifying the code.

      For End-end I formulated an empty XML using message transform bean and selected ignore in empty file processing mode.

      So no file written using file channel , only the decoded PDF using the code in the UDF.:)

  • Jul 17, 2017 at 10:19 AM

    Hi Raghu!

    After you've built DOM tree for your source XML in java mapping you can try this snippet:


    XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); String xpathString =  "//DocumentFiles/File/Contents"; XPathExpression expr = xpath.compile(xpathString); byte[] fileBytes = DatatypeConverter.parseBase64Binary(((Node)expr.evaluate(doc, XPathConstants.NODE)).getTextContent()); out.write(fileBytes);

    Or you can use:

     byte[] fileBytes = DatatypeConverter.parseBase64Binary(doc.getElementsByTagName("Name").item[0].getTextContent().trim());

    instead of compiling XPath expression for getting the same result element's contents as byte array.

    For building DOM tree of your source XML you can use this code:

    DocumentBuilderFactory bldFactory=DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder;
    bldFactory.setAttribute("", false);
    Document inputDoc; try {
    docBuilder = bldFactory.newDocumentBuilder();
    inputDoc = docBuilder.parse(in);}
    catch (Exception e) {
    throw new StreamTransformationException("Exception caught while parsing input document: " + e.getMessage());

    Regards, Evgeniy.

    Add comment
    10|10000 characters needed characters exceeded

    • Hi Manoj!

      As far as I remember Dynamic Configuration data is lost when you use adapter bridge in ICo configuration. But you can save DC value to module context variable before calling standard adapter bean and restore it back to DC after call.

      Regards, Evgeniy.

  • Jul 17, 2017 at 01:59 PM


    As an alternative to the approach suggested and explained in detail by Evgeniy, you can also give a try to the custom adapter module FormatConversionBean developed and released by Eng Swee - you can find the landing page with module functionality description in Eng Swee's blog. In particular, this adapter module contains functionality for decoding Base64 encoding String and converting it to binary - refer to the corresponding blog on its usage. Provided XPath to the message payload's element that contains Base64 encoded String, the adapter module will replace the original message payload with the binary data that is an outcome of Base64 to binary conversion. The primary difference between these two approaches is, at which message processing step the conversion takes place: as a part of message mapping execution, or after the mapping, when the message already reaches communication channel and before it gets processed by the receiver adapter.



    Add comment
    10|10000 characters needed characters exceeded

  • Jul 14, 2017 at 06:20 PM

    Hi Raghuraman,

    What is the overall end to end setup for this scenario? A client calls a web service which includes that data as part of the response? That data is intended to be delivered as a file to some other destination aside from the primary client?


    Ryan Crosby

    Add comment
    10|10000 characters needed characters exceeded

    • Hello Ryan,

      Its an File-WS Sync scenario using req/resp beans.

      Once I get the output(one field will have encoded string) that needs to be decoded which will generate a PDF.

      This PDF is required in the output directory.

  • Jul 15, 2017 at 03:29 PM

    Hi Raghu!

    If you need just to read PDF from response message and save it as file to any folder, it could be easily done using java mapping.

    Regards, Evgeniy.

    Add comment
    10|10000 characters needed characters exceeded