0% found this document useful (0 votes)
108 views4 pages

Merge Multiple Forms Into A Single PDF and Send As Email

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views4 pages

Merge Multiple Forms Into A Single PDF and Send As Email

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Merge Multiple Adobe forms into Single PDF

and Send as Email Attachment


CLASS lcl_merge_form_and_send_mail DEFINITION.
PUBLIC SECTION.
TYPES: tt_customerid TYPE TABLE OF s_customer WITH EMPTY KEY.
METHODS: send_mail.
METHODS: merge_forms IMPORTING it_customers TYPE tt_customerid.

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.

CLASS lcl_merge_form_and_send_mail IMPLEMENTATION.


METHOD get_customer_data.
SELECT SINGLE FROM scustom
FIELDS *
WHERE id = @iv_customer_id
INTO @customer_detail.
SELECT FROM bookings
FIELDS *
WHERE customid = @iv_customer_id
INTO TABLE @bookings.
IF lines( bookings ) > 0.
SELECT * FROM spfli INTO TABLE @connections
FOR ALL ENTRIES IN @bookings
WHERE carrid = @bookings-carrid
AND connid = @bookings-connid.
ENDIF.
ENDMETHOD.

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

* get name of the generated function module


TRY.
CALL FUNCTION 'FP_FUNCTION_MODULE_NAME'
EXPORTING
i_name = lv_form_name
IMPORTING
e_funcname = lv_form_fm_name.
CATCH cx_fp_api INTO DATA(lx_fp_api).
MESSAGE ID lx_fp_api->msgid TYPE lx_fp_api->msgty
NUMBER lx_fp_api->msgno
WITH lx_fp_api->msgv1 lx_fp_api->msgv2
lx_fp_api->msgv3 lx_fp_api->msgv4.
RETURN.
ENDTRY.

* Set output parameters and open spool job


ls_outputparams-nodialog = 'X'. " no print preview
ls_outputparams-getpdf = 'X'. " request PDF
CALL FUNCTION 'FP_JOB_OPEN'
CHANGING
ie_outputparams = ls_outputparams
EXCEPTIONS
cancel = 1
usage_error = 2
system_error = 3
internal_error = 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.
RETURN.
ENDIF.

* Now call the generated function module


CALL FUNCTION lv_form_fm_name
EXPORTING
/1bcdwb/docparams = ls_docparams
customer = customer
bookings = bookings
connections = connections
IMPORTING
/1bcdwb/formoutput = ls_formoutput
EXCEPTIONS
usage_error = 1
system_error = 2
internal_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
RETURN.
ENDIF.

* Close spool job


CALL FUNCTION 'FP_JOB_CLOSE'
EXCEPTIONS
usage_error = 1
system_error = 2
internal_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
RETURN.
ENDIF.

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

DATA(lv_merged_pdf_size) = xstrlen( mv_merged_pdf ).


lo_document->add_attachment(
i_attachment_type = 'PDF'
i_attachment_subject = 'Customer Form'
i_attachment_size = CONV #( lv_merged_pdf_size )
i_att_content_hex = cl_bcs_convert=>xstring_to_solix( mv_merged_p
df ) ).

lo_mail->set_document( lo_document ).
lo_mail->set_send_immediately( abap_true ).
DATA(lv_send_success) = lo_mail->send( ).
COMMIT WORK.

CATCH cx_send_req_bcs cx_address_bcs cx_document_bcs INTO DATA(lx_mail).


MESSAGE lx_mail TYPE 'E'.

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

You might also like