转载:http://www.saptechnical.com/Tutorials/Smartforms/OTF2PDF.htm
*&---------------------------------------------------------------------*
*& Report ZTX_PRACTISE1
*&
*&---------------------------------------------------------------------*
REPORT ZTX_PRACTISE1.
* Converting Smart Forms output to PDF format
" Variable declarations
DATA:
w_from_name TYPE tdsfname VALUE 'ZTEST_F1', " Name of Smart Forms
w_fmodule TYPE rs38l_fnam, " Name of Function Module
w_cparam TYPE ssfctrlop,
w_outoptions TYPE ssfcompop,
w_bin_filesize TYPE i, " Binary File size
w_file_name TYPE string,
w_file_path TYPE string,
w_full_path TYPE string.
" Internal table declaration
" Hold OTF data
DATA: t_otf TYPE itcoo OCCURS 0 WITH HEADER LINE,
" Hold OTF data record form Smart Forms
t_otf_from_fm TYPE ssfcrescl,
" Hold data from FM convert_off
t_pdf_tab LIKE tline OCCURS 0 WITH HEADER LINE.
" This function module is used to retrive the name of function
" module generated when the Smart Forms is activated
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING
formname = w_from_name
IMPORTING
fm_name = w_fmodule
EXCEPTIONS
no_form = 1
no_function_module = 2
OTHERS = 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
" Calling the Smart Forms using the function module above
" Get_OTF parameter in the Control_Parameters is get the OTF
" format of the output
w_cparam-no_dialog = 'X'.
w_cparam-preview = space. " Suppressing dialog box for print preview
w_cparam-getotf = 'X'.
" Print name to be used is provided in the export parameter
" OUTPUT_OPTIONS
w_outoptions-tddest = 'LP01'.
CALL FUNCTION w_fmodule
EXPORTING
control_parameters = w_cparam
output_options = w_outoptions
IMPORTING
job_output_info = t_otf_from_fm
EXCEPTIONS
formatting_error = 1
internal_erroe = 2
send_error = 3
user_canceled = 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.
t_otf[] = t_otf_from_fm-otfdata[].
" Function Module Convert_otf is used to convert the OTF to PDF
CALL FUNCTION 'CONVERT_OTF'
EXPORTING
format = 'PDF'
max_linewidth = 132
IMPORTING
bin_filesize = w_bin_filesize
TABLES
otf = t_otf
lines = t_pdf_tab
EXCEPTIONS
err_max_linewidth = 1
err_format = 2
err_conv_not_possible = 3
err_bad_otf = 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.
" To display file Save dialog window
CALL METHOD cl_gui_frontend_services=>file_save_dialog
CHANGING
filename = w_file_name
path = w_file_path
fullpath = w_full_path
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
not_supported_by_gui = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
" Use FM GUI_DOWNLOAD to download the generated PDF file onto
" the presentation server
CALL FUNCTION 'GUI_DOWNLOAD'
EXPORTING
bin_filesize = w_bin_filesize
filename = w_full_path
filetype = 'BIN'
TABLES
data_tab = t_pdf_tab.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.