Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Password Encrypted Mail triggering with PDF attachment for HR payslip (Customised.)

dineshs
Explorer

Hi Gurus,

I am stuck at building a program where HR payslip is sent through mail in Password protected format.

I have followed the below method to accomplish the mail triggering program with Password protected.

First I have installed a Package where the Password encryption Library is installed into Application server.

Then Have Build a program for Mail triggering in HR payslip (Customized).

IF it_reclist[] IS NOT INITIAL.

CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
EXPORTING
formname = 'ZHR_SEQUEL_PAYSLIP1'
IMPORTING
fm_name = fm_name
EXCEPTIONS
no_form = 1
no_function_module = 2
OTHERS = 3.
IF sy-subrc <> 0.
* MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
* WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF .

wa_ctrlop-getotf = 'X'.
wa_ctrlop-no_dialog = 'X'.
wa_compop-tdnoprev = 'X'.

READ TABLE it_net INTO wa_net WITH KEY pernr = wa_final-pernr
earn_desc = 'Net Pay'.
IF sy-subrc = 0.
CLEAR wa_final-net_pay.
wa_final-net_pay = wa_net-earn_amt.
ENDIF.

CALL FUNCTION fm_name
EXPORTING
control_parameters = wa_ctrlop
output_options = wa_compop
user_settings = 'X'
wa_final = wa_final
lv_finyear = lv_finyear
IMPORTING
job_output_info = wa_return
TABLES
it_display = it_display1
EXCEPTIONS
formatting_error = 1
internal_error = 2
send_error = 3
user_canceled = 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.


it_otf[] = wa_return-otfdata[].

CALL FUNCTION 'CONVERT_OTF'
EXPORTING
format = 'PDF'
max_linewidth = 132
IMPORTING
bin_filesize = lv_len_in
* bin_file = lv_xstring
TABLES
otf = it_otf
lines = it_line
EXCEPTIONS
err_max_linewidth = 1
err_format = 2
err_conv_not_possible = 3
OTHERS = 4.

IF sy-subrc <> 0.
ENDIF.

LOOP AT it_otf INTO lw_otf.
CLEAR lw_content.
CONCATENATE lw_otf-tdprintcom lw_otf-tdprintpar
INTO lw_content.
APPEND lw_content TO li_content_txt.
ENDLOOP.

REFRESH : li_content_hex,
li_objhead.

CLEAR : lv_transfer_bin,
lv_len.

*/.. FM to convert OTF to PDF
CALL FUNCTION 'SX_OBJECT_CONVERT_OTF_PDF'
EXPORTING
format_src = 'OTF'
format_dst = 'PDF'
CHANGING
transfer_bin = lv_transfer_bin
content_txt = li_content_txt
content_bin = li_content_hex
objhead = li_objhead
len = lv_len
EXCEPTIONS
err_conv_failed = 1
OTHERS = 2.

IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.

DATA: it_line1 LIKE TABLE OF it_line.

CALL FUNCTION 'CONVERT_OTF'
EXPORTING
format = 'PDF'
max_linewidth = 132
IMPORTING
bin_filesize = lv_len_in
bin_file = lv_xstring
TABLES
otf = it_otf
lines = it_line1
EXCEPTIONS
err_max_linewidth = 1
err_format = 2
err_conv_not_possible = 3
OTHERS = 4.

IF sy-subrc <> 0.
ENDIF.

* generate a password using SAP standard function
* to avoid confusion about 1 and l or 0 and O the
* alphabet is restricted to clearly identifiable
* characters
CALL FUNCTION 'RSEC_GENERATE_PASSWORD'
EXPORTING
alphabet = 'ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789'
output_length = 12
IMPORTING
output = pass
EXCEPTIONS
some_error = 1
OTHERS = 2.
IF sy-subrc <> 0.
WRITE: / 'Error when generating password'.
EXIT.
ENDIF.
xpayslip = lv_xstring.

* set the PDF and the encryption
CREATE OBJECT o_pdf.
o_pdf->set_pdf_file( xpayslip ).

* pass = 'TEST'.

upass = pass.
CONCATENATE 'O_' pass INTO opass.
CREATE OBJECT o_encrypt_aes
EXPORTING
keylength = 128.
o_encrypt ?= o_encrypt_aes.

* set the user permissions for the PDF file
o_encrypt->set_permission_ex( print_allowed = 'X'
modify_allowed = ' '
copy_allowed = 'X'
add_annotations_allowed = ' '
fill_fields_allowed = ' '
copy_accessibility_allowed = 'X'
assemble_allowed = ' '
print_highres_allowed = 'X' ).

* set the user and the owner password for the PDF
o_encrypt->set_passwords( user_password = upass owner_password = opass ).
* generate the passwords
o_encrypt->generate_passwords( o_pdf ).

* encrypt the PDF
o_pdf->do_action( o_encrypt ).

* get the encrypted and processed PDF xstring
result = o_pdf->create_pdf( ).

len = xstrlen( result ).

* convert to a solix_tab
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = result
TABLES
binary_tab = lt_xpdf.

DATA: attch TYPE so_obj_des.
DATA: d_start(10) TYPE c.
DATA: d_end(10) TYPE c.

WRITE wa_rgdir-fpbeg TO d_start.
WRITE wa_rgdir-fpend TO d_end.
CONCATENATE 'Payslip_from_' d_start '_to_' d_end INTO attch.

* create the mail and attachment as described in the BCS_EXAMPLE programs
TRY.
send_request = cl_bcs=>create_persistent( ).
CONCATENATE 'Dear' wa_i0001-ename ',' INTO line-line SEPARATED BY ' '.
APPEND line TO text.
line-line = ''.
APPEND line TO text.
APPEND 'please find your payslip in the attachment' TO text.
line-line = ''.
APPEND line TO text.
CONCATENATE 'Password:' pass INTO line-line SEPARATED BY ' '.
APPEND line TO text.
line-line = ''.
APPEND line TO text.
line-line = 'Kind Reagrads'.
APPEND line TO text.
line-line = 'Payroll'.
APPEND line TO text.
document = cl_document_bcs=>create_document(
i_type = 'RAW'
i_text = text
* i_length = '72'
i_subject = attch ).
* DATA: w_xpdf like lt_xpdf,
* w_content_hex like li_content_hex.
* LOOP at lt_xpdf INTO w_xpdf.
* APPEND w_xpdf to li_content_hex.
* ENDLOOP.
APPEND LINES OF li_content_hex to lt_xpdf.

CALL METHOD document->add_attachment
EXPORTING
i_attachment_type = 'PDF'
i_attachment_subject = attch
i_attachment_size = len
i_att_content_hex = lt_xpdf.
*
* CALL METHOD document->add_attachment
* EXPORTING
* i_attachment_type = 'PDF'
* i_attachment_subject = attch
* i_attachment_size = len
* i_att_content_hex = lt_xpdf.
"lt_xpdf.
"lt_xpdf

CALL METHOD send_request->set_document( document ).
sender = cl_sapuser_bcs=>create( 'USERNAME' ).

CALL METHOD send_request->set_sender
EXPORTING
i_sender = sender.
recipient = cl_cam_address_bcs=>create_internet_address(
'joe.doe@crazy-company.com' ).


CALL METHOD send_request->add_recipient
EXPORTING
i_recipient = recipient
i_express = 'X'.

CALL METHOD send_request->send(
EXPORTING
i_with_error_screen = 'X'
RECEIVING
result = sent_to_all ).
IF sent_to_all = 'X'.
WRITE: / 'Document Sent Successfully'.
ENDIF.
COMMIT WORK.

CATCH cx_bcs INTO bcs_exception.
WRITE: 'Fehler aufgetreten.'(001).
WRITE: 'Fehlertyp:'(002), bcs_exception->error_type.
EXIT.

ENDTRY.

****************************************************************************************

Observation 1: Mail is Triggering with encrypted password and when Password is put only blank screen is shown no PDF details is there.

Approach 2: Later I have Debuugged the program and found that in Code

APPEND LINES OF li_content_hex to lt_xpdf.

CALL METHOD document->add_attachmentEXPORTING
i_attachment_type = 'PDF'
i_attachment_subject = attch
i_attachment_size = len
i_att_content_hex = lt_xpdf.

Observation: If in i_att_content_hex lt_xpdf is passed without append lines of code then PDF with HR payslip is displayed.

If APPEND LINES OF li_content_hex to lt_xpdf.

Is given means only the password protected screen appears when we open the pdf attachment and encrypted password is given means blank screen is shown.

The reason for ‘APPEND LINES OF li_content_hex to lt_xpdf.’ Code is to merge the password encrypted with the PDF encryption.

Data declaration that is used for the program is.

SPAN { font-family: "Courier New"; font-size: 10pt; color: #000000; background: #FFFFFF; } .L0S32 { color: #3399FF; } .L0S52 { color: #0000FF; } .L0S55 { color: #800080; }

DATA: xpayslip TYPE xstring.
DATA: data_lines TYPE /grube/raw256_table.
DATA: len1 TYPE i.
DATA: fl TYPE string.
DATA: file TYPE file_table.
DATA: files TYPE filetable.
DATA: rc TYPE i.
DATA: l_molga TYPE molga.
DATA: l_rgdir TYPE h99_clst_t_rgdir.
DATA: wa_rgdir TYPE LINE OF h99_clst_t_rgdir.
DATA: lt_evp TYPE h99_clst_t_rgdir.
DATA: i0001 TYPE TABLE OF p0001.
DATA: wa_i0001 TYPE p0001.
DATA: pmehf TYPE pmehf.
DATA: hrform_name TYPE hrf_name.
DATA: message1 TYPE bapiret1.
DATA: form_object TYPE REF TO object.
DATA: pass(12).
DATA: o_pdf TYPE REF TO /grube/cl_pdf.
DATA: o_encrypt_aes TYPE REF TO /grube/cl_pdf_encry_aes_256.
DATA: o_encrypt TYPE REF TO /grube/cl_pdf_encryption.
DATA: upass TYPE string.
DATA: opass TYPE string.
DATA: line TYPE soli.
DATA: result TYPE xstring.
DATA: len TYPE so_obj_len.
DATA: lt_xpdf TYPE solix_tab.
DATA: l_xpdf TYPE solix.
DATA: send_request TYPE REF TO cl_bcs.
DATA: bcs_exception TYPE REF TO cx_bcs.
DATA: text_line TYPE soli.
DATA: text TYPE bcsy_text.
DATA: document TYPE REF TO cl_document_bcs.
DATA: sender TYPE REF TO cl_sapuser_bcs.
DATA: recipient TYPE REF TO if_recipient_bcs.
DATA: sent_to_all TYPE os_boolean.

Have refered the link .

http://pdfencryption.de/fscenario.php

http://pdfencryption.de/dl/ABAPListing_Payslip_encrypted_HRFORM.txt(for program)

Attachment installed into server from link.

http://pdfencryption.de/adownloaddemo.php

Installation Demo version of ABAP PDF library.

5 REPLIES 5

SimoneMilesi
Active Contributor

Hi Dinesh,

Can you format your code? (Copy it, Paste as plane text, select it all and press button "CODE")

I fear I'm misreading something because it's a bit hard to read.

I'm not sure about what you are doing because it seems to me you are mashing and mixing different stuff.

As first thing, I see you get your encrypted pdf with

result = o_pdf->create_pdf( ).
len = xstrlen( result ).

It seems to me that you determine len for result (i.e. len = 100) but then your attachment table is bigger since you append more lines (i.e. correct_len = 200 ) so SAP works only the size you put as len, creating a wrong PDF

RESULT should be an HEX table, right?, like the attachment definition: try to use directly RESULT as attachment and let me know!

0 Kudos
RESULT is XSTRING.

Sandra_Rossi
Active Contributor

To support a discussion, here are the main steps of your algorithm. You need to remove the 3 blocks of code marked "??", and add the one marked "++". I hope my short explanations are sufficient:

  1. CALL FUNCTION fm_name : Generate smart form in OTF format
  2. CONVERT_OTF : Convert OTF format into PDF format (PDF in parameter LINES -> variable IT_LINE)
  3. ++ "cast" IT_LINE (PDF in C-like variable) into xpayslip (PDF in XSTRING variable) by using SCMS_BINARY_TO_XSTRING EXPORTING input_length = lv_len_inIMPORTING buffer = xpayslip TABLES binary_tab = it_line
  4. ?? SX_OBJECT_CONVERT_OTF_PDF : OTF to PDF again? (into li_content_hex)
  5. ?? CONVERT_OTF : OTF to PDF again?
  6. RSEC_GENERATE_PASSWORD : generate random password
  7. CREATE OBJECT o_pdf + set_pdf_file( xpayslip ) : object to encrypt PDF, first you initialize it with the non-encrypted PDF (xpayslip variable)
  8. CREATE OBJECT o_encrypt … : object to encrypt passwords
  9. o_pdf->do_action( o_encrypt ) : DO IT (encrypt the PDF)
  10. result = o_pdf->create_pdf_file( ) : to get the encrypted PDF file into an XSTRING variable
  11. SCMS_XSTRING_TO_BINARY : to "cast" the PDF from an XSTRING variable to a HEX internal table (LT_XPDF)
  12. cl_document_bcs=>create_document : initiates the email
  13. ?? APPEND LINES OF li_content_hex to lt_xpdf. why?? LT_XPDF already contains the whole PDF with encryption included, no need to change it (and anyway I guess any change would corrupt the PDF)
  14. document->add_attachment : to attach the encrypted PDF file (LT_XPDF) to the email

deepaktiwariminda
Discoverer
0 Kudos

DATA: O_ENCRYPT_AES TYPE REF TO /GRUBE/CL_PDF_ENCRY_AES_256.
DATA: O_ENCRYPT TYPE REF TO /GRUBE/CL_PDF_ENCRYPTION.

These Classes are showing unknown, I am using S/4 latest upgrade version.

Please help.

Note:- I am using standard custom payslip using

CALL FUNCTION 'BAPI_GET_PAYSLIP_PDF'

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'

CALL FUNCTION 'QCE1_CONVERT'


0 Kudos

DATA: O_ENCRYPT_AES TYPE REF TO /GRUBE/CL_PDF_ENCRY_AES_256.
DATA: O_ENCRYPT TYPE REF TO /GRUBE/CL_PDF_ENCRYPTION.                                                        This two classes gives an Error on Sap S4, where are they defined.

Please Help.