I received a requirement to merge two Smart Forms into a single output, even though they are not part of the same form. In other words, they are two separate Smart Forms, but the final printout should combine them for better user understanding.
First, I created the Smart Forms in transaction SMARTFORMS as usual. I designed each form, placed all the required elements, and completed the layout according to the specifications.
After finishing both Smart Forms, I wrote the logic in the corresponding program. In the program (using the appropriate transaction) T-Code SE38, I called each Smart Form by referencing its generated function module, as shown below.
* Data Declarations.
DATA: lv_fm_dpr TYPE rs38l_fnam,
lv_fm_inv TYPE rs38l_fnam,
lv_otf1 TYPE ssfcrescl,
lv_otf2 TYPE ssfcrescl,
lt_otf1 TYPE STANDARD TABLE OF itcoo,
lt_otf2 TYPE STANDARD TABLE OF itcoo,
lt_TLINE TYPE STANDARD TABLE OF tline,
lv_pdf_xstr1 TYPE xstring,
lv_pdf_xstr2 TYPE xstring,
lv_len TYPE i,
lt_data TYPE STANDARD TABLE OF raw255,
lv_file TYPE string,
lv_path TYPE string,
lv_file_name TYPE string,
lv_merged_xstr TYPE xstring,
lv_rc TYPE i.
* Data Declarations.
DATA: ls_ctrlop TYPE ssfctrlop,
ls_compop TYPE ssfcompop.
ls_ctrlop-no_dialog = abap_true.
ls_ctrlop-getotf = abap_true.
* Get Function Module Names of SmartForms for form 1.
CALL FUNCTION ‘SSF_FUNCTION_MODULE_NAME’
EXPORTING
formname = ‘ZTEST_SMF’
IMPORTING
fm_name = lv_fm_dpr.
* Get Function Module Names of SmartForms for form 2.
CALL FUNCTION ‘SSF_FUNCTION_MODULE_NAME’
EXPORTING
formname = ‘ZSF_TEST2’
IMPORTING
fm_name = lv_fm_inv.
* Call SmartForm 1 and capture OTF (Output text format )
CALL FUNCTION lv_fm_dpr
EXPORTING
control_parameters = ls_ctrlop
output_options = ls_compop
IMPORTING
job_output_info = lv_otf1.
lt_otf1[] = lv_otf1-otfdata.
* Convert to PDF (XSTRING)
CALL FUNCTION ‘CONVERT_OTF’
EXPORTING
format = ‘PDF’
IMPORTING
bin_file = lv_pdf_xstr1
TABLES
otf = lt_otf1
lines = lt_TLINE
EXCEPTIONS
OTHERS = 1.
* Call SmartForm 2 and capture OTF
CALL FUNCTION lv_fm_inv
EXPORTING
control_parameters = ls_ctrlop
output_options = ls_compop
IMPORTING
job_output_info = lv_otf2.
lt_otf2[] = lv_otf2-otfdata.
* Convert to PDF (XSTRING)
CALL FUNCTION ‘CONVERT_OTF’
EXPORTING
format = ‘PDF’
IMPORTING
bin_file = lv_pdf_xstr2
TABLES
otf = lt_otf2
lines = lt_TLINE
EXCEPTIONS
OTHERS = 1.
* Merge the two XSTRING PDFs
DATA(lo_merger) = NEW cl_rspo_pdf_merge( ).
lo_merger->add_document( lv_pdf_xstr1 ).
lo_merger->add_document( lv_pdf_xstr2 ).
* merging 2 documents into one
lo_merger->merge_documents(
IMPORTING
merged_document = lv_merged_xstr
rc = lv_rc ).
IF lv_rc <> 0.
MESSAGE ‘PDF merge failed. Please check input documents.’ TYPE ‘E’.
ENDIF.
* Converting XSTRING to binary table
CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY’
EXPORTING
buffer = lv_merged_xstr
IMPORTING
output_length = lv_len
TABLES
binary_tab = lt_data.
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
window_title = ‘Save Combined PDF’
default_file_name = ‘Combined_SmartForms.pdf’
default_extension = ‘pdf’
CHANGING
filename = lv_file_name
path = lv_path
fullpath = lv_file
EXCEPTIONS
OTHERS = 1.
IF lv_file IS INITIAL.
MESSAGE ‘File path not selected’ TYPE ‘I’.
EXIT.
ENDIF.
* Download Merged PDF to Frontend
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
bin_filesize = lv_len
filename = lv_file
filetype = ‘BIN’
CHANGING
data_tab = lt_data
EXCEPTIONS
OTHERS = 1.
* Open PDF after saving
CALL METHOD cl_gui_frontend_services=>execute
EXPORTING
document = lv_file
synchronous = ‘X’
EXCEPTIONS
OTHERS = 1.
After the code is executed, the system will prompt you to enter the name or path of your local device, as shown below. This allows the application to determine where the generated PDF should be saved.
Next, the system will once again prompt you to click on Print Preview for the second PDF.
After that, you will be required to select the destination path where the merged PDF should be downloaded and saved on your local device.
the final output will be generated in PDF format, where both Smart Forms—originally created as two separate documents—will be successfully combined into a single, unified PDF file.
CONCLUSION:
Upon executing the above code, you must specify the desired local system path where the output should be saved. Once the path is provided, the system will generate and download a single PDF file to your local machine. This PDF will contain both Smart Forms seamlessly merged into one consolidated document.
Thank You…
I received a requirement to merge two Smart Forms into a single output, even though they are not part of the same form. In other words, they are two separate Smart Forms, but the final printout should combine them for better user understanding.First, I created the Smart Forms in transaction SMARTFORMS as usual. I designed each form, placed all the required elements, and completed the layout according to the specifications.After finishing both Smart Forms, I wrote the logic in the corresponding program. In the program (using the appropriate transaction) T-Code SE38, I called each Smart Form by referencing its generated function module, as shown below.* Data Declarations.
DATA: lv_fm_dpr TYPE rs38l_fnam,
lv_fm_inv TYPE rs38l_fnam,
lv_otf1 TYPE ssfcrescl,
lv_otf2 TYPE ssfcrescl,
lt_otf1 TYPE STANDARD TABLE OF itcoo,
lt_otf2 TYPE STANDARD TABLE OF itcoo,
lt_TLINE TYPE STANDARD TABLE OF tline,
lv_pdf_xstr1 TYPE xstring,
lv_pdf_xstr2 TYPE xstring,
lv_len TYPE i,
lt_data TYPE STANDARD TABLE OF raw255,
lv_file TYPE string,
lv_path TYPE string,
lv_file_name TYPE string,
lv_merged_xstr TYPE xstring,
lv_rc TYPE i.
* Data Declarations.
DATA: ls_ctrlop TYPE ssfctrlop,
ls_compop TYPE ssfcompop.
ls_ctrlop-no_dialog = abap_true.
ls_ctrlop-getotf = abap_true.
* Get Function Module Names of SmartForms for form 1.
CALL FUNCTION ‘SSF_FUNCTION_MODULE_NAME’
EXPORTING
formname = ‘ZTEST_SMF’
IMPORTING
fm_name = lv_fm_dpr.
* Get Function Module Names of SmartForms for form 2.
CALL FUNCTION ‘SSF_FUNCTION_MODULE_NAME’
EXPORTING
formname = ‘ZSF_TEST2’
IMPORTING
fm_name = lv_fm_inv.
* Call SmartForm 1 and capture OTF (Output text format )
CALL FUNCTION lv_fm_dpr
EXPORTING
control_parameters = ls_ctrlop
output_options = ls_compop
IMPORTING
job_output_info = lv_otf1.
lt_otf1[] = lv_otf1-otfdata.
* Convert to PDF (XSTRING)
CALL FUNCTION ‘CONVERT_OTF’
EXPORTING
format = ‘PDF’
IMPORTING
bin_file = lv_pdf_xstr1
TABLES
otf = lt_otf1
lines = lt_TLINE
EXCEPTIONS
OTHERS = 1.
* Call SmartForm 2 and capture OTF
CALL FUNCTION lv_fm_inv
EXPORTING
control_parameters = ls_ctrlop
output_options = ls_compop
IMPORTING
job_output_info = lv_otf2.
lt_otf2[] = lv_otf2-otfdata.
* Convert to PDF (XSTRING)
CALL FUNCTION ‘CONVERT_OTF’
EXPORTING
format = ‘PDF’
IMPORTING
bin_file = lv_pdf_xstr2
TABLES
otf = lt_otf2
lines = lt_TLINE
EXCEPTIONS
OTHERS = 1.
* Merge the two XSTRING PDFs
DATA(lo_merger) = NEW cl_rspo_pdf_merge( ).
lo_merger->add_document( lv_pdf_xstr1 ).
lo_merger->add_document( lv_pdf_xstr2 ).
* merging 2 documents into one
lo_merger->merge_documents(
IMPORTING
merged_document = lv_merged_xstr
rc = lv_rc ).
IF lv_rc <> 0.
MESSAGE ‘PDF merge failed. Please check input documents.’ TYPE ‘E’.
ENDIF.
* Converting XSTRING to binary table
CALL FUNCTION ‘SCMS_XSTRING_TO_BINARY’
EXPORTING
buffer = lv_merged_xstr
IMPORTING
output_length = lv_len
TABLES
binary_tab = lt_data.
CALL METHOD cl_gui_frontend_services=>file_save_dialog
EXPORTING
window_title = ‘Save Combined PDF’
default_file_name = ‘Combined_SmartForms.pdf’
default_extension = ‘pdf’
CHANGING
filename = lv_file_name
path = lv_path
fullpath = lv_file
EXCEPTIONS
OTHERS = 1.
IF lv_file IS INITIAL.
MESSAGE ‘File path not selected’ TYPE ‘I’.
EXIT.
ENDIF.
* Download Merged PDF to Frontend
CALL METHOD cl_gui_frontend_services=>gui_download
EXPORTING
bin_filesize = lv_len
filename = lv_file
filetype = ‘BIN’
CHANGING
data_tab = lt_data
EXCEPTIONS
OTHERS = 1.
* Open PDF after saving
CALL METHOD cl_gui_frontend_services=>execute
EXPORTING
document = lv_file
synchronous = ‘X’
EXCEPTIONS
OTHERS = 1.After the code is executed, the system will prompt you to enter the name or path of your local device, as shown below. This allows the application to determine where the generated PDF should be saved.Next, the system will once again prompt you to click on Print Preview for the second PDF.After that, you will be required to select the destination path where the merged PDF should be downloaded and saved on your local device.the final output will be generated in PDF format, where both Smart Forms—originally created as two separate documents—will be successfully combined into a single, unified PDF file.CONCLUSION:Upon executing the above code, you must specify the desired local system path where the output should be saved. Once the path is provided, the system will generate and download a single PDF file to your local machine. This PDF will contain both Smart Forms seamlessly merged into one consolidated document.Thank You… Read More Technology Blog Posts by Members articles
#SAP
#SAPTechnologyblog