Password protect a PDF file
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.
Hi Antonio Vaz,
This is what I am looking for..
Please guide me to upload .jar file using CG3Z.
Thanks
Sreekanth
I pasted jar file directly to application server directory.
Its working perfectly.. thanks again 🙂
Hi Sreekanth I'm on CET+0 and I just saw your question. Nevertheless you've managed to overcome the situation by yourself (great 🙂 ).
Best Regards,
António Vaz
Thank you..
May I know what are the other possibility with this jar?
Great question, please refer to Apache PDFBox | A Java PDF Library
Among this functionality others like "PDF to Image conversion", "Text Extraction", "PDF Creation" ... and so on, are available.
I have no experience with any of these (just with the "PDF Signing").
Thanks for ur instant replay 🙂
This is much interesting..
Warm Regards
Sreekanth
Hi Antonio,
I have tried to download .jar file but couldn't . If you provide me .jar file or the proper link which is working it will be great.
Thanks,
Saurin
Hi Sreekanth,
I am not able to get .jar file from provided link. Is it possible for you to provide me the same . It will be great help.
Thanks,
Saurin
Hi,
I have followed same step.
Pdf document saved, but it is not password protected.
Can you please help me here what I'm doing wrong.
Thanks,
Nikesh
Hello Nikesh, did you managed to solve your problem?
Best regards,
Nehemias.
Hello Hehemias,
I've got the same issue as Nikesh Kumar Singh .
The PDF is generated but is no password protected.
Can you help me please?
Kind regards,
Iulian Durlan
Hi.
what is the proposed password of the pdf?
Is it necessary for the pdf to exist on the server?
Kind regards,
Ricardo Herrera.
Hi
Â
Will it have any impact on applicaton server while import and execution jar files .
Â
Thanks .
Â