cancel
Showing results for 
Search instead for 
Did you mean: 

How to convert byte-array within XML-Response into pdf and save to filesystem

Former Member
0 Kudos

Hi folks,

I have scenario where I need to create a PDF-file from a given xml-request. The request get postet to the PI using a proxy object. Then I perform several conversion task in PI and as a response I get a byte-array in response.

When I take this byte-array and convert it useing a base64 decode on the internet, I get the required pdf-file. But I need to save this xml-byte-stream as a pdf-file useing PI The message type looks as follow:

How can I archive to save this PDF_Response as real pdf-file?

thanks in advance

regards

Christian

Accepted Solutions (1)

Accepted Solutions (1)

former_member181985
Active Contributor
0 Kudos

Hi Christian,

If I have understood your requirement correctly, you should use a java mapping to read the base64 binary data from PDF_Response field from input XML and then decode to binary and write it to output stream (TransformationOutput).

That's all..

Regards,

Praveen Gujjeti

Former Member
0 Kudos

Hi Praven,

yes that's correct.

I allready got the response as byte-stream within the mapping, because this is the response from a another server.

Now the two remaining questions are:

  • Do I need to transform this byte-stream useing base64
  • How can I send this data to the receiver channel and the convert it to pdf

regards

Christian

former_member181985
Active Contributor
0 Kudos

Hi,

In java mapping,

read the base64 textual data from 'PDF_Response' field of input XML --> decode it to binary stream (data) and then write it to output stream (TransformationOutput).


In the receiver file channel give some test.pdf as file name. And then check the generated file with acrobat reader

Regards,

Praveen Gujjeti

former_member189420
Active Participant
0 Kudos

Hi Christian,

Once you have the Byte[] data you can call the file system operations directly from the mapping and save it the location where you want it to be.

Something like this: (within an UDF):


File f = new File("\\XXX\BBB\response.pdf";

FileOutputStream fos = new FileOutputStream(f);

fos.write(byte[]);

fos.close();

Place appropriate try{}catch{} block.

Hope it helps!

Cheers,

Anand Patil

former_member181985
Active Contributor
0 Kudos

Hi,

You could also test from Operation mapping for the pdf file, without having to build an end to end scenario. Check my blog:

Regards,

Praveen Gujjeti

Former Member
0 Kudos

Hi Anand,

unfortunately I can't use this option. On the receiver side there is a ftp server that I need to pass the pdf-file to. But anyway thanks for the input!

regards

Christian

Former Member
0 Kudos

Hi Praveen

so by going for this solution, do I need another message type with just the pdf-payload as type? Currently the message types consits of the material-number (as filename) and the appropriate byte-stream.

Regards

Christian

former_member189420
Active Participant
0 Kudos

Hi Christian,

In that case you can directly use a java mapping to output the stream as Praveen suggested and then message type should not matter.

Regards,

Anand

former_member181985
Active Contributor
0 Kudos

Hi Christian,

You can use any message type (service interface) with java mapping for any type of data (textual or binary)

Regards,

Praveen Gujjeti

Former Member
0 Kudos

Hi,

ok but now I have one question left. I have implemented the source code within a UDF as I need to pass several values to the funtion during mapping execution.

Can I use the TransformationOutput also within the UDF? Currently I have done all the required steps and performed the decryption. The byte-stream looks good  but when I try to pass it as output I get the following error:

Do I need to convert the UDF to a real java function? Can I then pass several values to this function?

regards

Christian

former_member181985
Active Contributor
0 Kudos

Hi Christian,

You should use a pure java mapping with decoding code etc.., not the message mapping with UDF function with decoding logic

Regards,

Praveen Gujjeti

former_member189420
Active Participant
0 Kudos

Hi Christian,

You can directly convert the Base64Decode to String and pass it to the output message type using your UDF (as you are doing some other conversions...) Then you can use a simple java mapping to transfer the payload to outputstream in which you can convert it back to stream. This would require 2 message mappings in your operation mapping. Just a thought!

Cheers,

Anand

Former Member
0 Kudos

Hi there,

you were completly wright with the pure java-mapping. I did this and it worked in the first time. But the I made a change wihin the mapping code and re-deployed the jar file. Now I'm getting the below error:

Any clue how this error can be resolved? As I set the mapping worked fine for the first time...

regards

Christian

former_member189420
Active Participant
0 Kudos

Hi Christian,

The codecBase64 class from org.apache.commons is missing in your jar extract/java mapping jar. You need to export your java mapping class along with your org.apache.commons.* jar files.

Hope it helps!

Cheers,

Anand

former_member181985
Active Contributor
0 Kudos

Hi Christian,

I think you are missing apache library as an imported archive

BR,

Praveen Gujjeti

Former Member
0 Kudos

Once again you were wright! I deleted the second imported archive in the ESR. Although the class is imported and included in my jar file it also needs to be imported as imported archive in the ESR... I imported the archive and now everything is working as expected.

Thanks a lot!

regards

Christian

Answers (0)