Tuesday, 12 October 2021 17:09

Send password protected PDF file from SAP via email

Written by Vipin Saraika
Rate this item
(0 votes)
Source https://blogs.sap.com/2021/10/13/send-password-protected-pdf-file-from-sap-via-email/
“© 2020. SAP SE or an SAP affiliate company. All rights reserved.” “Used with permission of SAP SE”

This blog describes the procedure to email a PDF with password protection, as an attachment. Normally, we have a scenario where we convert the OTF output of a Smartform, or a spool request to PDF.
Without using any third-party tool, we can password protect the PDF and send it via email using SAP.
Business Scenario:
Generate a payslip using smartform, encrypt it with a password and send it to the employee via email.


  • You are able to generate a smartform with OTF data


Convert OTF of a smartform to PDF file

 CALL FUNCTION 'CONVERT_OTF' EXPORTING format = 'PDF' max_linewidth = 132 IMPORTING bin_filesize = v_len_in TABLES otf = i_otf lines = i_tline EXCEPTIONS err_max_linewidth = 1 err_format = 2 err_conv_not_possible = 3 OTHERS = 4.


Move the PDF file to the Application server. In the below scenario we are moving the file to /tmp/ directory in AL11 Transaction, One in .PDF format and the other in .ZIP format.

 DATA: l_file TYPE string, l_file_zip TYPE string, itab_attach TYPE xstring, t_attachment TYPE solix_tab. CLEAR:l_file,l_file_zip,itab_attach. REFRESH:t_attachment[]. CONCATENATE '/tmp/' wa_final-pernr '''.PDF' INTO l_file. CONCATENATE '/tmp/' wa_final-pernr '''.zip' INTO l_file_zip. REPLACE ALL OCCURRENCES OF '''' IN l_file WITH space. REPLACE ALL OCCURRENCES OF '''' IN l_file_zip WITH space. ****code move PDF to server location*** OPEN DATASET l_file FOR OUTPUT IN BINARY MODE . IF sy-subrc = 0 . LOOP AT i_tline. TRANSFER i_tline TO l_file . ENDLOOP. CLOSE DATASET l_file . ELSE. WRITE : / 'operating system could not open file' . ENDIF. ***end server code***


Configure the external command using the transaction SM69. Refer to the below screenshot for information to be maintained as part of the configuration.


SM69-External Command


Use the below source code and get external commands from Transaction SM69, and encrypt the file with a password using SXPG_COMMAND_EXECUTE Function module.

 DATA: BEGIN OF command_list OCCURS 0. INCLUDE STRUCTURE sxpgcolist. DATA: END OF command_list . DATA: BEGIN OF exec_protocol OCCURS 0. INCLUDE STRUCTURE btcxpm. DATA: END OF exec_protocol. DATA: status LIKE btcxp3-exitstat, commandname LIKE sxpgcolist-name VALUE 'ZDJ_ENCRYPTPDF', sel_no LIKE sy-tabix. * GET LIST OF EXTERNAL COMMANDS CALL FUNCTION 'SXPG_COMMAND_LIST_GET' EXPORTING commandname = commandname operatingsystem = sy-opsys TABLES command_list = command_list EXCEPTIONS OTHERS = 1. CALL FUNCTION 'SXPG_COMMAND_CHECK' EXPORTING commandname = command_list-name operatingsystem = sy-opsys EXCEPTIONS no_permission = 1 command_not_found = 2 parameters_too_long = 3 security_risk = 4 wrong_check_call_interface 
Continue reading here
Read 52 times

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.