Merge Multiple Forms Into A Single PDF and Send As Email
Merge Multiple Forms Into A Single PDF and Send As Email
PRIVATE SECTION.
DATA: mv_merged_pdf TYPE fpcontent.
METHODS: get_form IMPORTING iv_customer_id TYPE s_customer
RETURNING VALUE(rv_pdf) TYPE fpcontent.
METHODS: get_customer_data IMPORTING iv_customer_id TYPE s_customer
EXPORTING customer_detail TYPE scustom
bookings TYPE ty_bookings
connections TYPE ty_connections.
ENDCLASS.
METHOD get_form.
DATA(lv_form_name) = CONV tdsfname( 'FP_TEST_03' ).
DATA(lv_form_fm_name) = VALUE rs38l_fnam( ).
DATA(ls_docparams) = VALUE sfpdocparams( ).
DATA(ls_outputparams) = VALUE sfpoutputparams( ).
DATA(ls_formoutput) = VALUE fpformoutput( ).
* Get data
get_customer_data(
EXPORTING
iv_customer_id = iv_customer_id
IMPORTING
customer_detail = DATA(customer)
bookings = DATA(bookings)
connections = DATA(connections) ).
rv_pdf = ls_formoutput-pdf.
ENDMETHOD.
METHOD merge_forms.
DATA(lo_pdf_merger) = cl_rspo_pdf_merger=>create_instance( ).
LOOP AT it_customers ASSIGNING FIELD-SYMBOL(<customer_id>).
lo_pdf_merger->add_document( get_form( <customer_id> ) ).
ENDLOOP.
TRY.
mv_merged_pdf = lo_pdf_merger->merge_documents( ).
CATCH cx_rspo_pdf_merger INTO DATA(lx_pdf).
ENDTRY.
ENDMETHOD.
METHOD send_mail.
TRY.
DATA(lo_mail) = cl_bcs=>create_persistent( ).
DATA(lo_sender_address) = cl_cam_address_bcs=>create_internet_address(
i_address_string = CONV #( '[email protected]' ) ).
lo_mail->set_sender( lo_sender_address ).
DATA(recipient_address) = cl_cam_address_bcs=>create_internet_address(
i_address_string = CONV #( '[email protected]' ) ).
lo_mail->add_recipient( i_recipient = recipient_address ).
DATA(lo_document) = cl_document_bcs=>create_document(
i_type = 'htm'
i_subject = `Adobe Form Attachment`
i_text = VALUE #( ( CONV #( 'Form Attachment' ) ) )
i_sender = lo_sender_address ).
lo_mail->set_document( lo_document ).
lo_mail->set_send_immediately( abap_true ).
DATA(lv_send_success) = lo_mail->send( ).
COMMIT WORK.
ENDTRY.
ENDMETHOD.
ENDCLASS.
START-OF-SELECTION.
DATA(lo_mail) = NEW lcl_merge_form_and_send_mail( ).
lo_mail->merge_forms( it_customers = VALUE #( ( CONV #( 12 ) )
( CONV #( 13 ) ) ) ).
lo_mail->send_mail( ).
Result:
Page 1
Page 2