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

Here is the code to send the Smartform to mail as PDF attachment.

REPORT ztest_nreddy_pdf_mail.
* Internal Table declarations
DATA: i_otf TYPE itcoo OCCURS 0 WITH HEADER LINE,
      i_tline TYPE TABLE OF tline WITH HEADER LINE,
      i_receivers TYPE TABLE OF somlreci1 WITH HEADER LINE,
      i_record LIKE solisti1 OCCURS 0 WITH HEADER LINE,
* Objects to send mail.
      i_objpack LIKE sopcklsti1 OCCURS 0 WITH HEADER LINE,
      i_objtxt LIKE solisti1 OCCURS 0 WITH HEADER LINE,
      i_objbin LIKE solisti1 OCCURS 0 WITH HEADER LINE,
      i_reclist LIKE somlreci1 OCCURS 0 WITH HEADER LINE,
* Work Area declarations
      wa_objhead TYPE soli_tab,
      w_ctrlop TYPE ssfctrlop,
      w_compop TYPE ssfcompop,
      w_return TYPE ssfcrescl,
      wa_doc_chng TYPE sodocchgi1,
      w_data TYPE sodocchgi1,
      wa_buffer TYPE string, "To convert from 132 to 255
* Variables declarations
      v_form_name TYPE rs38l_fnam,
      v_len_in LIKE sood-objlen,
      v_len_out LIKE sood-objlen,
      v_len_outn TYPE i,
      v_lines_txt TYPE i,
      v_lines_bin TYPE i.

CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
  EXPORTING
    formname           = 'ZTEST'
  IMPORTING
    fm_name            = v_form_name
  EXCEPTIONS
    no_form            = 1
    no_function_module = 2
    OTHERS             = 3.
IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
  WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
w_ctrlop-getotf = 'X'.
w_ctrlop-no_dialog = 'X'.
w_compop-tdnoprev = 'X'.
CALL FUNCTION v_form_name
  EXPORTING
    control_parameters = w_ctrlop
    output_options     = w_compop
    user_settings      = 'X'
  IMPORTING
    job_output_info    = w_return
  EXCEPTIONS
    formatting_error   = 1
    internal_error     = 2
    send_error         = 3
    user_canceled      = 4
    OTHERS             = 5.
IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
  WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
i_otf[] = w_return-otfdata[].
CALL FUNCTION 'CONVERT_OTF'
  EXPORTING
    format                = 'PDF'
    max_linewidth         = 132
  IMPORTING
    bin_filesize          = v_len_in
  TABLES
    otf                   = i_otf
    lines                 = i_tline
  EXCEPTIONS
    err_max_linewidth     = 1
    err_format            = 2
    err_conv_not_possible = 3
    OTHERS                = 4.
IF sy-subrc <> 0.
ENDIF.
LOOP AT i_tline.
  TRANSLATE i_tline USING '~'.
  CONCATENATE wa_buffer i_tline INTO wa_buffer.
ENDLOOP.
TRANSLATE wa_buffer USING '~'.
DO.
  i_record = wa_buffer.
  APPEND i_record.
  SHIFT wa_buffer LEFT BY 255 PLACES.
  IF wa_buffer IS INITIAL.
    EXIT.
  ENDIF.
ENDDO.
* Attachment
REFRESH: i_reclist,
          i_objtxt,
          i_objbin,
          i_objpack.
CLEAR wa_objhead.
i_objbin[] = i_record[].
* Create Message Body Title and Description
i_objtxt = 'test with pdf-Attachment!'.
APPEND i_objtxt.
DESCRIBE TABLE i_objtxt LINES v_lines_txt.
READ TABLE i_objtxt INDEX v_lines_txt.
wa_doc_chng-obj_name = 'smartform'.
wa_doc_chng-expiry_dat = sy-datum + 10.
wa_doc_chng-obj_descr = 'smartform'.
wa_doc_chng-sensitivty = 'F'.
wa_doc_chng-doc_size = v_lines_txt * 255.
* Main Text
CLEAR i_objpack-transf_bin.
i_objpack-head_start = 1.
i_objpack-head_num = 0.
i_objpack-body_start = 1.
i_objpack-body_num = v_lines_txt.
i_objpack-doc_type = 'RAW'.
APPEND i_objpack.
* Attachment (pdf-Attachment)
i_objpack-transf_bin = 'X'.
i_objpack-head_start = 1.
i_objpack-head_num = 0.
i_objpack-body_start = 1.
DESCRIBE TABLE i_objbin LINES v_lines_bin.
READ TABLE i_objbin INDEX v_lines_bin.
i_objpack-doc_size = v_lines_bin * 255 .
i_objpack-body_num = v_lines_bin.
i_objpack-doc_type = 'PDF'.
i_objpack-obj_name = 'smart'.
i_objpack-obj_descr = 'test'.
APPEND i_objpack.
CLEAR i_reclist.
i_reclist-receiver = 'nareshreddy.k@gmail.com'.
i_reclist-rec_type = 'U'.
APPEND i_reclist.
CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
  EXPORTING
    document_data              = wa_doc_chng
    put_in_outbox              = 'X'
    commit_work                = 'X'
  TABLES
    packing_list               = i_objpack
    object_header              = wa_objhead
    contents_bin               = i_objbin
    contents_txt               = i_objtxt
    receivers                  = i_reclist
  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.
  WRITE:/ 'Error When Sending the File', sy-subrc.
ELSE.
  WRITE:/ 'Mail sent'.
ENDIF.
 

If you want to send some text as Body of the Mail then follow this once.

When you are calling the FM 'SO_NEW_DOCUMENT_ATT_SEND_API1'.. points to remember

  1. You have to pass the body of content in table CONTENTS_TXT(ia m using I_OBJBIN) (each line a record) then. Suppose i have appended 11 records to the table CONTENTS_TXT
  2. PACKING_LIST(i am using I_OBJPACK) table you have to append a record as follows
i_objpack-transf_bin = ' '.
i_objpack-head_start = 000000000000001.
i_objpack-head_num = 000000000000001.
i_objpack-body_start = 000000000000002.
i_objpack-body_num = 000000000000010.
i_objpack-doc_type = 'RAW'.
APPEND i_objpack.
 

By the above code system treat the first line in table I_OBJBIN as header and the 2nd line to 10 lines tread as body.

CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
  EXPORTING
    document_data              = wa_doc_chng
    put_in_outbox              = 'X'
  TABLES
    packing_list               = i_objpack
    object_header              = wa_objhead
    contents_bin               = i_objbin
    contents_txt               = i_objtxt
    receivers                  = i_reclist
  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.
 

9 Comments

  1. Guest

    Hi All

    If you are getting empty or corrupted PDF files it might be because of this. Here is the original coding as it comes using copy/paste.

    LOOP AT I_TLINE.
    TRANSLATE I_TLINE USING '~'.
    CONCATENATE WA_BUFFER I_TLINE INTO WA_BUFFER.
    ENDLOOP.
    TRANSLATE WA_BUFFER USING '~'. 

    This did not work for me. If you change both "translate" lines to have a blank space before (or in the second line after) the swung dash/tilde it works. The blank space before and after the swung dash (or tilde) are important.

  2. Former Member

    Hi All,

    Great code. I need to avoid the window to set the printer device..and set it one by default by code...

    Is this possible? 

    Regards and thanks in advance.

    Mon

  3. Guest

    As Harry pointed out above, this code did not 100% work.  He had to add a space in front of the tilde in the first translate command, then a space after the tilde in the second translate command, such that it looked like this:

    LOOP AT I_TLINE.
      TRANSLATE I_TLINE USING ' ~'.
      CONCATENATE WA_BUFFER I_TLINE INTO WA_BUFFER.
    ENDLOOP.
    TRANSLATE WA_BUFFER USING '~ '.

    That got me closer, but still not 100%.  I was still getting errors from the pdf reader saying "an unrecognized token" had been received, and it caused my pdf to be corrupted.  Eventually, I figured out that this whole maneuver with the tilde was being done in order to maintain proper spacing when moving from I_TLINE into the WA_BUFFER area.  As we've all probably found out the hard way with SAP, when you move data that contains spaces from one area to another, it tends to drop the spaces and cram everything together rather than maintaining the orginal spacing.  So the tilde was being used in the example to take the place of a space until the data had been moved into the work area, at which point the tildes were translated back into spaces.  But in my case, the problem was that the OTFDATA being stored in I_TLINE already contained tildes that were part of the data.  So when I translated all the tildes, I got all the ones that used to be spaces, but I also turned some tildes into spaces that truly should have remained tildes.  This is what was causing my pdf to be corrupted.  I struggled with this for a couple of days and finally came up with some logic that worked.  I should note that I found I could control the spacing issues better by handling the individual columns of table I_TLINE (I_TLINE-TDFORMAT and I_TLINE-TDLINE) rather than handling the whole table in one statement.  So that's why I've addressed them separately in the code below.

    DATA: wa_tdformat TYPE STRING,
    wa_tdline TYPE STRING,
    length TYPE I.

    LOOP AT I_TLINE.
      CLEAR: wa_tdformat, length.
      wa_tdformat = I_TLINE-TDFORMAT.
      length = STRLEN( wa_tdformat ).
      IF length < 2.
        CONCATENATE wa_tdformat 'hold' INTO wa_tdformat.
      ENDIF.
      WHILE wa_tdformat CA ' '.
        REPLACE ' ' WITH 'hold' INTO wa_tdformat.
      ENDWHILE.

      CLEAR: wa_tdline, length.
      wa_tdline = I_TLINE-TDLINE.
      length = STRLEN( wa_tdline ).
      IF length < 132.
        CONCATENATE wa_tdline 'hold' INTO wa_tdline.
      ENDIF.
      WHILE wa_tdline CA ' '.
        REPLACE ' ' WITH 'hold' INTO wa_tdline.
      ENDWHILE.
      CONCATENATE wa_buffer wa_tdformat wa_tdline INTO wa_buffer.
    ENDLOOP.

    WHILE wa_buffer CS 'hold'.
      REPLACE 'hold' WITH ' ' INTO wa_buffer.
    ENDWHILE.

    DO.
      i_record = wa_buffer.
      APPEND i_record.
      SHIFT wa_buffer LEFT BY 255 PLACES.
      IF wa_buffer IS INITIAL.
        EXIT.
      ENDIF.
    ENDDO.

    Sorry for the wordiness of the post, just trying to explain it the best I can.  And thanks to the original poster.  His code got me 90% of the way there.

  4. A wonderfull solution. The change , which was declared in the 3rd comment, help me to transfer also graphics . I'm excited.

  5. Former Member

    Hi,

     

    U are seriously helped me too much ,thanks for the snippet,specially 3rd message saved me from sudden dead(smile)

    Love scn

     

    best regards.

  6. HI,

     

    What if I have some interface parameters in the smart form. In that case the FM will have extra import parameters

    1. You will call as usual the generated function module.that does not matter when converting to pdf Format.

  7. this works perfectly thank you so much!