Converting ABAP Spool to PDF and sending email to Distribution list

Scenario: There is standard report generated by Report painter tool. Now that output of report need to send as PDF format as attachement to multiple users.

Solution and Code:
Run the standard report as Job, then create ABAP prgoram to capture spool of Job, then convert spool into PDF, then send this PDF attachment thru email to Distribution list.
(Note: In SAP business workplace you need to create distibution list by assigning all user Email IDs )

PARAMETERS p_jobnam LIKE tbtcp-jobname.

data send_request  type ref to cl_bcs.
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_size      type so_obj_len.
data pdf_content   type solix_tab.
data pdf_xstring   type xstring.
data lm_dist       type so_obj_nam.
data rqident       type tsp01-rqident.
DATA v_spoolid     TYPE tsp01-rqident.
data subline       type SO_OBJ_DES.

DATAwa_tbtcp      TYPE tbtcp,
      it_tbtcp      TYPE STANDARD TABLE OF tbtcp,
      wa_varsub     type zvarsub,
      it_varsub     type standard table of zvarsub.

start-of-selection.

* Get spool number for the job
  perform get_spool_for_job.

* Get varinat details for the job.
  perform get_var_details.

loop at it_tbtcp into wa_tbtcp.
  rqident wa_tbtcp-listident.

  perform create_pdf.

  perform send_mail.

  clear wa_tbtcp.

endloop.


*&---------------------------------------------------------------------*
*&      Form  GET_VAR_DETAILS
*&---------------------------------------------------------------------*
*       Fetch Distribution list and Subject for the variants
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form GET_VAR_DETAILS .

  select from zvarsub into table it_varsub.

endform.                    " GET_VAR_DETAILS
*&---------------------------------------------------------------------*
*&      Form  GET_SPOOL_FOR_JOB
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
form GET_SPOOL_FOR_JOB .

* get the job and spool information from table
SELECT FROM tbtcp INTO TABLE it_tbtcp WHERE jobname p_jobnam.

* sort table to get latest spool ID.
SORT it_tbtcp DESCENDING BY sdldate jobcount.

* read first line to get jobcount id.
READ TABLE it_tbtcp INTO wa_tbtcp INDEX 1.

* Delete items from table which are not required
DELETE it_tbtcp WHERE jobcount NE wa_tbtcp-jobcount.

clear wa_tbtcp.

endform.                    " GET_SPOOL_FOR_JOB

*&---------------------------------------------------------------------*
*&      Form  create_pdf
*&---------------------------------------------------------------------*
* Create PDF Content
* 1) get attributes of spool request
* 2) convert spool request to PDF dependent on document type
*----------------------------------------------------------------------*
form create_pdf.

  data rq       type tsp01.
  data bin_size type i.
  data dummy    type table of rspoattr.

  clearrqbin_sizedummypdf_xstringpdf_size.

*   ------------ get attributes of spool request ---------------------
  call function 'RSPO_GET_ATTRIBUTES_SPOOLJOB'
    exporting
      rqident     rqident
    importing
      rq          rq
    tables
      attributes  dummy
    exceptions
      no_such_job 1
      others      2.
  if sy-subrc <> 0.
    message e126(powith rqident.
  endif.

*   --- convert spool request into PDF, dependent on document type ---

    call function 'CONVERT_ABAPSPOOLJOB_2_PDF'
      exporting
        src_spoolid              rqident
        no_dialog                'X'
        pdf_destination          'X'
        no_background            'X'
      importing
        pdf_bytecount            bin_size
        bin_file                 pdf_xstring
      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.
      message e712(powith sy-subrc 'CONVERT_ABAPSPOOLJOB_2_PDF'.
    endif.

  pdf_size bin_size.
endform.                    "create_pdf

*&---------------------------------------------------------------------*
*&      Form  send Email with PDF attachment
*&---------------------------------------------------------------------*
form send_mail.

  try.

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

*     -------- create and set document -------------------------------
      pdf_content cl_document_bcs=>xstring_to_solixpdf_xstring ).

      clear wa_varsub.

*  get distribution list and subject line for the varinats.

      read table it_varsub into wa_varsub with key variant wa_tbtcp-variant.

      if wa_varsub is initial.
       write'There is no varinat maintained in the zvarsub table'.
       exit.
      else.
       clearsublinelm_dist.

       subline wa_varsub-subject.
       lm_dist wa_varsub-distlist.
      endif.

*  create PDF document.
      document cl_document_bcs=>create_document(
            i_type    'PDF'
            i_hex     pdf_content
            i_length  pdf_size
            i_subject subline ).  "#EC NOTEXT

*     add document object to send request
      send_request->set_documentdocument ).

*     --------- add recipient (e-mail address) -----------------------
*     create recipient object
*      recipient = cl_cam_address_bcs=>create_internet_address( mailto ).
       recipient cl_distributionlist_bcs=>GETU_PERSISTENT(
                   i_dliname lm_dist
                   i_private space ).

*     add recipient object to send request
      send_request->add_recipientrecipient ).

* Set that you don't need a Return Status E-mail

      send_request->set_status_attributes(
                    i_requested_status 'E' ).

* set send immediately flag

      send_request->set_send_immediately'X' ).
*     ---------- send document ---------------------------------------
      sent_to_all send_request->sendi_with_error_screen 'X' ).

      commit work.

      if sent_to_all is initial.
        message i500(sbcomswith lm_dist.
      else.
        message s022(so).
      endif.

*   ------------ exception handling ----------------------------------
*   replace this rudimentary exception handling with your own one !!!
    catch cx_bcs into bcs_exception.
      message i865(sowith bcs_exception->error_type.
  endtry.

endform.                    "send

 

Comments

  1. Nice Post for Readers. Otherwise if any One Want to Fill Your Career GAP Else Want Genuine Experience Certificate So Contact us & Get all Details-9599119376 Or Visit Website- https://experiencecertificates.com/experience-certificate-provider-in-bangalore.html

    Best Consultant for Experience Certificate Providers in Bangalore, India
    Best Consultant for Experience Certificate Providers in Hyderabad, India
    Best Consultant for Experience Certificate Providers in Chennai, India

    ReplyDelete

Post a Comment

Popular posts from this blog

Add additional fields in VA05 report

Totals table for ACDOCA in S/4 HANA - ACDOCT

FAGLL03 / FBL1N / FBL3N / FBL5N not showing custom fields