Skip to Content
Author's profile photo Krupa Janiji

PDF with Password protection

1: convert otf to pdf file

2: download pdf file into application server

3:encrypt pdf file with password protect

4:upload pdf file from application server

5:send mail

      OTR TO PDF

FORM pdf .
CLEAR gt_otf.
REFRESH gt_otf .
gt_otf[]
= it_otfdata[].
*• Convert the OTF DATA to SAP Script Text lines
CLEAR gt_pdf_tab.

CALL FUNCTION 'CONVERT_OTF'
EXPORTING
format                      = 'PDF'
max_linewidth              
= 132

IMPORTING
bin_filesize               
= gv_bin_filesize
*   BIN_FILE                    =
TABLES
otf                        
= gt_otf
lines                       = gt_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.


PERFORM download.
PERFORM pssword_prot_encryptpdf .
PERFORM send_mail.
ENDFORM.                   

     Then download pdf file into application server using following code

FORM download .
*
DATA: l_file TYPE string  .
CONCATENATE  ‘d:\pdf/’ main_dtlvbeln ‘.PDF’ INTO l_file.

OPEN DATASET l_file FOR OUTPUT IN BINARY MODE  .
IF  sysubrc = 0 .
LOOP AT gt_pdf_tab.
TRANSFER gt_pdf_tab TO l_file .
ENDLOOP.
CLOSE DATASET l_file  .
ELSE.
WRITE : / ‘operating system could not open file’ .
ENDIF.

ENDFORM.     

  

  • Then  use command line mode Encryptpdf, from verypdf.com,
  • so successfuly file encrypted and password protected.
  • Also you have to create external operating system command (SM69)

       ZTEST is external command

/wp-content/uploads/2012/06/external_112363.jpg

FORM pssword_prot .

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 btcxp3exitstat,
commandname
LIKE sxpgcolistname VALUE ‘ZTEST’,

  sel_no LIKE sytabix.

* GET LIST OF EXTERNAL COMMANDS

CALL FUNCTION ‘SXPG_COMMAND_LIST_GET’
EXPORTING
commandname    
= commandname
operatingsystem
= syopsys
TABLES
command_list   
= command_list
EXCEPTIONS
OTHERS          = 1.

CALL FUNCTION ‘SXPG_COMMAND_CHECK’
EXPORTING
commandname               
= command_listname
operatingsystem           
= syopsys
EXCEPTIONS
no_permission             
= 1
command_not_found         
= 2
parameters_too_long       
= 3
security_risk             
= 4
wrong_check_call_interface
= 5
x_error                   
= 6
too_many_parameters       
= 7
parameter_expected        
= 8
illegal_command           
= 9
communication_failure     
= 10
system_failure            
= 11
OTHERS                     = 12.

CLEAR command_list.
REFRESH command_list.
DATA: v_dir_input      TYPE sxpgcolistparameters.
DATA: v_dir_input1      TYPE sxpgcolistparameters.

command_listname = ‘ZTEST’.
command_list
opsystem = ‘Windows NT’.

DATA : doc  TYPE string.

data : pass type string .

  Doc = ‘invoice’.

  Pass ‘123456’.


CONCATENATE   ‘d:\pdf\’ doc‘.PDF’ INTO name.
CONCATENATE ‘cmd /c d:\pdf\encryptpdf.exe’ ‘-i’  name  ‘-o ‘ name  ‘-u’  pass INTO v_dir_input SEPARATED BY space .

READ TABLE command_list INDEX sel_no.

CONCATENATE command_listopcommand v_dir_input INTO command_listopcommand SEPARATED BY space.

* CHECK AUTHORIZATION
command_list
addpar = ‘X’.
APPEND command_list.
.
CONSTANTS: c_extcom    TYPE sxpgcolistname VALUE ‘ZTEST’,
c_oper     
TYPE syopsys VALUE ‘Windows NT’.

DATA: t_result         TYPE STANDARD TABLE OF btcxpm.
v_dir_input 
command_listopcommand.

CALL FUNCTION ‘SXPG_COMMAND_EXECUTE’

    EXPORTING

      commandname                   = c_extcom

      additional_parameters         = v_dir_input

      operatingsystem               = c_oper

    TABLES

      exec_protocol                 = t_result

    EXCEPTIONS

      no_permission                 = 1

      command_not_found             = 2

      parameters_too_long           = 3

      security_risk                 = 4

      wrong_check_call_interface    = 5

      program_start_error           = 6

      program_termination_error     = 7

      x_error                       = 8

      parameter_expected            = 9

      too_many_parameters           = 10

      illegal_command               = 11

      wrong_asynchronous_parameters = 12

      cant_enq_tbtco_entry          = 13

      jobcount_generation_error     = 14

      OTHERS                        = 15.

*
ENDFORM.                   

FORM send_mail

.

You can open file using following logic

DATA : doc  TYPE string

.

DATA: pdf TYPE REF TO cl_hrasr00_pdf_generation.

DATA : lt_data            TYPE solix_tab

DATA : doc  TYPE string.

Doc = ‘invoice’.

filename = name.

 

IF NAME IS INITIAL.


CONCATENATE   ‘d:\pdf\’ doc ‘.PDF’ INTO name.


filename
= name.


ENDIF

.


OPEN DATASET filename FOR INPUT IN BINARY MODE.
IF sysubrc EQ 0.
READ DATASET filename INTO filex.
*      CLOSE DATASET filename.
ENDIF.


After that using following Functional module you can send file

CALL FUNCTION ‘SO_NEW_DOCUMENT_ATT_SEND_API1’

ENDFORM.                    ” SEND_MAIL






Assigned Tags

      15 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Thomas Zloch
      Thomas Zloch

      Is this your fan club? šŸ˜†

      Looks like you converted your own thread into a document, which is fine, but please try to have a more consistent layout next time.

      http://scn.sap.com/thread/1816222

      Thomas

      Author's profile photo Krupa Janiji
      Krupa Janiji
      Blog Post Author
      Above thread which u gave link ..has .not any type of logic ,related to how to encrypt or password protect and also not given any FM for encrypt file ,only wrote how to send mail and how to open file. But in this document i explained how to encrypt .PDF file with password protect.

      Author's profile photo Former Member
      Former Member

      how to pass v_dir_input in

      'SXPG_COMMAND_EXECUTE'

          Functional module in my side i am getting no such file error always i can't over come this issue

      Author's profile photo Former Member
      Former Member

      Hi Krupa,

      Can you explain the meaning of each step? Especially the encrypt part (in application server), I still do not get it. One more point, I need to encrypt the current PDF email process, and they are using pdf_xstring (CONVERT_OTFSPOOLJOB_2_PDF) instead of TDLINE, can I use your solution? Kindly advise.

      Author's profile photo Former Member
      Former Member

      One more question, what command should we use if our system is Linux, not Windows NT?

      OK, I got it, we absolutely cannot use this solution, so sad but thanks for sharing.

      Author's profile photo Kiran K
      Kiran K

      Krupa,

      encryptpdf.exe

      verypdf.com

      From where you got the encryptpdf.exe file ?

      Is verypdf.com a freeware ?

      The above two points are very crucial.If those are proprietary software whose license needs to be procured by the Customer;its usage without license will be considered as illegal.

      K.Kiran.

      Author's profile photo Krupa Janiji
      Krupa Janiji
      Blog Post Author

      you have to install encryptpdf in ur server

      Author's profile photo Former Member
      Former Member

      Thank you very much for the good information.

      Could you please let me know how I can get the encryptpdf.exe you mentioned. is it a 3rd party software that our company will need to buy?  Is it the verypdf software we need to buy? Where can we get it from.

      Please advise.

      Kind regards,

      Peter

      Author's profile photo Krupa Janiji
      Krupa Janiji
      Blog Post Author

      You have to install encryptpdf software in server side

      Author's profile photo Krupa Janiji
      Krupa Janiji
      Blog Post Author

      insted of encryptpdf software you can use winzip 'cmd /c c:\winzip\wzzip.exe '.

      Author's profile photo Former Member
      Former Member

      thanks for the quick reply.

      Can I apply the same encryption and password protection to the PDF file if I use winzip.

      We need to do this for security before we are sending employee payslips.

      Kind regards,

      Peter

      Author's profile photo Pallavi Garrepally
      Pallavi Garrepally

      Hi Krupa,

      I have the similar requirement to make PDF password protected in background. but 'SXPG_COMMAND_EXECUTE' returns sy-subrc '2'. Also when I solely execute command from SM69, external command is not making pdf password protected

      Can you please provide the parameters to this FM?

      Could you achieve this when adobe PDF data to be password protected?

      Thank you,

      Pallavi

      Author's profile photo nishanth budama
      nishanth budama

      Hi krupa,

      I have used encryptpdf but i am getting

      'External program terminated with exit code 1'  in T_RESULT from

      SXPG_COMMAND_EXECUTE can u tell me the solution for that,

      Thank you.

      Author's profile photo Saikrishna Dogiparthi
      Saikrishna Dogiparthi

      Hello Krupa,

      When iā€™m trying to encrypt the file using encryptpdf.exe iam getting the error ā€˜The system cannot find the path specified.ā€™

      Can you please help me on this ?

      Ā 

      Thanks,

      Sai Krishna.

      Author's profile photo Mantri Shekar
      Mantri Shekar

      Hello Krupa,

      The above Solution Working fine in ECC with respect to Presentation Server (Fore ground saving file Locally and in Application Server Saving PDF file in SFTP). But the same is not Working in S4 Hana as the OS USed in S4 Hana is AIX OS. Could you Please do need ful to Provide the Respective command used ofr AIX OS.

       

       

      Regards,

      Shekar.