如何将打印报表作为PDF附件发送的代码样例

此博客展示了如何在SAP环境中通过编程方式利用BCS(Business Communication Services)服务,结合PDF表单生成和SPFLI连接,将含有客户预订信息的PDF文档作为电子邮件附件发送给指定用户。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

*----------------------------------------------------------------------*
*       Report BCS_EXAMPLE_6
*----------------------------------------------------------------------*
*       Email documents using PDF based forms
*----------------------------------------------------------------------*
REPORT bcs_example_6.

DATA: carr_id TYPE sbook-carrid.

PARAMETER:      p_custid  TYPE scustom-id DEFAULT 12.
SELECT-OPTIONS: s_carrid FOR carr_id     DEFAULT 'AA' TO 'ZZ'.
PARAMETER:      p_email   TYPE adr6-smtp_addr OBLIGATORY.

DATA: customer          TYPE scustom,
      bookings          TYPE ty_bookings,
      connections       TYPE ty_connections,
      fm_name           TYPE rs38l_fnam,
      fp_docparams      TYPE sfpdocparams,
      fp_outputparams   TYPE sfpoutputparams,
      error_string      TYPE string.
DATA  ls_formoutput     TYPE fpformoutput.
DATA  lx_fp_api         TYPE REF TO cx_fp_api.
data  lp_form           TYPE tdsfname  value 'FP_TEST_03'.
data  lp_langu          TYPE langu     value 'D'.
data  lp_countr         TYPE land1     value 'DE'.

* BCS data
DATA  send_request       TYPE REF TO cl_bcs.
DATA  text               TYPE bcsy_text.
DATA  document           TYPE REF TO cl_document_bcs.
DATA  recipient          TYPE REF TO if_recipient_bcs.
DATA: bcs_exception      TYPE REF TO cx_bcs.
DATA  sent_to_all        TYPE os_boolean.
DATA  pdf_content        TYPE solix_tab.
DATA  lp_pdf_size        TYPE so_obj_len.


* get data
SELECT SINGLE * FROM scustom INTO customer WHERE id = p_custid.
CHECK sy-subrc = 0.
SELECT * FROM sbook INTO TABLE bookings
         WHERE customid = p_custid
         AND   carrid   IN s_carrid
         ORDER BY PRIMARY KEY.
if sy-subrc = 0.
  SELECT * FROM spfli INTO TABLE connections
           FOR ALL ENTRIES IN bookings
           WHERE carrid = bookings-carrid
           AND   connid = bookings-connid
           ORDER BY PRIMARY KEY.
endif.

* Print data:

* First get name of the generated function module
TRY.
    CALL FUNCTION 'FP_FUNCTION_MODULE_NAME'
      EXPORTING
        i_name     = lp_form
      IMPORTING
        e_funcname = fm_name.
  CATCH cx_fp_api INTO lx_fp_api.
*   exception handling
    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.
    EXIT.
ENDTRY.

* Set output parameters and open spool job
fp_outputparams-nodialog = 'X'.   " no print preview
fp_outputparams-getpdf = 'X'.    " request PDF

CALL FUNCTION 'FP_JOB_OPEN'
  CHANGING
    ie_outputparams = fp_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.
ENDIF.

* Set form language and country (->form locale)
fp_docparams-langu   = lp_langu.
fp_docparams-country = lp_countr.

* Now call the generated function module
CALL FUNCTION fm_name
  EXPORTING
    /1bcdwb/docparams  = fp_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.
  CALL FUNCTION 'FP_GET_LAST_ADS_ERRSTR'
    IMPORTING
      e_adserrstr = error_string.
  IF NOT error_string IS INITIAL.
*     we received a detailed error description
    WRITE:/ error_string.
    EXIT.
  ELSE.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
ENDIF.

* Close spool job
CALL FUNCTION 'FP_JOB_CLOSE'
*   IMPORTING
*     E_RESULT             =
  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.
ENDIF.


* ------------ Call BCS interface ----------------------------------

TRY.
*   ---------- create persistent send request ----------------------
    send_request = cl_bcs=>create_persistent( ).

*   ---------- add document ----------------------------------------
*   get PDF xstring and convert it to BCS format
    lp_pdf_size = XSTRLEN( ls_formoutput-pdf ).

    pdf_content = cl_document_bcs=>xstring_to_solix(
        ip_xstring = ls_formoutput-pdf ).

    document = cl_document_bcs=>create_document(
          i_type    = 'PDF'
          i_hex     = pdf_content
          i_length  = lp_pdf_size
          i_subject = 'test created by BCS_EXAMPLE_6' ). "#EC NOTEXT

*   add document to send request
    send_request->set_document( document ).

*   ---------- add recipient (e-mail address) ----------------------
    recipient = cl_cam_address_bcs=>create_internet_address(
        i_address_string = p_email ).

*   add recipient to send request
    send_request->add_recipient( i_recipient = recipient ).

*   ---------- send document ---------------------------------------
    sent_to_all = send_request->send(
        i_with_error_screen = 'X' ).

    IF sent_to_all = 'X'.
      message i022(so).
    ENDIF.

*   ---------- explicit 'commit work' is mandatory! ----------------
    COMMIT WORK.

* ------------------------------------------------------------------
* *            exception handling
* ------------------------------------------------------------------
* * replace this very rudimentary exception handling
* * with your own one !!!
* ------------------------------------------------------------------
  CATCH cx_bcs INTO bcs_exception.
    WRITE: text-001.
    WRITE: text-002, bcs_exception->error_type.
    EXIT.

ENDTRY.
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值