In this article, you will learn how to get location of a pdf file, how to convert pdf to xstring in abap and how to use GUI_UPLOAD Function Module to perform pdf upload in ABAP.
To Upload a PDF using ABAP Program, you need to get the location of the file and the pdf file. You need to convert this file in XSTRING. In the code shown below, we have used GUI_UPLOAD Function module to get data in an Internal Table.
Program to Upload PDF in ABAP
parameters: p_file type string lower case. data: gs_store_file type z1127582_upload,"z1127582_upload is a table to store PDF file and other relevant data gt_content type standard table of tdline, len type i, xstr_content type xstring. DATA: w_filename TYPE string, " File name w_length TYPE i, lt_file_table TYPE filetable, lv_filelength TYPE i, lv_rc TYPE i, lv_filename TYPE string, w_pdf_data TYPE xstring, lt_rawtab TYPE TABLE OF char255, w_pdf_file TYPE string, * Create PDF Object using destination 'ADS' (<-- this is how it is * defined in SM59) lo_pdfobj TYPE REF TO if_fp_pdf_object VALUE IS INITIAL, xslt_message TYPE string, exc TYPE REF TO cx_root, fs_per_info TYPE ZWD_PER_INFO. AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file. PERFORM f4_help_for_file. start-of-selection. "Upload the file to Internal Table call function 'GUI_UPLOAD' exporting filename = p_file filetype = 'BIN' importing filelength = len tables data_tab = gt_content 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 others = 17. if sy-subrc <> 0. message 'Unable to upload file' type 'E'. endif. "Convert binary ITAB to xstring call function 'SCMS_BINARY_TO_XSTRING' exporting input_length = len * FIRST_LINE = 0 * LAST_LINE = 0 importing buffer = xstr_content tables binary_tab = gt_content exceptions failed = 1 others = 2 . if sy-subrc <> 0. message 'Unable to convert binary to xstring' type 'E'. endif. clear gs_store_file. gs_store_file-filename = p_file. gs_store_file-file_content = xstr_content. "Insert file into table insert z1127582_upload from gs_store_file. if sy-subrc is initial. message 'Successfully uploaded' type 'S'. else. message 'Failed to upload' type 'E'. endif. INCLUDE z1127582_test2_f4_help_for_f01. FORM f4_help_for_file . CALL METHOD cl_gui_frontend_services=>file_open_dialog CHANGING file_table = lt_file_table rc = lv_rc * USER_ACTION = * FILE_ENCODING = EXCEPTIONS file_open_dialog_failed = 1 cntl_error = 2 error_no_gui = 3 not_supported_by_gui = 4 OTHERS = 5. IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. * READ TABLE lt_file_table * INTO lv_filename * INDEX 1. READ TABLE lt_file_table INTO p_file INDEX 1. IF sy-subrc EQ 0. lv_filename = p_file. ENDIF. cl_gui_frontend_services=>gui_upload( EXPORTING filename = lv_filename filetype = 'BIN' "Binary IMPORTING filelength = lv_filelength CHANGING data_tab = lt_rawtab 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 ). ENDFORM.
Initially, we have defined variables in three parts:
- Parameter: p_file that is of type string and will store location of file
- gs_store_file, gt_content, len, xstr_content: data related to file i.e. where file will be store, how the content will be stored, length of the file and type of content.
- Third set of variables deals with the PDF file content detail. These details are the one that is stored in a table and whose type internal table is created above “gs_store_file”.
Function Module used:
What exactly to store in the table via upload in ABAP:
- PDF file location
- PDF file name
- File type
- File X-STRING Data [This is the actual file that is converted in x-string]
Later on we have called different Function Modules. We have added respective comment in the code itself.