cancel
Showing results for 
Search instead for 
Did you mean: 

Base 64 Decoding And Sending as PDF File

former_member186851
Active Contributor
0 Kudos

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?

Accepted Solutions (1)

Accepted Solutions (1)

manoj_khavatkopp
Active Contributor

Raghu,

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 😛

package com;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
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();
try
{
int rd;
byte[] data = new byte[16384];
while ((rd = in.read(data, 0, data.length)) != -1)
buffer.write(data, 0, rd);
buffer.flush();
System.out.println(buffer);
BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(buffer.toString());
out.write(decodedBytes);
out.close();
}
catch (Exception e){
System.out.println((e.getMessage()));}
}
catch (Exception e){
System.out.println((e.getMessage()));}}
}

Br,

Manoj

former_member186851
Active Contributor
0 Kudos

Let me try this Manoj.

But the output file which will be written by the file adapter needs to be deleted somehow?

manoj_khavatkopp
Active Contributor
0 Kudos

Raghu , this code is just the desktop version you may need to re-tweak it to use it in PI and the output of this Code will be your decoded PDF .

former_member186851
Active Contributor
0 Kudos

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.:)

former_member637026
Participant
0 Kudos

Hi Raghuram,

We are having the same requirement. We have a synchronous flow, Proxy>PI>REST API. REST API is sending response xml in which one field has base64 string of PDF. PI has to convert the base64 string to PDF and place the PDF in nfsshare folder.

Could you please share the UDF code you used and step by step process in PI.

Thanks,

Answers (4)

Answers (4)

vadimklimov
Active Contributor

Hi,

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 engswee.yeoh - 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.

Regards,

Vadim

former_member186851
Active Contributor
0 Kudos

Thanks Vadim.

former_member190293
Active Contributor

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.setIgnoringElementContentWhitespace(true);
bldFactory.setNamespaceAware(true);
bldFactory.setValidating(false);
bldFactory.setAttribute("http://apache.org/xml/features/nonvalidating/load-external-dtd", 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.

former_member186851
Active Contributor
0 Kudos

Let me try this Eve,

So this code will convert the PDF in to string in XML and then I have to write it or how it works?

former_member190293
Active Contributor
0 Kudos

Hi Raghi!

This code will convert Base64-encoded string from given XML element into byte array which, in turn, will be written to output payload of your mapping. So you'll get your PDF file as binary payload after mapping.

Regards, Evgeniy.

former_member186851
Active Contributor

Thanks Eve, completed the Scenario,I replied the approach in Manoj's comment.

former_member190293
Active Contributor

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.

former_member186851
Active Contributor
0 Kudos
Eve can you please share the code if possible.

Requirement is just to decode and write the PDF as file.

Ryan-Crosby
Active Contributor

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?

Regards,

Ryan Crosby

former_member186851
Active Contributor
0 Kudos

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.