2019년 10월 23일 수요일

[ABAP] Smartforms 출력 옵션 및 PDF 출력 및 이메일 첨부 발송하기

  DATA TAB_OTF_FINAL      TYPE ITCOO OCCURS WITH HEADER LINE,
         CONTORL_PARAMETERS TYPE SSFCTRLOP,
         OUTPUT_OPTIONS     TYPE SSFCOMPOP,
         TAB_OTF_DATA       TYPE SSFCRESCL,
         LV_CNT             TYPE I,
         FILE_SIZE          TYPE I,
         ARC_IDX            LIKE TOA_DARA,
         FILE_NAME          TYPE STRING VALUE 'TEST',
         DOCTAB             LIKE DOCS OCCURS WITH HEADER LINE,
         PDF_TAB            LIKE TLINE OCCURS WITH HEADER LINE.

  data : GT_OBJBIN   LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE,
       
        L_TXT(1000),
               TYPE I.

    DESCRIBE TABLE ITAB LINES LV_CNT.

  LOOP AT ITAB.
    CONTORL_PARAMETERS-NO_DIALOG 'X'.  "인쇄출력팝업 표시여부
    CONTORL_PARAMETERS-PREVIEW   SPACE."미리보기 기능
    CONTORL_PARAMETERS-GETOTF    'X'.  "PDF출력시 X
    CONTORL_PARAMETERS-LANGU     'D'.  "Language key
* "복수 페이지 출력시 조정 옵션
* "첫페이지 NO_OPEN   = ' ' / NO_CLOSE  = 'X'
* "중간페이지 NO_OPEN   = 'X' / NO_CLOSE  = 'X'
* "마지막페이지  NO_OPEN   = 'X' / NO_CLOSE  = ' '   
* "한페이지일 경우 NO_OPEN   = ' ' / NO_CLOSE  = ' '
    CONTORL_PARAMETERS-NO_OPEN   ' '.  
    CONTORL_PARAMETERS-NO_CLOSE  ' '.    
   
    OUTPUT_OPTIONS-TDNOPREV      'X'.  "미리보기버튼 비활성
    OUTPUT_OPTIONS-TDIMMED       'X'.  "Print Immediately (Print Parameters)
    OUTPUT_OPTIONS-TDDEST        'LOCL'"Spool: Output device
    ...
   
    IF LV_CNT NE 1.
      CONTORL_PARAMETERS-NO_OPEN   'X'.
      CONTORL_PARAMETERS-NO_CLOSE  'X'.
      AT FIRST.
        CONTORL_PARAMETERS-NO_OPEN   SPACE.
        CONTORL_PARAMETERS-NO_CLOSE  'X'.
      ENDAT.
      AT LAST.
        CONTORL_PARAMETERS-NO_OPEN   'X'.
        CONTORL_PARAMETERS-NO_CLOSE  SPACE.
      ENDAT.      
    ENDIF.
   
    CALL FUNCTION FUNCTION
      EXPORTING
        CONTROL_PARAMETERS CONTORL_PARAMETERS
        OUTPUT_OPTIONS     OUTPUT_OPTIONS
        GS_HEAD            LS_HEAD
      IMPORTING
        JOB_OUTPUT_INFO    TAB_OTF_DATA
      TABLES
        ITEM               LT_ITEM
        TEXT               LT_TEXT
      EXCEPTIONS
        FORMATTING_ERROR   1
        INTERNAL_ERROR     2
        SEND_ERROR         3
        USER_CANCELED      4.
   ...
   ENDLOOP.
  
*  "PDF 전환시 TAB_OTF_DATA를 받아 포맷 변경한다.
    CLEARTAB_OTF_FINAL[]FILE_SIZEPDF_TAB[].
    TAB_OTF_FINAL[] TAB_OTF_DATA-OTFDATA[].
   
    CALL FUNCTION 'CONVERT_OTF_2_PDF'
     EXPORTING
*       USE_OTF_MC_CMD               = 'X'
       ARCHIVE_INDEX                 ARC_IDX
     IMPORTING
       BIN_FILESIZE                  =  FILE_SIZE
      TABLES
        OTF                          TAB_OTF_DATA-OTFDATA
        DOCTAB_ARCHIVE               DOCTAB
        LINES                        PDF_TAB
     EXCEPTIONS
       ERR_CONV_NOT_POSSIBLE        1
       ERR_OTF_MC_NOENDMARKER       2
       OTHERS                       3.

    DATATYPE I,
          N TYPE I,
          L_TXT(1000),
          LV_LINES LIKE SY-TABIX,
          S_MAIL_H   LIKE SODOCCHGI1,
          T_OBJBIN   LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE,
          T_OBJCON   LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE,
          T_OBJPACK  LIKE SOPCKLSTI1 OCCURS WITH HEADER LINE,
          T_RECEIVER LIKE SOMLRECI1 OCCURS WITH HEADER LINE,
          V_DOC_SIZE LIKE SY-TABIX..

* Convert pdf(134) to binary(255) file.
    LOOP AT PDF_TAB.
      L_TXT+I PDF_TAB.
      ADD 134 TO I.
      IF >= 255.
        GT_OBJBIN L_TXT.
        APPEND GT_OBJBIN.
        SUBTRACT 255 FROM I.
        L_TXT L_TXT+255.
      ENDIF.
    ENDLOOP.
    IF <> 0.
      GT_OBJBIN L_TXT.
      APPEND GT_OBJBIN.
    ENDIF.

* "PDF 파일 출력시    
    CALL FUNCTION 'GUI_DOWNLOAD'
        EXPORTING
          BIN_FILESIZE                    FILE_SIZE
          FILENAME                        FILE_NAME
          FILETYPE                        'BIN'
        TABLES
          DATA_TAB                        PDF_TAB
       EXCEPTIONS
         FILE_WRITE_ERROR                1
         NO_BATCH                        2
         GUI_REFUSE_FILETRANSFER         3
         INVALID_TYPE                    4
         NO_AUTHORITY                    5
         UNKNOWN_ERROR                   6
         HEADER_NOT_ALLOWED              7
         SEPARATOR_NOT_ALLOWED           8
         FILESIZE_NOT_ALLOWED            9
         HEADER_TOO_LONG                 10
         DP_ERROR_CREATE                 11
         DP_ERROR_SEND                   12
         DP_ERROR_WRITE                  13
         UNKNOWN_DP_ERROR                14
         ACCESS_DENIED                   15
         DP_OUT_OF_MEMORY                16
         DISK_FULL                       17
         DP_TIMEOUT                      18
         FILE_NOT_FOUND                  19
         DATAPROVIDER_EXCEPTION          20
         CONTROL_FLUSH_ERROR             21
         OTHERS                          22.
   
* "PDF 파일을 첨부한 이메일로 보내기
 
    DATA T_RECEIVER LIKE SOMLRECI1 OCCURS WITH HEADER LINE,
           T_OBJCON   LIKE SOLISTI1 OCCURS 10 WITH HEADER LINE.

*  "수신자 정보(발신자는 SAP ID 설정값임)
    T_RECEIVER-RECEIVER   =  'abc@xxx.com'
    T_RECEIVER-REC_TYPE   =  'U'.
    T_RECEIVER-EXPRESS    =  'X'.
    APPEND T_RECEIVERCLEAR T_RECEIVER.
   
*   "메일 본문 내용
    T_OBJCON-LINE '메일 본문입니다.'.
    APPEND T_OBJCONCLEAR T_OBJCON.
    APPEND T_OBJCON.
    GT_OBJCON-LINE '메일 본문 2라인입니다.'.
    APPEND T_OBJCONCLEAR T_OBJCON.
   
    DESCRIBE TABLE T_OBJCON LINES LV_LINES.
   
*& 메일 HEADER
    CLEAR L_TXT.

    L_txt '제목'.
    S_MAIL_H-OBJ_NAME     =  'MESSAGE'.
    S_MAIL_H-OBJ_DESCR    =  L_TXT.
    S_MAIL_H-OBJ_LANGU    =  'E'.
    S_MAIL_H-SENSITIVTY   =  'O'.
    "'O' : Standard 'P': Confidential
    S_MAIL_H-OBJ_PRIO     =  '1'.
    S_MAIL_H-NO_CHANGE    =  'X'.
    S_MAIL_H-PRIORITY     =  '1'.
*  gs_head-doc_size = gv_lines * 255.

    T_OBJPACK-HEAD_START 1.
    T_OBJPACK-HEAD_NUM 0.
    T_OBJPACK-BODY_START 1.
    T_OBJPACK-BODY_NUM LV_LINES.
    T_OBJPACK-DOC_TYPE 'RAW'.

    APPEND T_OBJPACKCLEAR T_OBJPACK.

*& 메일 첨부파일에 관련된 사항을 내용을 추가
    DESCRIBE TABLE GT_OBJBIN LINES LV_LINES.
    T_OBJPACK-TRANSF_BIN 'X'.
    T_OBJPACK-HEAD_START 1.
    T_OBJPACK-HEAD_NUM 1.
    T_OBJPACK-BODY_START 1.
    T_OBJPACK-BODY_NUM LV_LINES.
    T_OBJPACK-DOC_TYPE 'PDF'.
    T_OBJPACK-OBJ_NAME 'Attachment'.
*    GT_OBJPACK-OBJ_DESCR = 'ATT_SMART'.
    T_OBJPACK-OBJ_DESCR FILE_NAME .   "첨부파일 제목

    READ TABLE T_OBJBIN INDEX LV_LINES.

    V_DOC_SIZE LV_LINES * 255.

*  "SIZE = ( LINES - 1 ) * 255 + STRLEN( T_OBJBIN ).
    T_OBJPACK-DOC_SIZE V_DOC_SIZE.

    APPEND T_OBJPACKCLEAR T_OBJPACK.

*  "메일을 전송
    CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
      EXPORTING
        DOCUMENT_DATA              S_MAIL_H     
        PUT_IN_OUTBOX              'X'
        COMMIT_WORK                'X'
      TABLES
        PACKING_LIST               T_OBJPACK
        CONTENTS_BIN               T_OBJBIN  
        CONTENTS_TXT               T_OBJCON  
        RECEIVERS                  T_RECEIVER
      EXCEPTIONS
        TOO_MANY_RECEIVERS         1
        DOCUMENT_NOT_SENT          2
        DOCUMENT_TYPE_NOT_EXIST    3
        OPERATION_NO_AUTHORIZATION 4
        PARAMETER_ERROR            5
        X_ERROR                    6
        ENQUEUE_ERROR              7
        OTHERS                     8.

 
     SUBMIT RSCONN01 WITH MODE 'INT'
                     AND RETURN.
 

댓글 없음:

댓글 쓰기