*&---------------------------------------------------------------------*
*& Report /GRUBE/PDF_TEST_REPORT_AS_MAIL *
*& *
*&---------------------------------------------------------------------*
* Send an encrypted SAP report as PDF mail attachment
* to simplify the report the password is sent in the body of the mail
* In real scenarios the password should be sent in a different way (e.g. SMS)
* to protect the privacy
* A similar report can be used to send forms (e.g. payslips, invoices, etc) as
* encrypted PDF attachment. The watermark can be added in the same way as it is done
* in the other samples
* To avoid an email send error please create an user 'GC1MAIL' in SU01 with a sender
* email address
report /grube/pdf_test_report_as_mail .
data: lv_spool(20) type c.
data: l_spool type rspoid.
data: lt_pdf type table of tline.
data: l_pdf type tline.
data: send_request type ref to cl_bcs.
data: bcs_exception type ref to cx_bcs.
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.
data: lt_xpdf type solix_tab.
data: l_xpdf type solix.
data: result type xstring.
data: len type so_obj_len.
data: pass(12).
data: o_pdf type ref to /grube/cl_pdf.
data: o_encrypt_aes type ref to /grube/cl_pdf_encryption_aes.
data: o_encrypt type ref to /grube/cl_pdf_encryption.
data: upass type string.
data: opass type string.
data: line type SOLI.
start-of-selection.
* run the report and send the output to the spooler
submit rsusr003 with p_title = 'PDF Mail Test'
to sap-spool
without spool dynpro
destination space
immediately space
new list identification 'X'
and return.
* get the spool id
get parameter id 'SPI' field lv_spool.
l_spool = lv_spool.
* convert the spool to PDF
call function 'CONVERT_ABAPSPOOLJOB_2_PDF'
exporting
src_spoolid = l_spool
no_dialog = 'X'
tables
pdf = lt_pdf
exceptions
err_no_abap_spooljob = 1
err_no_spooljob = 2
err_no_permission = 3
err_conv_not_possible = 4
err_bad_destdevice = 5
user_cancelled = 6
err_spoolerror = 7
err_temseerror = 8
err_btcjob_open_failed = 9
err_btcjob_submit_failed = 10
err_btcjob_close_failed = 11
others = 12.
if sy-subrc <> 0.
write: / 'Error when converting spool to PDF'.
exit.
endif.
* convert the PDF table into xstring
field-symbols: type x.
loop at lt_pdf into l_pdf.
assign l_pdf to casting.
concatenate result into result in byte mode.
endloop.
* 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.
* set the PDF and the encryption
create object o_pdf.
o_pdf->set_pdf_file( result ).
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
.
* create the mail and attachment as described in the BCS_EXAMPLE programs
try.
send_request = cl_bcs=>create_persistent( ).
append 'Please find the requested report in the attachment' to text.
concatenate 'Password:' pass into line-line.
append line to text.
document = cl_document_bcs=>create_document(
i_type = 'RAW'
i_text = text
i_length = '72'
i_subject = 'SAP Report' ).
call method document->add_attachment
exporting
i_attachment_type = 'PDF'
i_attachment_subject = 'SAP Report'
i_attachment_size = len
i_att_content_hex = lt_xpdf.
call method send_request->set_document( document ).
sender = cl_sapuser_bcs=>create( 'GC1MAIL' ).
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.