Tuesday, 28 July 2015

PWB DOWNLOAD PDF AND SEND A MAIL

&---------------------------------------------------------------------*
*&   Form USER_EXIT_ZISU_END_EXITN
*&---------------------------------------------------------------------*
*&   This exit is processed AFTER the hierarchy has been processed.
*&
*&   Use this Exit to check or set
*    final settings in the form, if required.
*&---------------------------------------------------------------------*
FORM USER_EXIT_ZISU_END_EXITN .


DATA : V_BIN_FILESIZE TYPE I.
DATA :I_PDF LIKE TLINE OCCURS 100 WITH HEADER LINE.
data :  v_name                   TYPE string,
      v_path                   TYPE string,
      v_fullpath               TYPE string,
      v_filter                 TYPE string,
      v_uact                   TYPE i,
      v_guiobj                 TYPE REF TO cl_gui_frontend_services,
      v_filename               TYPE string,
      v_fm_name                TYPE rs38l_fnam.

CALL FUNCTION 'CONVERT_OTF'
EXPORTING
FORMAT = 'PDF'
MAX_LINEWIDTH = 132
IMPORTING
BIN_FILESIZE = V_BIN_FILESIZE
* BIN_FILE =
TABLES
OTF = GT_OTF[]
LINES = I_PDF
.


  CONCATENATE 'smrt' '.pdf' INTO v_name.
  CREATE OBJECT v_guiobj.
  CALL METHOD v_guiobj->file_save_dialog
    EXPORTING
      default_extension  = 'pdf'
      default_file_name  = v_name
      file_filter        = v_filter
    CHANGING
      filename           = v_name
      path               = v_path
      fullpath           = v_fullpath
      user_action        = v_uact.
  IF v_uact = v_guiobj->action_cancel.
    EXIT.
  ENDIF.


CALL FUNCTION 'GUI_DOWNLOAD'
  EXPORTING
    BIN_FILESIZE = V_BIN_FILESIZE
    FILENAME     = v_name "'C:\Users\VIJAY\Desktop\VI.pdf'
    FILETYPE     = 'BIN'
  TABLES
    DATA_TAB     = I_PDF.

**************************************mail code


DATA:
        g_spool_no TYPE tsp01-rqident.

  DATA :
      g_sent_to_all   TYPE sonv-flag,
      g_tab_lines     TYPE i.
  "Types
  TYPES:
      t_document_data  TYPE  sodocchgi1,
      t_packing_list   TYPE  sopcklsti1,
      t_attachment     TYPE  solisti1,
      t_body_msg       TYPE  solisti1,
      t_receivers      TYPE  somlreci1,
      t_pdf            TYPE  tline.
  "Workareas
  DATA :

      wa_fkkop         TYPE  fkkop,
      wa_vkk_doc_id    TYPE  isu21_t_vkk_doc_id,

      w_document_data  TYPE  t_document_data,
      w_packing_list   TYPE  t_packing_list,
      w_attachment     TYPE  t_attachment,
      w_body_msg       TYPE  t_body_msg,
      w_receivers      TYPE  t_receivers,
      w_pdf            TYPE  t_pdf.
  "Internal Tables
  DATA :

      it_fkkop         TYPE STANDARD TABLE OF fkkop,
      it_vkk_doc_id    TYPE STANDARD TABLE OF isu21_t_vkk_doc_id,

      i_document_data  TYPE STANDARD TABLE OF t_document_data,
      i_packing_list   TYPE STANDARD TABLE OF t_packing_list,
      i_attachment     TYPE STANDARD TABLE OF t_attachment,
      i_body_msg       TYPE STANDARD TABLE OF t_body_msg,
      i_receivers      TYPE STANDARD TABLE OF t_receivers.
*      i_pdf            TYPE STANDARD TABLE OF t_pdf.




*Write the data.

  DATA:
  l_lay    TYPE pri_params-paart,
  l_lines  TYPE pri_params-linct,
  l_cols   TYPE pri_params-linsz,
  l_val    TYPE c.
*Types
  TYPES:
  t_pripar TYPE pri_params,
  t_arcpar TYPE arc_params.
  "Work areas
  DATA:
  lw_pripar TYPE t_pripar,
  lw_arcpar TYPE t_arcpar.


CALL FUNCTION 'SX_TABLE_LINE_WIDTH_CHANGE'
    EXPORTING
      line_width_src              = 134
      line_width_dst              = 255
    TABLES
      content_in                  = i_pdf
      content_out                 = i_attachment
    EXCEPTIONS
      err_line_width_src_too_long = 1
      err_line_width_dst_too_long = 2
      err_conv_failed             = 3
      OTHERS                      = 4.
  IF sy-subrc NE 0.
    MESSAGE s000(0k) WITH 'Conversion Failed'.
    EXIT.
  ENDIF.





  "Write Packing List for Body
  DESCRIBE TABLE i_body_msg LINES g_tab_lines.
  w_packing_list-head_start = 1.
  w_packing_list-head_num   = 0.
  w_packing_list-body_start = 1.
  w_packing_list-body_num   = g_tab_lines.
  w_packing_list-doc_type   = 'RAW'.
  APPEND w_packing_list TO i_packing_list.
  CLEAR  w_packing_list.

  "Write Packing List for Attachment
  w_packing_list-transf_bin = 'X'.
  w_packing_list-head_start = 1.
  w_packing_list-head_num   = 1.
  w_packing_list-body_start = 1.
  DESCRIBE TABLE i_attachment LINES w_packing_list-body_num.
  w_packing_list-doc_type   = 'PDF'.
  w_packing_list-obj_descr  = 'PDF Attachment'.
  w_packing_list-obj_name   = 'PDF_ATTACHMENT'.
  w_packing_list-doc_size   = w_packing_list-body_num * 255.
  APPEND w_packing_list TO i_packing_list.
  CLEAR  w_packing_list.

  "Fill the document data and get size of attachment
  w_document_data-obj_langu  = sy-langu.
  READ TABLE i_attachment INTO w_attachment INDEX g_tab_lines.
  w_document_data-doc_size = ( g_tab_lines - 1 ) * 255 + STRLEN( w_attachment ).

  "Receivers List.
  w_receivers-rec_type   = 'B'.  " 'U' to Internet address
  w_receivers-receiver   = sy-uname ." p_mail.
  w_receivers-com_type   = 'INT'.
  w_receivers-notif_del  = 'X'.
  w_receivers-notif_ndel = 'X'.
  APPEND w_receivers TO i_receivers .
  CLEAR:w_receivers.

  "Function module to send mail to Recipients
  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      document_data              = w_document_data
      put_in_outbox              = 'X'
      commit_work                = 'X'
    IMPORTING
      sent_to_all                = g_sent_to_all
    TABLES
      packing_list               = i_packing_list
      contents_bin               = i_attachment
      contents_txt               = i_body_msg
      receivers                  = i_receivers
    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.

  IF sy-subrc = 0 .
*    MESSAGE i303(me) WITH 'Mail has been Successfully Sent.'.
  ELSE.
    WAIT UP TO 2 SECONDS.
    "This program starts the SAPconnect send process.
    SUBMIT rsconn01 WITH mode = 'INT'
    WITH output = 'X'
    AND RETURN.
  ENDIF.


************************************
endform.