Skip to Content

The case of cl_http_utility=>decode_x_base64

Hi,

Lately there was a thread of converting BASE 64 TO PDF .

I was intrigued so I decided to do some tests (We might get any kind of data this way) .

Using Java I generated a Base64 text file (this will be used as input ) .

/wp-content/uploads/2015/01/screenshot_01_631153.png

This is how the output file looks like (1,060,588 byte long string)

/wp-content/uploads/2015/01/screenshot_02_631158.png

Time for some abap code:

Program steps ( Y_R_EITAN_TEST_31_10 ) :

/wp-content/uploads/2015/01/screenshot_01_631153.png

– Using cl_gui_frontend_services=>gui_upload to upload the text file (C:\temp\An Easy Reference for ALV Grid Control.pdf.txt).

– Using cl_http_utility=>if_http_utility~decode_x_base64 to decode the data the output from this method is xstring .

– CALL METHOD cl_gui_frontend_services=>gui_download to write the xstring as PDF (C:\temp\An Easy Reference for ALV Grid Control.lcl.pdf) .

– Use OPEN DATASET BINARY MODE to write the xstring as PDF to a shared folder (\\server\<some shared folder>\An Easy Reference for ALV Grid Control.shr.pdf)

The new PDF files can be opened with no problems using adobe reader

Comparing the files using MD5 tell me that the file downloaded using cl_gui_frontend_services=>gui_download is not identical to the original !!!!

/wp-content/uploads/2015/01/screenshot_02_631158.png

So I used a binary editor to compare:

/wp-content/uploads/2015/01/screenshot_04_631160.png

There is some extra bytes in the new file .

This does not interfere in this case but might cause a problem with other file formats and it is annoying…(Any idea ???? )

This all for now.

regards.

Thanks to the sharp eyes of Tomas Buryanek the problem with cl_gui_frontend_services=>gui_download was solved (My bug…)

We need to send to cl_gui_frontend_services=>gui_download the original file size (p_filsiz).

/wp-content/uploads/2015/01/screenshot_01_631153.png

7 Comments
You must be Logged on to comment or reply to a post.
    • Hi,

      I have done a test using “OPEN DATASET BINARY MODE” and this produce an identical twin to the original .

      This leaves cl_gui_frontend_services=>gui_download or ME 😛 (I hope that this is the case) as main suspects…..

      Program Y_R_EITAN_TEST_31_10 was updated to include the whole show…

      Regards.

  • Can anyone please share the code for Application server? We are using the same functionality but my file at application server is not converted to PDF. It’s still in Binary format.

    • Hi,

      Here is some abap.

      REPORT y_r_eitan_test_31_10 .

      TYPE-POOLS: abap .

      *———————————————————————-*
      SELECTION-SCREEN BEGIN OF BLOCK block06 WITH FRAME .

      PARAMETERS: p_path_1 TYPE pathextern OBLIGATORY
      DEFAULT ‘C:\temp\An Easy Reference for ALV Grid Control.pdf.txt’ .

      PARAMETERS: p_path_2 TYPE pathextern OBLIGATORY
      DEFAULT ‘C:\temp\An Easy Reference for ALV Grid Control.lcl.pdf’ .

      PARAMETERS: p_filsiz TYPE epsfilsiz DEFAULT ‘795439’ .

      PARAMETERS: p_path_3 TYPE pathextern OBLIGATORY
      DEFAULT ‘\\server\SomeSharedFolder>\An Easy Reference for ALV Grid Control.shr.pdf’ .

      SELECTION-SCREEN END OF BLOCK block06 .
      *———————————————————————-*

      INITIALIZATION.
      PERFORM at_initialization .

      START-OF-SELECTION.
      PERFORM at_start_of_selection .

      *———————————————————————-*
      FORM at_initialization .

      DATA: it_seltexts TYPE TABLE OF rsseltexts .
      DATA: st_seltexts LIKE LINE OF it_seltexts .

      st_seltexts-name = ‘P_PATH_1’ .
      st_seltexts-kind = ‘P’ .
      st_seltexts-text = ‘Input path’ .
      APPEND st_seltexts TO it_seltexts .

      st_seltexts-name = ‘P_PATH_2’ .
      st_seltexts-kind = ‘P’ .
      st_seltexts-text = ‘Output Path (local drive)’ .
      APPEND st_seltexts TO it_seltexts .

      st_seltexts-name = ‘P_FILSIZ’ .
      st_seltexts-kind = ‘P’ .
      st_seltexts-text = ‘Original file size’ .
      APPEND st_seltexts TO it_seltexts .

      st_seltexts-name = ‘P_PATH_3’ .
      st_seltexts-kind = ‘P’ .
      st_seltexts-text = ‘Output Path (shared folder)’ .
      APPEND st_seltexts TO it_seltexts .

      CALL FUNCTION ‘SELECTION_TEXTS_MODIFY’
      EXPORTING
      program = sy-cprog
      TABLES
      seltexts = it_seltexts
      EXCEPTIONS
      program_not_found = 1
      program_cannot_be_generated = 2
      OTHERS = 3.

      ENDFORM . “at_initialization
      *———————————————————————-*
      FORM at_start_of_selection .

      DATA: filename TYPE string .

      DATA: it_soli TYPE soli_tab .
      DATA: filelength TYPE i .

      filename = p_path_1 .

      CALL METHOD cl_gui_frontend_services=>gui_upload
      EXPORTING
      filename = filename
      filetype = ‘ASC’
      has_field_separator = abap_false
      * header_length = 0
      read_by_line = abap_false
      * dat_mode = SPACE
      * codepage = SPACE
      * ignore_cerr = ABAP_TRUE
      * replacement = ‘#’
      * virus_scan_profile =
      IMPORTING
      filelength = filelength
      * header =
      CHANGING
      data_tab = it_soli ” it_string “it_soli
      EXCEPTIONS
      file_open_error = 1
      file_read_error = 2
      no_batch = 3
      gui_refuse_filetransfer = 4
      invalid_type = 5
      no_authority = 6
      unknown_error = 7
      bad_data_format = 8
      header_not_allowed = 9
      separator_not_allowed = 10
      header_too_long = 11
      unknown_dp_error = 12
      access_denied = 13
      dp_out_of_memory = 14
      disk_full = 15
      dp_timeout = 16
      not_supported_by_gui = 17
      error_no_gui = 18
      OTHERS = 19
      .

      IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ENDIF.

      DATA: encoded TYPE string .

      CALL METHOD cl_bcs_convert=>txt_to_string
      EXPORTING
      iv_size = filelength
      it_soli = it_soli
      RECEIVING
      ev_string = encoded.

      DATA: decoded TYPE xstring .

      CALL METHOD cl_http_utility=>if_http_utility~decode_x_base64
      EXPORTING
      encoded = encoded
      RECEIVING
      decoded = decoded.

      DATA: it_solix TYPE solix_tab .

      CALL METHOD cl_bcs_convert=>xstring_to_solix
      EXPORTING
      iv_xstring = decoded
      RECEIVING
      et_solix = it_solix.

      filename = p_path_2 .

      DATA: bin_filesize TYPE i .

      bin_filesize = p_filsiz .” The original file size

      CALL METHOD cl_gui_frontend_services=>gui_download
      EXPORTING
      bin_filesize = bin_filesize
      filename = filename
      filetype = ‘BIN’
      trunc_trailing_blanks = abap_false
      CHANGING
      data_tab = it_solix
      EXCEPTIONS
      file_write_error = 1
      no_batch = 2
      gui_refuse_filetransfer = 3
      invalid_type = 4
      no_authority = 5
      unknown_error = 6
      header_not_allowed = 7
      separator_not_allowed = 8
      filesize_not_allowed = 9
      header_too_long = 10
      dp_error_create = 11
      dp_error_send = 12
      dp_error_write = 13
      unknown_dp_error = 14
      access_denied = 15
      dp_out_of_memory = 16
      disk_full = 17
      dp_timeout = 18
      file_not_found = 19
      dataprovider_exception = 20
      control_flush_error = 21
      not_supported_by_gui = 22
      error_no_gui = 23
      OTHERS = 24.

      IF sy-subrc NE 0 .
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
      ENDIF.

      DATA: mess TYPE string .

      filename = p_path_3 .

      OPEN DATASET filename FOR OUTPUT IN BINARY MODE MESSAGE mess .

      IF sy-subrc NE 0.
      MESSAGE e306(f7) WITH filename .
      RETURN .
      ENDIF.

      TRANSFER decoded TO filename NO END OF LINE .

      CLOSE DATASET filename .

      MESSAGE s640(ms) WITH filename .

      ENDFORM. “at_start_of_selection
      *———————————————————————-*

      *Selection texts
      *———————————————————-
      * P_FILSIZ D .
      * P_PATH_1 D .
      * P_PATH_2 D .
      * P_PATH_3 D .

      *Messages
      *———————————————————-
      *
      * Message class: F7
      *306 File & could not be opened
      *
      * Message class: MS
      *640 Successfully exported to & file

      ———————————————————————————-
      Extracted by Mass Download version 1.5.2 – E.G.Mellodew. 1998-2018. Sap Release 701