Wednesday, February 9, 2011

Uploading a PDF file in an Adobe Form offline scenario

This document provides the idea of how to upload the PDF file data into SAP. This document is extension to document ‘how to send the PDF file as an attachment in interactive offline scenario’. Once the selected candidate fills the personnel information form and sends it back to HR, they update the data in SAP.

To extract the data from the PDF file we should do the following steps.

1. Upload the PDF file using Function Module ‘GUI_UPLOAD’

2. Instantiate the PDF object and assign the PDF file to this PDF object

  • Get the reference to form processing (FP) class using the interface IF_FP.

  • Get the reference of PDF object class using interface IF_FP_PDF_OBJECT.

  • Create the PDF object using the method ‘CREATE_PDF_OBJECT’ defined in the interface IF_FP

  • Set our PDF in the created PDF object using method SET_DOCUMENT of the interface ‘IF_FP_PDF_OBJECT’.

  • Extract the data from PDF object using the method ‘SET_EXTRACTDATA’ of the interface IF_FP_PDF_OBJECT.

  • Call the Adobe Document Service (ADS) using the method EXECUTE defined in the interface ‘IF_FP_PDF_OBJECT’.

3. Extract the data from PDF Object by method called GET_DATA defined in the interface’ IF_FP_PDF_OBJECT’.

4. The extracted data is in XML format. We have to convert XML data to ABAP internal table using the Function Module ‘SMUM_XML_PARSE’.

Write the following code:

REPORT  zpersonnel_02.
SELECTION-SCREEN BEGIN OF BLOCK b1.
PARAMETERS:
p_file LIKE rlgrap-filename " File path
OBLIGATORY.
SELECTION-SCREEN END OF BLOCK b1.
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.
PERFORM get_pdf_data.
PERFORM get_pdf_instantiate.
PERFORM extract_data.
*&----------------------------------------------------------------*
*& Form F4_HELP_FOR_FILE
*&----------------------------------------------------------------*
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. " F4_HELP_FOR_FILE
*&----------------------------------------------------------------*
*& Form GET_PDF_INSTANTIATE
*&----------------------------------------------------------------*
form GET_PDF_INSTANTIATE .
* Get FP reference
DATA: lo_fp TYPE REF TO if_fp VALUE IS INITIAL.
lo_fp = cl_fp=>get_reference( ).
* For handling exceptions
DATA: lo_fpex TYPE REF TO cx_fp_runtime VALUE IS INITIAL.
TRY.
lo_pdfobj = lo_fp->create_pdf_object( connection = 'ADS' ).
* Set document
lo_pdfobj->set_document(
EXPORTING
pdfdata = w_pdf_data ).
* Tell PDF object to extract data
lo_pdfobj->set_extractdata( ).
* Execute the call to ADS
lo_pdfobj->execute( ).
CATCH cx_fp_runtime_system INTO exc.
xslt_message = exc->get_text( ).
ENDTRY.
endform. " GET_PDF_INSTANTIATE
*&----------------------------------------------------------------*
*& Form EXTRACT_DATA
*&----------------------------------------------------------------*

form EXTRACT_DATA .
DATA: xml_data TYPE xstring,
t_return type standard table of bapiret2,
t_per_info type standard table of smum_xmltb,
lfs_per_info type smum_xmltb.
lo_pdfobj->get_data(
IMPORTING
formdata = xml_data ).
CALL FUNCTION 'SMUM_XML_PARSE'
EXPORTING
xml_input = xml_data
tables
xml_table = t_per_info
return = t_return
.
  LOOP AT t_per_info INTO lfs_per_info.
CASE lfs_per_info-cname.
WHEN 'FNAME'.
fs_per_info-FNAME = lfs_per_info-cvalue.
WHEN 'LNAME'.
fs_per_info-lname = lfs_per_info-cvalue.
WHEN 'DOB'.
REPLACE ALL OCCURRENCES OF '-' IN lfs_per_info-cvalue WITH space.
CONDENSE lfs_per_info-cvalue.
fs_per_info-DOB = lfs_per_info-cvalue.
WHEN 'TEXP'.
fs_per_info-TEXP = lfs_per_info-cvalue.
WHEN 'CEMPLOYER'.
fs_per_info-CEMPLOYER = lfs_per_info-cvalue.
WHEN 'CSALARY'.
fs_per_info-CSALARY = lfs_per_info-cvalue.
WHEN 'ESALARY'.
fs_per_info-ESALARY = lfs_per_info-cvalue.
WHEN OTHERS.
ENDCASE.
ENDLOOP.
fs_per_info-WAERS = 'INR'.
INSERT INTO ZWD_PER_INFO VALUES fs_per_info.
endform.                    " EXTRACT_DATA
*&----------------------------------------------------------------*
*& Form GET_PDF_DATA
*&----------------------------------------------------------------*
form GET_PDF_DATA .
CALL FUNCTION 'SCMS_BINARY_TO_XSTRING'
EXPORTING
input_length = lv_filelength
* FIRST_LINE = 0
* LAST_LINE = 0
IMPORTING
BUFFER = w_pdf_data
tables
binary_tab = lt_rawtab
* EXCEPTIONS
* FAILED = 1
* OTHERS = 2
.
IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
* WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
endform.                    " GET_PDF_DATA  

Now execute the program.

Upload the PDF file.

Press execute button.

Now the data will be stored in database table.

Note: Create table with above structure.

No comments:

Tutorials on SAP-ABAP

Adobe Interactive Forms Tutorials

Business Server Pages (BSP)

Userexits/BADIs

Web Dynpro for ABAP (Step by step procedure for web dynpro,Tutorials on Web Dynpro,)

ALV Tutorials

Blog Archive

goodsites