Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

 Classical output as PDF attachment  and send to external email.

Author : Anmol Bhat, Tecnics India.

Description :

The above code can be used to send classical report output as pdf attachment to external email id.

In the above code a GUI status with 'PF01' is created with an

application toolbar button and the fcode for that button is 'SEND'

 
*&---------------------------------------------------------------------*
*& Report  ZAN_TESTPDFSEND
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
*&--Sending output as PDF to external email id's---------------------*

*&Coded By : ANMOL BHAT, INDIA-----------------------------------------*
*In PF01 create 1 button for sending mail, Fcode- SEND
REPORT  ZAN_TESTPDFSEND LINE-SIZE 69 LINE-COUNT 50(3) NO STANDARD PAGE HEADING.
TABLES : VBAK, VBAP.
TYPES : BEGIN OF STR_VBAK,
          VBELN TYPE VBAK-VBELN,
          ERDAT TYPE VBAK-ERDAT,
          KUNNR TYPE VBAK-KUNNR,
          NETWR TYPE VBAK-NETWR,
          WAERK TYPE VBAK-WAERK,
        END OF STR_VBAK.
TYPES : BEGIN OF STR_VBAP,
          VBELN TYPE VBAP-VBELN,
          POSNR TYPE VBAP-POSNR,
          MATNR TYPE VBAP-MATNR,
          KWMENG TYPE VBAP-KWMENG,
          MEINS TYPE VBAP-MEINS,
        END OF STR_VBAP.
DATA : IT_VBAK TYPE TABLE OF STR_VBAK WITH HEADER LINE,
       WA_VBAK TYPE STR_VBAK,
       IT_VBAP TYPE TABLE OF STR_VBAP WITH HEADER LINE.
SELECTION-SCREEN BEGIN OF BLOCK BL1 WITH FRAME TITLE TEXT-001.
SELECT-OPTIONS : S_VBELN FOR VBAK-VBELN,
                 S_ERDAT FOR VBAK-ERDAT.
PARAMETERS : PO_EMAIL TYPE AD_SMTPADR LOWER CASE.
SELECTION-SCREEN END OF BLOCK BL1.
TOP-OF-PAGE.
  PERFORM TOP_OF_PAGE.
START-OF-SELECTION.
*format color 7 INVERSE off.
  SET PF-STATUS 'PF01'.
  PERFORM GET_DATA.
  PERFORM PRINT.
END-OF-PAGE.
  PERFORM END_OF_PAGE.
AT USER-COMMAND.
  CASE SY-UCOMM.
    WHEN 'SEND'.
      PERFORM SEND_PDF.
  ENDCASE.
*&---------------------------------------------------------------------*
*&      Form  top_of_page
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM TOP_OF_PAGE .
  FORMAT COLOR 1 INTENSIFIED ON.
  WRITE : / 'Sales Report' CENTERED.
  IF S_VBELN-HIGH IS INITIAL.
    WRITE : / 'Sales report for sales order', S_VBELN-LOW.
  ELSE.
    WRITE : / 'Sales order report for sales orders from',
              S_VBELN-LOW,'to',S_VBELN-HIGH.
  ENDIF.
  FORMAT COLOR 1 INTENSIFIED OFF.              " top_of_page
ENDFORM.                    " top_of_page
*&---------------------------------------------------------------------*
*&      Form  get_data
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM GET_DATA .
  SELECT VBELN ERDAT KUNNR NETWR WAERK FROM VBAK
          INTO CORRESPONDING FIELDS OF TABLE IT_VBAK
              WHERE VBELN IN S_VBELN
                AND ERDAT IN S_ERDAT.
  IF IT_VBAK[] IS NOT INITIAL.
    SELECT VBELN POSNR MATNR KWMENG MEINS
         FROM VBAP
           INTO CORRESPONDING FIELDS OF TABLE IT_VBAP
            FOR ALL ENTRIES IN IT_VBAK
               WHERE VBELN = IT_VBAK-VBELN.
  ENDIF.
ENDFORM.                    " get_data
*&---------------------------------------------------------------------*
*&      Form  print
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM PRINT .
  LOOP AT IT_VBAK.
    AT FIRST.
      ULINE.
      FORMAT COLOR 4 ON.
      WRITE :/10 'VBELN',21 SY-VLINE,22 'ERDAT',33 SY-VLINE, 34 'KUNNR',
               45 SY-VLINE,46 'NETWR',62 SY-VLINE,63 'WAERK',69 SY-VLINE.
      ULINE.
      FORMAT COLOR 4 OFF.
    ENDAT.
    WA_VBAK = IT_VBAK.
    AT NEW VBELN.
      WRITE : /10 WA_VBAK-VBELN,21 SY-VLINE,22 WA_VBAK-ERDAT,33 SY-VLINE, 34 WA_VBAK-KUNNR,
               45 SY-VLINE,46 WA_VBAK-NETWR,62 SY-VLINE,63 WA_VBAK-WAERK,69 SY-VLINE.
      ULINE.
      LOOP AT IT_VBAP WHERE VBELN = WA_VBAK-VBELN.
        AT FIRST.
          FORMAT COLOR 5 ON.
          WRITE : /22 'POSNR',29 SY-VLINE,30 'MATNR',45 SY-VLINE,
                 46 'KWMENG',62 SY-VLINE,63 'MEINS',69 SY-VLINE.
          ULINE.
          FORMAT COLOR 5 OFF.
        ENDAT.
        WRITE : /22 IT_VBAP-POSNR,29 SY-VLINE,30 IT_VBAP-MATNR,45 SY-VLINE,
                 46 IT_VBAP-KWMENG,62 SY-VLINE,63 IT_VBAP-MEINS,69 SY-VLINE.
        AT LAST.
          ULINE.
        ENDAT.
      ENDLOOP.
      CLEAR WA_VBAK.
    ENDAT.
    AT END OF VBELN.
      ULINE.
    ENDAT.
  ENDLOOP.
ENDFORM.                    " print
*&---------------------------------------------------------------------*
*&      Form  END_OF_PAGE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM END_OF_PAGE .
  FORMAT COLOR 7 ON.
  ULINE.
  WRITE : / 'Page ended Page Number :',SY-PAGNO.
  FORMAT COLOR 7 OFF.
ENDFORM.                    " END_OF_PAGE
*&---------------------------------------------------------------------*
*&      Form  SEND_PDF
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM SEND_PDF .
  DATA: L_PARAMS TYPE PRI_PARAMS,
              L_VALID TYPE STRING,
              W_SPOOL_NR LIKE TSP01-RQIDENT.
  DATA : IT_PDF TYPE TABLE OF TLINE.
  DATA : IT_PDFDATA TYPE TABLE OF SOLISTI1.
  DATA : WA_PDF TYPE TLINE,
     WA_PDFDATA TYPE SOLISTI1,
     I TYPE I,
     J TYPE I,
     K TYPE I,
     L TYPE I.
  DATA: LI_OBJCONT TYPE STANDARD TABLE OF SOLISTI1,
        LI_RECLIST TYPE STANDARD TABLE OF SOMLRECI1,
        LI_OBJPACK TYPE STANDARD TABLE OF SOPCKLSTI1,
        LI_OBJHEAD TYPE STANDARD TABLE OF SOLISTI1,
        LI_CONTENT TYPE STANDARD TABLE OF SOLISTI1,
        LWA_OBJCONT TYPE SOLISTI1,
        LWA_RECLIST TYPE SOMLRECI1,
        LWA_OBJPACK TYPE SOPCKLSTI1,
        LWA_OBJHEAD TYPE SOLISTI1,
        LWA_CONTENT TYPE SOLISTI1,
        LWA_DOC TYPE SODOCCHGI1,
        L_LINES TYPE I,
        OBJ_LIST TYPE TABLE OF  ABAPLIST,
        LWA_LIST TYPE ABAPLIST.
  REFRESH: LI_OBJCONT[], LI_RECLIST[],
            LI_OBJPACK[], LI_OBJHEAD[],
            LI_CONTENT[].
  CLEAR: LWA_OBJCONT, LWA_RECLIST,
         LWA_OBJPACK, LWA_OBJHEAD,
         LWA_CONTENT, LWA_DOC.
**
  CALL FUNCTION 'GET_PRINT_PARAMETERS'
    EXPORTING
      NO_DIALOG      = 'X'
    IMPORTING
      OUT_PARAMETERS = L_PARAMS
      VALID          = L_VALID.
  IF SY-SUBRC <> 0.
  ENDIF.
  DATA: BEGIN OF I_RSPARAMS OCCURS 0.
          INCLUDE STRUCTURE RSPARAMS.
  DATA: END OF I_RSPARAMS.
  CALL FUNCTION 'RS_REFRESH_FROM_SELECTOPTIONS'
    EXPORTING
      CURR_REPORT     = SY-REPID
    TABLES
      SELECTION_TABLE = I_RSPARAMS.
  SUBMIT ZAN_TESTPDFSEND WITH SELECTION-TABLE I_RSPARAMS
                               TO SAP-SPOOL
                               SPOOL PARAMETERS L_PARAMS
                               WITHOUT SPOOL DYNPRO
                               AND RETURN.
  SELECT MAX( RQIDENT ) INTO W_SPOOL_NR FROM TSP01
                             WHERE RQCLIENT = SY-MANDT
                             AND   RQOWNER  = SY-UNAME.
  CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
    EXPORTING
      SRC_SPOOLID = W_SPOOL_NR
    TABLES
      PDF         = IT_PDF.
* TO CONVERT TLINE WITH LENGHT 134 TO 255
  I = 134.          "LENGTH FOR TLINE IN PDF FORMAT.
  LOOP AT IT_PDF INTO WA_PDF.
    MOVE WA_PDF(I) TO WA_PDFDATA+J.
    J = J + I.
    IF J >= 255.         "255 LENGTH FOR solisti1
      APPEND WA_PDFDATA TO IT_PDFDATA.
      CLEAR WA_PDFDATA.
      CLEAR J.
      K = 134 - I.
      IF K > 0.
        MOVE WA_PDF+I(K) TO WA_PDFDATA+0(K).
      ENDIF.
      J = J + K.
    ENDIF.
    L = J + I.
    IF L >= 255.
      I = 255 - J.
    ELSE.
      I = 134.
    ENDIF.
  ENDLOOP.
* END OF CONVERT.
  LI_OBJCONT[] = IT_PDFDATA[].
  LWA_RECLIST-RECEIVER = PO_EMAIL.
  LWA_RECLIST-REC_TYPE = 'U'.
  APPEND LWA_RECLIST TO LI_RECLIST.
  LWA_OBJHEAD = 'SEND OUTPUT AS PDF'.
  APPEND LWA_OBJHEAD TO LI_OBJHEAD.
  LWA_CONTENT = 'Hi'.
  APPEND LWA_CONTENT TO LI_CONTENT.
  CLEAR LWA_CONTENT.
  APPEND LWA_CONTENT TO LI_CONTENT.
  LWA_CONTENT = 'Please find attached document as output in pdf format'. "test anmol
  APPEND LWA_CONTENT TO LI_CONTENT.                                      "test anmol
  CLEAR L_LINES.
  DESCRIBE TABLE LI_CONTENT LINES L_LINES.
  READ TABLE LI_CONTENT INTO LWA_CONTENT INDEX L_LINES.
  LWA_DOC-DOC_SIZE = ( L_LINES - 1 ) * 255 + STRLEN( LWA_CONTENT ).
  LWA_DOC-OBJ_LANGU = 'E'.
  LWA_DOC-OBJ_NAME = 'PDF OUTPUT'.
  LWA_DOC-OBJ_DESCR = 'OUTPUT AS PDF'.
  CLEAR LWA_OBJPACK-TRANSF_BIN.
  LWA_OBJPACK-HEAD_START = 1.
  LWA_OBJPACK-HEAD_NUM = 0.
  LWA_OBJPACK-BODY_START = 1.
  LWA_OBJPACK-BODY_NUM = L_LINES.
  LWA_OBJPACK-DOC_TYPE = 'RAW'.
  APPEND LWA_OBJPACK TO LI_OBJPACK.
  CLEAR: LWA_OBJPACK, L_LINES.
  DESCRIBE TABLE LI_OBJCONT LINES L_LINES.
  READ TABLE LI_OBJCONT INTO LWA_OBJCONT INDEX L_LINES.
  LWA_OBJPACK-DOC_SIZE = ( L_LINES - 1 ) * 255 + STRLEN( LWA_OBJCONT ).
  LWA_OBJPACK-TRANSF_BIN = 'X'.
  LWA_OBJPACK-HEAD_START = 1.
  LWA_OBJPACK-HEAD_NUM = 0.
  LWA_OBJPACK-BODY_START = 1.
  LWA_OBJPACK-BODY_NUM = L_LINES.
  LWA_OBJPACK-DOC_TYPE = 'PDF' .
  LWA_OBJPACK-OBJ_NAME = 'PDF OUTPUT'.
  LWA_OBJPACK-OBJ_DESCR = 'OUTPUT AS PDF'.
  APPEND LWA_OBJPACK TO LI_OBJPACK.
*Sending the mail
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      DOCUMENT_DATA              = LWA_DOC
      PUT_IN_OUTBOX              = 'X'
    TABLES
      PACKING_LIST               = LI_OBJPACK
      OBJECT_HEADER              = LI_OBJHEAD
      CONTENTS_BIN               = LI_OBJCONT
      CONTENTS_TXT               = LI_CONTENT
      RECEIVERS                  = LI_RECLIST
    EXCEPTIONS
      TOO_MANY_RECEIVERS         = 1
      DOCUMENT_NOT_SENT          = 2
      OPERATION_NO_AUTHORIZATION = 4
      OTHERS                     = 99.
  IF SY-SUBRC NE 0.
    WRITE:/ 'Document sending failed'.
  ELSE.
    WRITE:/ 'Document successfully sent'.
    COMMIT WORK.
  ENDIF.
ENDFORM.                    " SEND_PDF