*&---------------------------------------------------------------------*
*& Report /GRUBE/PDF_TEST_FORM_AS_MAIL *
*& *
*&---------------------------------------------------------------------*
* Send an encrypted SAP Smartform 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_form_as_mail .
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.
data: customer type scustom,
bookings type ty_bookings,
connections type ty_connections.
data: carr_id type sbook-carrid,
fm_name type rs38l_fnam.
data: p_form type tdsfname.
data: my_control_pars type ssfctrlop. "for CONTROL_PARAMETERS
data: my_output_info type ssfcrescl. "for JOB_OUTPUT_INFO
data: out_options type ssfcompop.
data: wa_out_infos type ssfcrescl.
data: bsize(10) type n.
data: t_docs type table of docs.
data: t_tlines type table of tline.
data: lt_pdf type swuoconttab.
data: l type i.
start-of-selection.
p_form = 'SF_EXAMPLE_01'.
* get data
select single * from scustom into customer where id = '0000001754'.
check sy-subrc = 0.
select * from sbook into table bookings
where customid = '0000001754'
and carrid = 'AA'
order by primary key.
select * from spfli into table connections
for all entries in bookings
where carrid = bookings-carrid
and connid = bookings-connid
order by primary key.
* print data
* Get the function module of the smartform
call function 'SSF_FUNCTION_MODULE_NAME'
exporting formname = p_form
* variant = ' '
* direct_call = ' '
importing fm_name = fm_name
exceptions no_form = 1
no_function_module = 2
others = 3.
if sy-subrc <> 0.
* error handling
message id sy-msgid type sy-msgty number sy-msgno
with sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
exit.
endif.
my_control_pars-no_dialog = 'X'.
my_control_pars-getotf = 'X'.
my_control_pars-device = 'PRINTER'.
out_options-tdimmed = 'X'.
out_options-tddelete = 'X'.
out_options-tddest = 'LP01'.
* now call the generated function module
call function fm_name
exporting
control_parameters = my_control_pars
output_options = out_options
user_settings = ' '
customer = customer
bookings = bookings
connections = connections
importing
job_output_info = wa_out_infos
exceptions
formatting_error = 1
internal_error = 2
send_error = 3
user_canceled = 4
others = 5.
call function 'CONVERT_OTF_2_PDF'
importing
bin_filesize = bsize
tables
otf = wa_out_infos-otfdata
doctab_archive = t_docs
lines = t_tlines
exceptions
err_conv_not_possible = 1
err_otf_mc_noendmarker = 2
others = 3.
call function 'SX_TABLE_LINE_WIDTH_CHANGE'
exporting
line_width_src = 134
line_width_dst = 255
tables
content_in = t_tlines
content_out = lt_pdf.
l = bsize.
call function 'SCMS_BINARY_TO_XSTRING'
exporting
input_length = l
importing
buffer = result
tables
binary_tab = lt_pdf.
* 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 invoice 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 = '73'
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.