Career Talk Series: Session 2- Unlock career opportunities in intelligent aut...
Encrypting PDF Files Using ABAP Code
1. Encrypting PDF Files by ABAP Code
By: Roger Lavi Chávez – Consultor SAP HCM/ABAP/WorFlow.
Content
1. Prerequisite................................................................................................................................. 2
1.1. Download Java Encrypter File..................................................................................... 2
1.2. Upload the .jar file to the Server SAP.......................................................................... 2
2. Procedure.................................................................................................................................... 3
2.1. Funcionality............................................................................................................... 3
2.2. ABAP Code. ............................................................................................................... 3
2. Encrypting PDF Files by ABAP Code
By: Roger Lavi Chávez – Consultor SAP HCM/ABAP/WorFlow.
1. Prerequisite
1.1. Download Java Encrypter File.
Download the .jar file from URL:
https://pdfbox.apache.org/downloads.html#recent
1.2. Upload the .jar file to the Server SAP.
The .jar file must be uploaded to a directory from SAP server. To do this, use the
CG3Z TCode. We indicate the PC directory where the .jar file was downloaded and
the server directory where you will be uploaded the .jar file.
3. Encrypting PDF Files by ABAP Code
By: Roger Lavi Chávez – Consultor SAP HCM/ABAP/WorFlow.
2. Procedure
2.1. Funcionality
This report has the next premises:
Require the URL of PDF file and the password to Encrypt It.
Indicate the server’s URL where .jar file was uploaded.
Define the PC URL where .jar file will be downloaded on runtime. Only if the .jar
file is not found in Local PC.
Define the PC URL where .BAT file will be created on runtime. This executes the
command to encrypt the PDF File.
This command has the next structure:
“java –jar <LocalURLpdfbox-app-1.8.6.jar> Encrypt –U <Password>
<LocalURLFile.PDF>”.
Example:
java -jar D:pdfbox-app-1.8.6.jar Encrypt -U 12345 D:TESTPDF.pdf
2.2. ABAP Code.
TYPES : BEGIN OF lty_content,
linecmd LIKE rlgrap-filename,
END OF lty_content.
DATA : ld_ftfront LIKE rcgfiletr-ftfront,
ld_ftappl LIKE rcgfiletr-ftappl,
ld_flgstay TYPE boolean,
ld_return TYPE c,
ld_filename TYPE string,
ld_fileDat TYPE rlgrap-filename VALUE 'D:FileCmd.BAT',
ld_flg_open_error TYPE boolean,
ld_os_message(100) TYPE c.
DATA : lt_content TYPE STANDARD TABLE OF lty_content,
ls_content LIKE LINE OF lt_content.
PARAMETERS : p_front TYPE rcgfiletr-ftfront DEFAULT 'D:pdfbox-app-
1.8.6.jar' NO-DISPLAY,
p_appl LIKE rcgfiletr-
ftappl DEFAULT '/usr/sap/trans/tmp/pdfbox-app-1.8.6.jar' NO-
DISPLAY,
p_Encryp TYPE rlgrap-
filename DEFAULT 'D:TESTPDF.pdf',
p_Pass TYPE rlgrap-filename DEFAULT '12345'.
ld_ftfront = p_front.
ld_ftappl = p_appl.
4. Encrypting PDF Files by ABAP Code
By: Roger Lavi Chávez – Consultor SAP HCM/ABAP/WorFlow.
ld_filename = ld_ftfront.
* Verify if it was downloaded previously
CALL METHOD cl_gui_frontend_services=>file_exist
EXPORTING
file = ld_filename
RECEIVING
result = ld_return
EXCEPTIONS
cntl_error = 1
error_no_gui = 2
wrong_parameter = 3
not_supported_by_gui = 4
OTHERS = 5.
IF ld_return EQ abap_false.
* Change the sy-cprog else the next function module return error.
sy-cprog = 'SAPLC13G'.
CALL FUNCTION 'C13Z_FILE_DOWNLOAD_BINARY'
EXPORTING
i_file_front_end = ld_ftfront
i_file_appl = ld_ftappl
i_file_overwrite = 'X'
IMPORTING
e_flg_open_error = ld_flg_open_error
e_os_message = ld_os_message
EXCEPTIONS
fe_file_open_error = 1
fe_file_exists = 2
fe_file_write_error = 3
ap_no_authority = 4
ap_file_open_error = 5
ap_file_empty = 6
OTHERS = 7.
ENDIF.
* Build the Command
CONCATENATE 'java -jar'
p_front
'Encrypt -U'
p_Pass
p_Encryp
INTO ls_content-linecmd
SEPARATED BY space.
* Agregar el Comando al contenido del archivo BAT
APPEND ls_content TO lt_content.
* Download .BAT File on Local Directory
CALL FUNCTION 'WS_DOWNLOAD'
5. Encrypting PDF Files by ABAP Code
By: Roger Lavi Chávez – Consultor SAP HCM/ABAP/WorFlow.
EXPORTING
FILENAME = ld_fileDat
FILETYPE = 'DAT'
TABLES
DATA_TAB = lt_content
EXCEPTIONS
FILE_OPEN_ERROR = 1
FILE_WRITE_ERROR = 2
INVALID_FILESIZE = 3
INVALID_TYPE = 4
NO_BATCH = 5
UNKNOWN_ERROR = 6
INVALID_TABLE_WIDTH = 7
GUI_REFUSE_FILETRANSFER = 8
CUSTOMER_ERROR = 9
OTHERS = 10.
* Execute .BAT file
CALL FUNCTION 'GUI_EXEC'
EXPORTING
command = ld_fileDat.
Finally Verify the PDF File and use the seted password