This code will generate a PDF based on a smartform and will encrypt it with a password.
Some prerequisites are necessary:
- It is necessary to upload the jar (available here) to the application server either using the server filesystem or using the tcode CG3Z.
- Create a custom command under SM69 (Fill in the correct path to the jar file)
Example source code:
data: l_formulario type tdsfname,
l_fun type rs38l_fnam,
v_language type sflangu,
v_e_devtype type rspoptype,
st_output_options type ssfcompop,
st_control_parameters type ssfctrlop,
st_document_output_info type ssfcrespd,
st_job_output_info type ssfcrescl,
st_job_output_options type ssfcresop,
otf_data type tsfotf,
gd_buffer type string,
v_bin_filesize type i,
it_docs type standard table of docs,
it_lines type standard table of tline,
wa_lines like line of it_lines,
l_file type string,
it_params type table of btcxpgpar,
wa_params like line of it_params,
l_cmd like sxpgcolist-name,
unique_name type sysuuid_c.
v_language = sy-langu.
CALL FUNCTION 'SSF_GET_DEVICE_TYPE'
EXPORTING
i_language = v_language
i_application = 'SAPDEFAULT'
IMPORTING
e_devtype = v_e_devtype.
st_output_options-tdprinter = v_e_devtype.
st_control_parameters-no_dialog = 'X'.
st_control_parameters-getotf = 'X'.
l_formulario = 'Z_TESTE_AJV'.
CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING
formname = l_formulario
IMPORTING
fm_name = l_fun
EXCEPTIONS
no_form = 1
no_function_module = 2
OTHERS = 3.
CALL FUNCTION l_fun
EXPORTING
control_parameters = st_control_parameters
output_options = st_output_options
IMPORTING
document_output_info = st_document_output_info
job_output_info = st_job_output_info
job_output_options = st_job_output_options
EXCEPTIONS
formatting_error = 1
internal_error = 2
send_error = 3
user_canceled = 4
OTHERS = 5.
otf_data = st_job_output_info-otfdata.
CALL FUNCTION 'CONVERT_OTF_2_PDF'
IMPORTING
bin_filesize = v_bin_filesize
TABLES
otf = otf_data
doctab_archive = it_docs
lines = it_lines
EXCEPTIONS
err_conv_not_possible = 1
err_otf_mc_noendmarker = 2
OTHERS = 3.
CALL FUNCTION 'SYSTEM_UUID_C_CREATE'
IMPORTING
UUID = unique_name.
CONCATENATE 'D:\usr\sap\DA1\DVEBMGS00\work\' unique_name '.PDF' INTO l_file.
OPEN DATASET l_file FOR OUTPUT IN BINARY MODE .
IF sy-subrc = 0 .
LOOP AT it_lines into wa_lines.
TRANSFER wa_lines TO l_file .
ENDLOOP.
CLOSE DATASET l_file .
" password <space> file
CONCATENATE '12345' l_file into wa_params SEPARATED BY space.
l_cmd = 'Z_PDF_ENCRYPT'.
CALL FUNCTION 'SXPG_COMMAND_EXECUTE'
EXPORTING
COMMANDNAME = l_cmd
ADDITIONAL_PARAMETERS = wa_params
EXCEPTIONS
OTHERS = 15.
IF SY-SUBRC = 0.
close dataset l_file.
OPEN DATASET l_file FOR INPUT IN BINARY MODE.
IF sy-subrc EQ 0.
refresh it_lines.
DO.
clear wa_lines.
READ DATASET l_file INTO wa_lines.
IF sy-subrc = 0.
append wa_lines to it_lines.
else.
IF wa_lines is not INITIAL.
append wa_lines to it_lines.
ENDIF.
exit.
ENDIF.
ENDDO.
CLOSE DATASET l_file.
" send PDF by email / save as pdf ...
call function 'GUI_DOWNLOAD'
EXPORTING
filename = 'C:\TEMP\B.PDF'
filetype = 'BIN'
TABLES
data_tab = it_lines
EXCEPTIONS
OTHERS = 15.
ENDIF.
ENDIF.
" remove temp file from app server
delete DATASET l_file.
ENDIF.