Skip to Content
Author's profile photo Former Member

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:

  1. It is necessary to upload the jar (available here) to the application server either using the server filesystem or using the tcode CG3Z.
  2. Create a custom command under SM69 (Fill in the correct path to the jar file)

Untitled.png

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 sxpgcolistname,

       unique_name               type sysuuid_c.

v_language = sylangu.

CALL FUNCTION ‘SSF_GET_DEVICE_TYPE’

   EXPORTING

     i_language    = v_language

     i_application = ‘SAPDEFAULT’

   IMPORTING

     e_devtype     = v_e_devtype.

st_output_optionstdprinter = v_e_devtype.

st_control_parametersno_dialog = ‘X’.

st_control_parametersgetotf = ‘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_infootfdata.

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  sysubrc = 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 SYSUBRC = 0.

     close dataset l_file.

     OPEN DATASET l_file FOR INPUT IN BINARY MODE.

     IF sysubrc EQ 0.

       refresh it_lines.

       DO.

         clear wa_lines.

         READ DATASET l_file INTO wa_lines.

         IF sysubrc = 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.

Assigned Tags

      13 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sreekanth Pavoor
      Sreekanth Pavoor

      Hi Antonio Vaz,

      This is what I am looking for..

      Please guide me to upload .jar file using CG3Z.

      Thanks

      Sreekanth

      Author's profile photo Sreekanth Pavoor
      Sreekanth Pavoor

      I pasted jar file directly to application server directory.

      Its working perfectly.. thanks again 🙂

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      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

      Author's profile photo Sreekanth Pavoor
      Sreekanth Pavoor

      Thank you..

      May I know what are the other possibility with this jar?

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      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").

      Author's profile photo Sreekanth Pavoor
      Sreekanth Pavoor

      Thanks for ur instant replay 🙂

      This is much interesting..

      Warm Regards

      Sreekanth

      Author's profile photo Former Member
      Former Member

      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

      Author's profile photo Former Member
      Former Member

      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

      Author's profile photo Nikesh Kumar Singh
      Nikesh Kumar Singh

      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

      Author's profile photo Nehemias Herrera
      Nehemias Herrera

      Hello Nikesh, did you managed to solve your problem?

       

      Best regards,

      Nehemias.

      Author's profile photo Iulian Durlan
      Iulian Durlan

      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

      Author's profile photo Ricardo Herrera
      Ricardo Herrera

      Hi.

      what is the proposed password of the pdf?

       

      Is it necessary for the pdf to exist on the server?

       

      Kind regards,

      Ricardo Herrera.

      Author's profile photo Prasad Sikkireddi
      Prasad Sikkireddi

      Hi

       

      Will it have any impact on applicaton server while import and execution jar files .

       

      Thanks .

      Â