Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

BASE64 TO PDF CONVERSION ABAP

fergomes
Explorer
0 Likes
10,067

Hi all,

I'm having problems converting a base64 code to a pdf file input in AL11, after recording and downloading I realize that the file is empty/blank, help me please.

- I tried to use the following ways, but without success, the pdf file always comes blank:

CODE 1:

DATA: fic_binario TYPE xstring,
contents_tb TYPE TABLE OF sdokcntbin,
contents_st TYPE sdokcntbin,
file_length TYPE i,
flag TYPE c,
off TYPE i,
len TYPE i.

CALL FUNCTION 'SSFC_BASE64_DECODE'
EXPORTING
b64data = lv_return_string
IMPORTING
bindata = fic_binario.

TRY.
len = xstrlen( fic_binario ).
file_length = len.
WHILE flag IS INITIAL.

IF len LE 1022.
contents_st-line = fic_binario+off(len).
flag = 'X'.
ELSE.
contents_st-line = fic_binario+off(1022).
off = off + 1022.
len = len - 1022.
ENDIF.
APPEND contents_st TO contents_tb.
ENDWHILE.

DATA(lv_filename_path) = |{ c_path_al11 }{ p_idgarantia }_{ p_tpform }.pdf|.

OPEN DATASET lv_filename_path FOR OUTPUT IN BINARY MODE.

LOOP AT contents_tb ASSIGNING FIELD-SYMBOL(<contents_fs>).
TRANSFER <contents_fs> TO lv_filename_path.
ENDLOOP.


CLOSE DATASET lv_filename_path.

ENDTRY.

CODE 2:

* CALL FUNCTION 'SCMS_BASE64_DECODE_STR'
* EXPORTING
* input = lv_return_string
* IMPORTING
* output = lv_base64_xtring
* EXCEPTIONS
* failed = 1
* OTHERS = 2.
*
* IF sy-subrc EQ 0.
*
* CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
* EXPORTING
* buffer = lv_base64_xtring
* IMPORTING
* output_length = lv_length
* TABLES
* binary_tab = lt_binary
* EXCEPTIONS
* error_message = 1
* OTHERS = 2.
*
* IF sy-subrc EQ 0.
*
* CALL FUNCTION 'SCMS_BINARY_TO_STRING'
* EXPORTING
* input_length = lv_length
* IMPORTING
* text_buffer = lv_string
* TABLES
* binary_tab = lt_binary
* EXCEPTIONS
* failed = 1
* OTHERS = 2.

CODE 3:

* DATA(lv_base64_pdf) = VALUE string( ).
* DATA: lv_decodedx TYPE xstring,
* lv_bin_filesize TYPE i,
* lt_data_s TYPE solix_tab.
*
* CALL METHOD cl_http_utility=>if_http_utility~decode_x_base64
* EXPORTING
** encoded = lv_base64_pdf
* encoded = lv_return_string
* RECEIVING
* decoded = lv_decodedx.
*
* CALL METHOD cl_bcs_convert=>xstring_to_solix
* EXPORTING
* iv_xstring = lv_decodedx
* RECEIVING
* et_solix = lt_data_s.
*
* lv_bin_filesize = xstrlen( lv_decodedx ).

pdf-filled.jpgpdf-blank.jpgbase64.txt

thankful.

Hi all,

I'm having problems converting a base64 code to a pdf file input in AL11, after recording and downloading I realize that the file is empty/blank, help me please.

- I tried to use the following ways, but without success, the pdf file always comes blank:

CODE 1:

DATA: fic_binario TYPE xstring,
contents_tb TYPE TABLE OF sdokcntbin,
contents_st TYPE sdokcntbin,
file_length TYPE i,
flag TYPE c,
off TYPE i,
len TYPE i.

CALL FUNCTION 'SSFC_BASE64_DECODE'
EXPORTING
b64data = lv_return_string
IMPORTING
bindata = fic_binario.

TRY.
len = xstrlen( fic_binario ).
file_length = len.
WHILE flag IS INITIAL.

IF len LE 1022.
contents_st-line = fic_binario+off(len).
flag = 'X'.
ELSE.
contents_st-line = fic_binario+off(1022).
off = off + 1022.
len = len - 1022.
ENDIF.
APPEND contents_st TO contents_tb.
ENDWHILE.

DATA(lv_filename_path) = |{ c_path_al11 }{ p_idgarantia }_{ p_tpform }.pdf|.

OPEN DATASET lv_filename_path FOR OUTPUT IN BINARY MODE.

LOOP AT contents_tb ASSIGNING FIELD-SYMBOL(<contents_fs>).
TRANSFER <contents_fs> TO lv_filename_path.
ENDLOOP.


CLOSE DATASET lv_filename_path.

ENDTRY.

CODE 2:

* CALL FUNCTION 'SCMS_BASE64_DECODE_STR'
* EXPORTING
* input = lv_return_string
* IMPORTING
* output = lv_base64_xtring
* EXCEPTIONS
* failed = 1
* OTHERS = 2.
*
* IF sy-subrc EQ 0.
*
* CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
* EXPORTING
* buffer = lv_base64_xtring
* IMPORTING
* output_length = lv_length
* TABLES
* binary_tab = lt_binary
* EXCEPTIONS
* error_message = 1
* OTHERS = 2.
*
* IF sy-subrc EQ 0.
*
* CALL FUNCTION 'SCMS_BINARY_TO_STRING'
* EXPORTING
* input_length = lv_length
* IMPORTING
* text_buffer = lv_string
* TABLES
* binary_tab = lt_binary
* EXCEPTIONS
* failed = 1
* OTHERS = 2.

CODE 3:

* DATA(lv_base64_pdf) = VALUE string( ).
* DATA: lv_decodedx TYPE xstring,
* lv_bin_filesize TYPE i,
* lt_data_s TYPE solix_tab.
*
* CALL METHOD cl_http_utility=>if_http_utility~decode_x_base64
* EXPORTING
** encoded = lv_base64_pdf
* encoded = lv_return_string
* RECEIVING
* decoded = lv_decodedx.
*
* CALL METHOD cl_bcs_convert=>xstring_to_solix
* EXPORTING
* iv_xstring = lv_decodedx
* RECEIVING
* et_solix = lt_data_s.
*
* lv_bin_filesize = xstrlen( lv_decodedx ).

pdf-filled.jpgpdf-blank.jpgbase64.txt

thankful.

4 REPLIES 4
Read only

Sandra_Rossi
Active Contributor
0 Likes
8,557

No need to post dead code, that will just confuse people.

Read only

Sandra_Rossi
Active Contributor
8,557

Try to simplify your code. This way for instance:

DATA: xstring TYPE xstring, base64_string TYPE string.

base64_string = 'YGE='.
CALL TRANSFORMATION id SOURCE whatever = base64_string RESULT whatever = xstring.

OPEN DATASET path_file FOR OUTPUT IN BINARY MODE.
TRANSFER xstring TO path_file.
CLOSE DATASET path_file.

Don't forget to handle the errors (SY-SUBRC, exceptions), and debug your code.

Read only

0 Likes
8,557

I'm having the same blank file issue.

Read only

8,557

As I told you, "Don't forget to handle the errors (SY-SUBRC, exceptions), and debug your code.".

NB: my standalone program above works for sure, provided that you use a correct file path.