Requirement
There is a common requirement given by client wherein they ask to generate PDF files (output) based on the data entered in SAP WebDynpro ABAP input screen.
Data entered in Input form can be:
- Simple Text
- Formatted Text
- Attachment etc.…
This article deals with displaying
ATTACHMENTS in attachment section of output form (SAP ADOBE form) and approach to achieve it.
Please refer to below image.
Note: This document is not specific to WebDynpro ABAP and can be leveraged as far as data (Input Form + Attachment) can be supplied to methods/FM mentioned in below sections.
Challenges
- Transferring the Data from SAP WebDynpro input screen to SAP ADOBE form.
Pre-requisite
Understanding on
- WebDynpro ABAP
- ABAP
Facts
- Details (content/mime type/filename) about the attachments can be retrieved via CONTENT_NODE in case WebDynpro screen is used for uploading the attachments.
Steps
1. Generate Input content in PDF template
List/Order of Function Modules required to get ‘Input Content in PDF format’.
S.No. |
Function Module |
Description |
Important Pointers |
Code Snippet |
1 |
FP_FUNCTION_MODULE_NAME |
To get Function Module name of the PDF form/ Template |
Importing parameter for FM is 'PDF template name' |
CALL FUNCTION 'FP_FUNCTION_MODULE_NAME'
EXPORTING
i_name = FORM_TEMPLATE_NAME
IMPORTING
e_funcname = FM_NAME. |
2 |
FP_JOB_OPEN |
To open a PDF form output job |
|
Set the value in structure 'fp_outputparams' based on configuration in system.
fp_outputparams-nodialog = 'X'.
fp_outputparams-nopreview = 'X'.
fp_outputparams-dest = 'LOCL'.
fp_outputparams-device = 'PRINTER'.
fp_outputparams-reqnew = 'X'. CALL FUNCTION 'FP_JOB_OPEN'
CHANGING
ie_outputparams = fp_outputparams. |
3 |
FM name for PDF form (received from S.No.1) |
Interface to the PDF form/Template |
Importing parameter will be interface parameters, and so get all the data for the interface parameters before calling this FM. |
CALL FUNCTION fm_name
EXPORTING
/1bcdwb/docparams = fp_docparams
EXP_PAR1 = VAL1
EXP_PAR2 = VAL2
EXCEPTIONS
usage_error = 1
system_error = 2
internal_error = 3
OTHERS = 99. |
4 |
FP_JOB_CLOSE |
To close the PDF output job |
|
CALL FUNCTION 'FP_JOB_CLOSE'
IMPORTING
e_result = RESULT
EXCEPTIONS
usage_error = 1
system_error = 2
internal_error = 3
OTHERS = 99. |
5 |
RSPO_ADSP_GET_PARTLIST |
Returns the part list and the number of ADS parts |
Based on the RESULT parameter got from S.No.4, take latest SPOOL ID. |
CALL FUNCTION 'RSPO_ADSP_GET_PARTLIST'
EXPORTING
rqident = SPOOL_ID
IMPORTING
old_document = adsp_old_document
TABLES
partlist = adsp_partlist
EXCEPTIONS
OTHERS = 99. |
6 |
RSPO_ADSP_GET_PDF |
Provides PDF data of an ADS part |
LV_CONTENT is of type XSTRING and post execution of this FM, we will get PDF data in this variable.
LV_CONTENT will be used in STEP3. |
CALL FUNCTION 'RSPO_ADSP_GET_PDF'
EXPORTING
rqident = SPOOL ID
navindex = '1'
old_document = adsp_old_document
IMPORTING
pdf_data = LV_CONTENT (XSTRING)
TABLES
partlist = adsp_partlist
EXCEPTIONS
no_such_job = 1
wrong_jobtype = 2
OTHERS = 99. |
Note: In above table, all the words in BOLD/ITALIC, needs to be defined and passed as per requirement.
2. Generate Attachment content
While uploading the attachments (refer to picture on Requirement Section) you can get of below values for all the attachments:
- CONTENT (in XSTRING)
- FILENAME (in STRING)
- MIMETYPE (in STRING)
Value of above attributes will be available in CONTEXT NODE for all the attachments.
Note: This is not specific to WebDynpro ABAP, but can be used anywhere in ABAP, as far as above mentioned 3 attributes can be retrieved.
DATA ls_attachment TYPE sfpattachments.
DATA lt_attachments TYPE tfpattachments.
LOOP AT LIST_OF_ATTACHMENT INTO ATTACHMENT.
ls_attachment-data = ATTACHMENT -content.
ls_attachment-name = ATTACHMENT -filename.
ls_attachment-filename = ATTACHMENT -filename.
ls_attachment-mimetype = ATTACHMENT -mimetype.
ls_attachment-description = ATTACHMENT -filename. (This is based on user input)
ENDLOOP.
INSERT ls_attachment INTO TABLE lt_attachments.
Note: LIST_OF_ATTACHMENT internal table should have list of attachments having CONTENT/FILENAME/MIMETYPE.
In case of WebDynpro ABAP, using CONTEXT_NODE, we can fill LIST_OF_ATTACHMENT.
3. Prepare PDF Output
Merge the content received from above steps i.e.
Generate Input content in PDF template and
Generate Attachment content
DATA lv_dest TYPE rfcdest VALUE 'ADS'.
DATA lv_full_content TYPE xstring.
DATA lo_pdfobj TYPE REF TO if_fp_pdf_object.
DATA lo_fpex TYPE REF TO cx_fp_runtime.
lv_dest = cl_fp=>get_ads_connection( ).
TRY.
"Create PDF Object.
lo_pdfobj = cl_fp=>get_reference( )->create_pdf_object( connection = lv_dest ).
"Set document
lo_pdfobj->set_document( pdfdata = lv_content ).
"Set attachment.
lo_pdfobj->set_attachments( attachments = lt_attachments ).
"Execute, call ADS.
lo_pdfobj->execute( ).
"Get result..
lo_pdfobj->get_document( IMPORTING pdfdata = lv_full_content ).
CATCH cx_fp_runtime INTO lo_fpex.
ENDTRY.
After execution of above code, variable ‘LV_FULL_CONTENT’ which is of type XSTRING, will have PDF content (INPUT DATA + ATTACHMENTS).
Developer can use this content to:
- Dynamically create a PDF file and store in some on ContentServer example DMS
- Download it as PDF, by using below code
CALL METHOD cl_wd_runtime_services=>attach_file_to_response
EXPORTING
i_filename = 'test.pdf'
i_content = lv_full_content
i_mime_type = 'application/pdf'
i_in_new_window = abap_true.