cancel
Showing results for 
Search instead for 
Did you mean: 

Converting to OTF and emailing

Former Member
0 Kudos

I am using the below code to convert a SAP output to OTF and then send in an email as an attachement. The email, works and the attachement is added but when I try to open the pdf attachement I get an error saying the file can not open or must be damaged. Other outputs work fine, I can't see what I am doing wrong?

CALL FUNCTION 'CONVERT_OTF'
    EXPORTING
      format                = 'PDF'
*      max_linewidth         = 132
    IMPORTING
      bin_filesize          = gv_binfilesize
    TABLES
      otf                   = gt_otf
      lines                 = gt_tline
    EXCEPTIONS
      err_max_linewidth     = 1
      err_format            = 2
      err_conv_not_possible = 3
      err_bad_otf           = 4.
 IF SY-SUBRC  <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
    WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


  LOOP AT GT_TLINE.
    TRANSLATE GT_TLINE USING '~'.
    CONCATENATE GS_BUFFER GT_TLINE INTO GS_BUFFER.
  ENDLOOP.

  TRANSLATE GS_BUFFER USING '~'.

  DO.
    GT_RECORD = GS_BUFFER.
    APPEND GT_RECORD.
    SHIFT GS_BUFFER LEFT BY 255 PLACES.
    IF GS_BUFFER IS INITIAL.
      EXIT.
    ENDIF.
  ENDDO.

  REFRESH: GT_RECLIST,
           GT_OBJTXT,
           GT_OBJBIN,
           GT_OBJPACK.

  CLEAR GS_OBJHEAD.

  GT_OBJBIN[] = GT_RECORD[].

  DESCRIBE TABLE GT_OBJBIN LINES LV_LINES_BIN.

  gs_doc_chng-obj_name   = 'Test Order Confirmation'.
  gs_doc_chng-expiry_dat = sy-datum + 10 .


* email body
  CONCATENATE 'Please find your Order Confirmation for'
              'your reference (see attached)'
               INTO gt_objtxt SEPARATED BY space.
  APPEND gt_objtxt.

  DESCRIBE TABLE gt_objtxt LINES lv_lines_txt.

  gs_doc_chng-obj_descr  = 'Order Confirmation'.
  gs_doc_chng-sensitivty = 'F'.
  gs_doc_chng-doc_size   = lv_lines_txt * 255.

  CLEAR gt_objpack-transf_bin.

  gt_objpack-head_start = 1.
  gt_objpack-head_num   = 0.
  gt_objpack-body_start = 1.
  gt_objpack-body_num   = lv_lines_txt.
  gt_objpack-doc_type   = 'RAW'.
  APPEND gt_objpack.

  gt_objpack-transf_bin = 'X'.
  gt_objpack-head_start = 1.

  gt_objpack-head_num   = 1.
  gt_objpack-body_start = 1.
  gt_objpack-body_num   = lv_lines_bin.
  gt_objpack-doc_type   = 'PDF'.
  gt_objpack-obj_name   = 'Order'.
  gt_objpack-obj_descr  = 'Order.pdf'.
*  gt_objpack-doc_size   = lv_lines_bin * 255.
  gt_objpack-doc_size   =  ( lv_lines_bin - 1 ) *
                            255 + STRLEN( GT_OBJBIN ).
  APPEND gt_objpack.

  CLEAR gt_reclist.

  gt_reclist-receiver = lv_email.
  gt_reclist-express  = 'X'.
  gt_reclist-rec_type = 'U'.
  APPEND gt_reclist.


  CALL FUNCTION 'SO_NEW_DOCUMENT_ATT_SEND_API1'
    EXPORTING
      document_data              = gs_doc_chng
      put_in_outbox              = 'X'
      commit_work                = 'X'
    TABLES
      packing_list               = gt_objpack
      contents_bin               = gt_objbin
      object_header              = gs_objhead
      contents_txt               = gt_objtxt
      receivers                  = gt_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 NE 0.
    WRITE:/ 'Error When Sending the File', sy-subrc.
  ENDIF.

Edited by: showthousand on May 11, 2011 7:00 AM

Accepted Solutions (1)

Accepted Solutions (1)

ravi_lanjewar
Contributor
0 Kudos

Hi,

Change source code line


gt_objpack-doc_size   =  ( lv_lines_bin - 1 ) *   255 + STRLEN( GT_OBJBIN ).

As


gt_objpack-doc_size   =   lv_lines_bin  *   255 .

Answers (3)

Answers (3)

Former Member
0 Kudos

I went with the BCS class option using the below code:

DATA: lv_email TYPE adr6-smtp_addr.

DATA: lv_form_name TYPE rs38l_fnam,

lv_len_in LIKE sood-objlen,

lv_len_out LIKE sood-objlen,

lv_len_outn TYPE i,

lv_lines_txt TYPE i,

lv_lines_bin TYPE i,

lv_subject TYPE so_obj_des.

  • BCS data

DATA send_request TYPE REF TO cl_bcs.

DATA text TYPE bcsy_text.

DATA document TYPE REF TO cl_document_bcs.

DATA recipient TYPE REF TO if_recipient_bcs.

DATA: bcs_exception TYPE REF TO cx_bcs.

DATA sent_to_all TYPE os_boolean.

DATA pdf_content TYPE solix_tab.

DATA lp_pdf_size TYPE so_obj_len.

CALL FUNCTION 'CONVERT_OTF'

EXPORTING

format = 'PDF'

max_linewidth = 132

IMPORTING

bin_filesize = gv_binfilesize

TABLES

otf = gt_otf

lines = gt_tline

EXCEPTIONS

err_max_linewidth = 1

err_format = 2

err_conv_not_possible = 3

err_bad_otf = 4.

IF sy-subrc <> 0.

MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

ENDIF.

LOOP AT gt_tline.

TRANSLATE gt_tline USING ' ~'.

CONCATENATE gs_buffer gt_tline INTO gs_buffer.

ENDLOOP.

TRANSLATE gs_buffer USING '~ '.

DO.

gt_record = gs_buffer.

APPEND gt_record.

SHIFT gs_buffer LEFT BY 255 PLACES.

IF gs_buffer IS INITIAL.

EXIT.

ENDIF.

ENDDO.

IF vbdka-vtweg = '01'.

CONCATENATE 'Please find your Roper Rhodes Order Confirmation for'

'your reference (see attached)'

INTO gt_objtxt SEPARATED BY space.

APPEND gt_objtxt.

lv_subject = 'Roper Rhodes Order Confirmation'.

ELSE.

CONCATENATE 'Please find your Tavistock Order Confirmation for'

'your reference (see attached)'

INTO gt_objtxt SEPARATED BY space.

APPEND gt_objtxt.

lv_subject = 'Tavistock Order Confirmation'.

ENDIF.

SELECT SINGLE smtp_addr

INTO lv_email

FROM adr6 AS a INNER JOIN kna1 AS b

ON badrnr = aaddrnumber

WHERE kunnr EQ vbdka-kunnr.

TRY.

  • ---------- create persistent send request ----------------------

send_request = cl_bcs=>create_persistent( ).

  • ---------- add document ----------------------------------------

document = cl_document_bcs=>create_document(

i_type = 'RAW'

i_text = gt_objtxt[]

i_subject = lv_subject ).

CALL METHOD document->add_attachment

EXPORTING

i_attachment_type = 'PDF'

i_attachment_subject = 'Order.pdf'

i_att_content_text = gt_record[].

  • add document to send request

send_request->set_document( document ).

  • ---------- add recipient (e-mail address) ----------------------

recipient = cl_cam_address_bcs=>create_internet_address(

i_address_string = lv_email ).

  • add recipient to send request

send_request->add_recipient( i_recipient = recipient ).

  • ---------- send document ---------------------------------------

sent_to_all = send_request->send(

i_with_error_screen = 'X' ).

IF sent_to_all = 'X'.

  • MESSAGE i022(so).

PERFORM update_control_table.

ENDIF.

  • ---------- explicit 'commit work' is mandatory! ----------------

COMMIT WORK.

  • ------------------------------------------------------------------

  • * exception handling

  • ------------------------------------------------------------------

  • * replace this very rudimentary exception handling

  • * with your own one !!!

  • ------------------------------------------------------------------

CATCH cx_bcs INTO bcs_exception.

WRITE: text-001.

WRITE: text-002, bcs_exception->error_type.

EXIT.

ENDTRY.

SuhaSaha
Advisor
Advisor
0 Kudos

Hello,

You've done additional coding in converting the PDF data to text format, this was not necesary

Former Member
0 Kudos

Hi,

Check with below Code,

{{color:red}Moderator message - Please respect the 5,000 character maximum when posting. Post only the relevant portions of code

Edited by: Rob Burbank on May 11, 2011 9:10 AM

SuhaSaha
Advisor
Advisor
0 Kudos

BCS classes are much easier to handle than the messy SO* function modules!

Search the forums for more details on the BCS classes.