cancel
Showing results for 
Search instead for 
Did you mean: 

Convert pdf to base64

maiconsbr
Explorer
0 Kudos

Hello all,

I am working on integration between Portal -> SAP API Management -> SAP Gateway and for one specific service, I need to retrieve the PDF file from Gateway. This pdf file needs to be download from portal side.

The service in Gateway is returning a pdf file(see below the response.content) and I need to convert it to base64 before send the response to Portal in json format.

I am using a python script to convert the response.content to base64, which I found being used in some other SAP policies, but the conversion is not working fine.

After decoded in portal, the pdf does not open properly. I found some differences in the content of base64 when I compare the result of this script, with the result from a REST client which converts automatically to base64.

Does anybody know how to resolve it? Or is there any other way to convert it using other scripts or policies?

Any suggestion is welcome.

Python script:

import base64

encodedfile = base64.b64encode(flow.getVariable("response.content"));"

Example of Response.Content:

"%PDF-1.4 %���� 9 0 obj << /Type /XObject /Subtype /Image /Name /I1 /Width 613 /Height 78 /BitsPerComponent 8 /ColorSpace /DeviceRGB /Length 5471 /Filter [ /FlateDecode ] >> stream x��k�� ����{���G�c���$����V�$/$\Bx��ѣG��=z��ѣG�Դ��y����[���p����5ϯyzM�<����g��^��$���>�����`���@���!�����߿�)Yr�g}��^�[������������ϯ��k��<������m+�i���O��x8-�,��ڭ......."

The final response to Portal will be like this:

{ "mime-type": "application/PDF", "file": "JVBERi0xLjQNCi/9///9DQoNCjkgMCBvYmoNCjw8DQovVHlw..." }

Many thanks in advance, Maicon.

View Entire Topic
namratharaj
Discoverer
0 Kudos

Hi Maicon, 

I am facing same issue. Please let me know if you found any solution?

maiconsbr
Explorer
0 Kudos
Hi, unfortunately we did not solve it from APIM side. After some discussion we agreed that sender applicatton should handle the pdf. So, this way we only transferred the pdf from Gateway to Portal without any transformation.