Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

sript pdf mail

Former Member
0 Likes
568

How to convert SAP script to pdf format and send mail? What do we need to do in print program and configuration we need to achive?

Thanks

3 REPLIES 3
Read only

rahulkavuri
Active Contributor
0 Likes
525

The below link explains you how to convert the script to PDF

After you convert the script to PDF, instead of using the old FM's the BCS class to send the mail as an attachment..

the Class BCS offers advanced options to send the mail..

CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'
    EXPORTING
      src_spoolid              = l_spoolno
      no_dialog                = 'X'
    TABLES
      pdf                      = l_ipdf
    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 ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    EXIT .
  ENDIF.

* prepare to mail PDF document
* Subject of message

* Body of message
  CONCATENATE zmmtctoatsheader-buildid(20)
              '     '
              zmmtctoatsheader-cust_state(2)
              ' '
              zmmtctoatsheader-cust_ewo(10)
              zmmtctoatsheader-cust_street(30)
              INTO wa RESPECTING BLANKS.

  APPEND wa TO text.

  CONCATENATE 'The As Built Report is attached for Build ID'
        s_buildid_i ', Revision' v_buildid_rev '.' INTO wa
        SEPARATED BY space.
  APPEND wa TO text.

*  -------------------------------------------*
*  do the move via an internal table
  DATA: BEGIN OF l_itab OCCURS 0,
          val(1) TYPE c,
        END OF l_itab ,
        l_index LIKE l_len .

  LOOP AT l_ipdf INTO l_pdf_line.
    CLEAR l_pos .
    WHILE l_pos < 134 .
      l_itab-val = l_pdf_line+l_pos(1) .
      APPEND l_itab .
      l_pos = l_pos + 1 .
    ENDWHILE .
  ENDLOOP.
  DESCRIBE TABLE l_itab LINES l_len .
  CLEAR l_pos .
  DO .
    IF l_index = l_len .
      IF l_pos < 255 .
        APPEND l_iobjbin .
        CLEAR: l_iobjbin, l_pos .
      ENDIF .
      EXIT .
    ELSE .
      l_index = l_index + 1 .
      READ TABLE l_itab INDEX l_index .
      IF sy-subrc = 0 .
        IF l_pos < 255 .
          l_iobjbin+l_pos(1) = l_itab-val .
          l_pos = l_pos + 1 .
        ELSEIF l_pos = 255 .
          APPEND l_iobjbin .
          CLEAR: l_iobjbin, l_pos .
          l_iobjbin+l_pos(1) = l_itab-val .
          l_pos = l_pos + 1 .
        ENDIF .
      ENDIF .
    ENDIF .
  ENDDO .
*  -------------------------------------------*
** email recepients list
  IF sy-sysid EQ 'P01'.
    DATA v_vbeln TYPE vbeln.
    DATA v_kunnr TYPE kunnr.
    DATA v_prsnr TYPE ad_persnum.
    DATA v_smtp_addr TYPE ad_smtpadr.
    SELECT SINGLE vbeln FROM zmmtctoatsheader INTO v_vbeln
                              WHERE  aufnr = v_aufnr.
    SELECT SINGLE kunnr FROM vbak INTO v_kunnr
                            WHERE vbeln = v_vbeln.
    SELECT SINGLE prsnr FROM knvk INTO v_prsnr
                             WHERE kunnr = v_kunnr
                             AND   name1 = 'ATS_CABLE'.

    SELECT SINGLE smtp_addr FROM adr6 INTO v_smtp_addr
                             WHERE  persnumber = v_prsnr.
    MOVE v_smtp_addr TO l_ireclist-receiver.
    l_ireclist-rec_type = 'U'.
    APPEND l_ireclist.
  ELSE.
    DATA v_charval1 TYPE zcharval1.
    SELECT SINGLE charval1  FROM zcto INTO v_charval1
                          WHERE ctotable  =  'ATS_CABLE'
                           AND  charkey1  =  'CONTACT_EMAIL'.
    MOVE v_charval1 TO l_ireclist-receiver.
    l_ireclist-rec_type = 'U'.
    APPEND l_ireclist.
  ENDIF.

* set the subject of the e-mail

  IF sy-sysid EQ 'P01'.
    CONCATENATE zmmtctoatsheader-buildid
                zmmtctoatsheader-cust_state
                zmmtctoatsheader-cust_ewo
                zmmtctoatsheader-cust_street
                INTO i_subject RESPECTING BLANKS.

  ELSE.
    CONCATENATE '[' sy-sysid 'TEST]'
                zmmtctoatsheader-buildid
                zmmtctoatsheader-cust_state
                space
                zmmtctoatsheader-cust_ewo
                zmmtctoatsheader-cust_street
                INTO i_subject RESPECTING BLANKS.

  ENDIF.

*  pass this value onto the string of the subject, *subject line more than 50 chars
  t_sub = i_subject.

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

* -------- create and set document with attachment ---------------
* create document from internal table with text
      document = cl_document_bcs=>create_document(
      i_type = 'RAW'
      i_text = text
      i_length = '12'
      i_subject = '' ).

*changing the content of the attachment
      binary_content[] = l_iobjbin[].

*change the name of the PDF attachment
      CONCATENATE 'Build ID' s_buildid_i 'Rev' v_buildid_rev
                          INTO i_att_sub SEPARATED BY space.

* add attachment to document
      CALL METHOD document->add_attachment
        EXPORTING
          i_attachment_type    = 'PDF'
          i_attachment_subject = i_att_sub
          i_att_content_hex    = binary_content.

*setting the option to send an e-mail more than 50 characters
      CALL METHOD send_request->set_message_subject
        EXPORTING
          ip_subject = t_sub.

* add document to send request
      CALL METHOD send_request->set_document
        EXPORTING
          i_document = document.

* --------- set sender -------------------------------------------
* note: this is necessary only if you want to set the sender
* different from actual user (SY-UNAME). Otherwise sender is
* set automatically with actual user.

      sender = cl_sapuser_bcs=>create( 'VATSUPPORT' ).
      CALL METHOD send_request->set_sender
        EXPORTING
          i_sender = sender.

*Send the list based on receivers list obtained

      LOOP AT l_ireclist.

        ad_smtpadr = l_ireclist-receiver.

* --------- add recipient (e-mail address) -----------------------
* create recipient - please replace e-mail address !!!
        recipient = cl_cam_address_bcs=>create_internet_address( ad_smtpadr ).

* add recipient with its respective attributes to send request
        CALL METHOD send_request->add_recipient
          EXPORTING
            i_recipient = recipient
            i_express   = 'X'.

      ENDLOOP.

      CALL METHOD send_request->set_status_attributes
        EXPORTING
          i_requested_status = 'E'
          i_status_mail      = 'E'.

* To send the mail immediately
      CALL METHOD send_request->set_send_immediately( 'X' ).

* ---------- send document ---------------------------------------
      CALL METHOD send_request->send( ).

      COMMIT WORK.


* -----------------------------------------------------------
* * exception handling
* -----------------------------------------------------------
* * replace this very rudimentary exception handling
* * with your own one !!!
* -----------------------------------------------------------
    CATCH cx_bcs INTO bcs_exception.

      EXIT.

  ENDTRY.

Read only

Former Member
0 Likes
525

hi ,

REPORT Z_SCRIPT .

DATA: itcpo LIKE itcpo,

tab_lines LIKE sy-tabix.

Variables for EMAIL functionality

DATA: maildata LIKE sodocchgi1.

DATA: mailpack LIKE sopcklsti1 OCCURS 2 WITH HEADER LINE.

DATA: mailhead LIKE solisti1 OCCURS 1 WITH HEADER LINE.

DATA: mailbin LIKE solisti1 OCCURS 10 WITH HEADER LINE.

DATA: mailtxt LIKE solisti1 OCCURS 10 WITH HEADER LINE.

DATA: mailrec LIKE somlrec90 OCCURS 0 WITH HEADER LINE.

DATA: solisti1 LIKE solisti1 OCCURS 0 WITH HEADER LINE.

PERFORM send_form_via_email.

************************************************************************

FORM SEND_FORM_VIA_EMAIL *

************************************************************************

FORM send_form_via_email.

CLEAR: maildata, mailtxt, mailbin, mailpack, mailhead, mailrec.

REFRESH: mailtxt, mailbin, mailpack, mailhead, mailrec.

Creation of the document to be sent File Name

maildata-obj_name = 'TEST'.

Mail Subject

maildata-obj_descr = 'Subject'.

Mail Contents

mailtxt-line = 'Here is your file'.

APPEND mailtxt.

Prepare Packing List

PERFORM prepare_packing_list.

Set recipient - email address here!!!

mailrec-receiver = 'RAVINDRANATH.K@IN.BOSCH.COM'.

mailrec-rec_type = 'U'.

APPEND mailrec.

Sending the document

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'

EXPORTING

document_data = maildata

put_in_outbox = ' '

TABLES

packing_list = mailpack

object_header = mailhead

contents_bin = mailbin

contents_txt = mailtxt

receivers = mailrec

EXCEPTIONS

too_many_receivers = 1

document_not_sent = 2

operation_no_authorization = 4

OTHERS = 99.

ENDFORM.

************************************************************************

Form PREPARE_PACKING_LIST

************************************************************************

FORM prepare_packing_list.

CLEAR: mailpack, mailbin, mailhead.

REFRESH: mailpack, mailbin, mailhead.

DESCRIBE TABLE mailtxt LINES tab_lines.

READ TABLE mailtxt INDEX tab_lines.

maildata-doc_size = ( tab_lines - 1 ) * 255 + STRLEN( mailtxt ).

Creation of the entry for the compressed document

CLEAR mailpack-transf_bin.

mailpack-head_start = 1.

mailpack-head_num = 0.

mailpack-body_start = 1.

mailpack-body_num = tab_lines.

mailpack-doc_type = 'RAW'.

APPEND mailpack.

Creation of the document attachment

This form gets the OTF code from the SAPscript form.

If you already have your OTF code, I believe that you may

be able to skip this form. just do the following code, looping thru

your SOLISTI1 and updating MAILBIN.

PERFORM get_otf_code.

LOOP AT solisti1.

MOVE-CORRESPONDING solisti1 TO mailbin.

APPEND mailbin.

ENDLOOP.

DESCRIBE TABLE mailbin LINES tab_lines.

mailhead = 'TEST.OTF'.

APPEND mailhead.

Creation of the entry for the compressed attachment

mailpack-transf_bin = 'X'.

mailpack-head_start = 1.

mailpack-head_num = 1.

mailpack-body_start = 1.

mailpack-body_num = tab_lines.

mailpack-doc_type = 'OTF'.

mailpack-obj_name = 'TEST'.

mailpack-obj_descr = 'Subject'.

mailpack-doc_size = tab_lines * 255.

APPEND mailpack.

ENDFORM.

************************************************************************

Form GET_OTF_CODE

************************************************************************

FORM get_otf_code.

DATA: BEGIN OF otf OCCURS 0.

INCLUDE STRUCTURE itcoo .

DATA: END OF otf.

DATA: itcpo LIKE itcpo.

DATA: itcpp LIKE itcpp.

CLEAR itcpo.

itcpo-tdgetotf = 'X'.

Start writing OTF code

CALL FUNCTION 'OPEN_FORM'

EXPORTING

form = 'Z08V3_COLLI'

language = sy-langu

options = itcpo

dialog = ' '

EXCEPTIONS

OTHERS = 1.

CALL FUNCTION 'START_FORM'

EXCEPTIONS

error_message = 01

OTHERS = 02.

CALL FUNCTION 'WRITE_FORM'

EXPORTING

window = 'MAIN'

EXCEPTIONS

error_message = 01

OTHERS = 02.

Close up Form and get OTF code

CALL FUNCTION 'END_FORM'

EXCEPTIONS

error_message = 01

OTHERS = 02.

MOVE-CORRESPONDING itcpo TO itcpp.

CALL FUNCTION 'CLOSE_FORM'

IMPORTING

result = itcpp

TABLES

otfdata = otf

EXCEPTIONS

OTHERS = 1.

Move OTF code to structure SOLI form email

CLEAR solisti1. REFRESH solisti1.

LOOP AT otf.

solisti1-line = otf.

APPEND solisti1.

ENDLOOP.

ENDFORM.

Visit this link for SAMple code of SAPSCRIPT to PDF.:

http://www.sapgenie.com/abap/pdf_creation.htm

and check the following weblog for code samples.

/people/sap.user72/blog/2004/11/10/bsphowto-generate-pdf-output-from-a-bsp

chk this one

http://www.sapdevelopment.co.uk/reporting/rep_spooltopdf.htm

some function modules useful would be

CONVERT_OTF

SO_NEW_DOCUMENT_ATT_SEND_API1 to send mails with attachments

plzz refer to this link..it will solve ur problem

regards,

venkat.

Read only

0 Likes
525

Venkat,

I have 4.6c version will it work with it too.

THanks.