cancel
Showing results for 
Search instead for 
Did you mean: 

How to read a pdf file from desktop to internal table in sap abap

shubh_das
Explorer
0 Kudos

I want to read a pdf file from presentation layer to internal table and do some operations. please suggest !!!

Sandra_Rossi
Active Contributor
0 Kudos
This question has been asked many times in the forum. I suggest that you search before posting.
View Entire Topic
hamzamujeebk
Explorer
0 Kudos

Certainly! To read a PDF file from the presentation layer (local file system) into an internal table in SAP ABAP, you can follow these steps:

1. **Upload PDF File to SAP Presentation Server**:
- Use the transaction `CG3Z` or similar to upload the PDF file from your local system to the presentation server (the application server of SAP system).

2. **Read PDF File and Populate Internal Table**:
- Use the function module `GUI_UPLOAD` to read the content of the PDF file into an internal table. Ensure that the internal table structure matches the content of your PDF file.
- Below is an example code snippet:

DATA: lt_pdf_content TYPE TABLE OF char255,
lv_file_name TYPE string,
lv_path TYPE string,
lv_rc TYPE i.

lv_file_name = 'YOUR_PDF_FILE.pdf'. "Replace with your PDF file name
lv_path = 'YOUR_PATH/'. "Replace with the path where the PDF file is located

CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = lv_path
filetype = 'BIN'
TABLES
data_tab = lt_pdf_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
file_not_found = 17
dataprovider_exception = 18
control_flush_error = 19
OTHERS = 20.

IF sy-subrc EQ 0.
WRITE: 'PDF file uploaded successfully.'.
ELSE.
WRITE: 'Error while uploading PDF file:', sy-subrc.
ENDIF.

 

3. **Perform Operations on Internal Table**:
- Once the PDF content is loaded into the internal table (`lt_pdf_content` in the example above), you can perform any required operations on the data.

4. **Processing PDF Content**:
- PDF content might not be in a structured format, so you may need to parse it accordingly. This might involve splitting the content, searching for specific patterns, or using third-party libraries for PDF parsing if necessary.

5. **Error Handling**:
- Ensure to handle any exceptions that might occur during the file upload process (`sy-subrc` values).

By following these steps, you should be able to read the content of a PDF file from the presentation layer into an internal table in SAP ABAP and perform necessary operations.

Sandra_Rossi
Active Contributor
0 Kudos

Please use the buttons "..." and "</>" to make the code easy to read.

e.g.

DATA: lt_pdf_content TYPE TABLE OF char255,
      lv_file_name TYPE string,
      lv_path TYPE string,
      lv_rc TYPE i.

lv_file_name = 'YOUR_PDF_FILE.pdf'. "Replace with your PDF file name
lv_path = 'YOUR_PATH/'. "Replace with the path where the PDF file is located

CALL FUNCTION 'GUI_UPLOAD'
  EXPORTING
    filename = lv_path
    filetype = 'BIN'
  TABLES
    data_tab = lt_pdf_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
    file_not_found = 17
    dataprovider_exception = 18
    control_flush_error = 19
    OTHERS = 20.

IF sy-subrc EQ 0.
  WRITE: 'PDF file uploaded successfully.'.
ELSE.
  WRITE: 'Error while uploading PDF file:', sy-subrc.
ENDIF.
Sandra_Rossi
Active Contributor
0 Kudos
Is it an answer generated by ChatGPT?
Sandra_Rossi
Active Contributor
0 Kudos
Calling the function module GUI_UPLOAD is obsolete. Instead, you must use the method CL_GUI_FRONTEND_SERVICES=>GUI_UPLOAD.