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

Error in PDF attachment send by SO_DOCUMENT_SEND_API1

Former Member
0 Likes
1,444

Hello,

I need to send an email with PDF attachment. The PDF file is stored on SAP server.

I use READ DATASET FOR INPUT IN BINARY MODE in order to get PDF content.

I fill a table T_DATA with content (T_DATA : STANDARD TABLE OF XSTRING) => T_DATA contains only 1 line.

Then I convert XSTRING in BINARY with function SCMS_XSTRING_TO_BINARY => fill T_DATA_CHAR (type table of solisti1)

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
            EXPORTING
              buffer                = s_data        => first LINE of T_DATA
           IMPORTING
             output_length         = w_output_length
            TABLES
              binary_tab            =  t_data_char.

I fill tables for 'SO_DOCUMENT_SEND_API1'.

APPEND LINES OF t_data_char TO t_contents_bin.
            s_packing_list-transf_bin = 'X'.
            s_packing_list-head_start = 1.
            s_packing_list-head_num   = w_cpt.
            s_packing_list-body_start = 1.
            DESCRIBE TABLE t_data_char  LINES s_packing_list-body_num.
            s_packing_list-doc_type   = 'PDF'.
...
            s_packing_list-doc_size  = w_output_length.
            APPEND s_packing_list TO t_packing_list.
....

The mail is well created but i can't open the PDF attachment because I have the message "There was error in opening. File damaged & could not be repaired".

What can I do ?

Thanks.

5 REPLIES 5
Read only

jordi_escodaruiz
Active Participant
0 Likes
960

Hi:

The code you pasted in the question seems correct.

¿Can you paste complete code?, may be the problem is located in some other lines

Regards.

Jordi

Read only

0 Likes
960

Complete code :

*       Title
        CLEAR w_document_data.
        CONCATENATE text-001 i_belnr
        INTO w_document_data-obj_descr
        SEPARATED BY space.
        w_document_data-sensitivty = 'F'.
        w_document_data-obj_langu = sy-langu.
        w_document_data-no_change = 'X' .
        w_document_data-expiry_dat = sy-datum + 30.

*       Mail content
        CLEAR s_packing_list.
        s_packing_list-transf_bin = space.
        s_packing_list-head_start = 1.
        s_packing_list-head_num = 0.
        s_packing_list-body_start = 1.
        DESCRIBE TABLE  t_corps_mail LINES s_packing_list-body_num.
        s_packing_list-doc_type = 'RAW'.
        APPEND s_packing_list TO    t_packing_list


OPEN DATASET w_path
                FOR INPUT
                IN BINARY MODE
                MESSAGE msg.

          IF sy-subrc = 0.
    DO.
              CLEAR s_data.
              READ DATASET w_path INTO s_data.
              IF sy-subrc <> 0.
                EXIT.
              ENDIF.
              APPEND s_data TO t_data.
            ENDDO.
          ENDIF.
          CLOSE DATASET w_path.
        READ TABLE t_data INTO s_data INDEX 1.

        DATA : w_output_length TYPE i.
          CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
            EXPORTING
              buffer                = s_data
           IMPORTING
             output_length         = w_output_length
            TABLES
              binary_tab            =  t_data_char

          IF sy-subrc = 0.

            APPEND LINES OF t_data_char TO t_contents_bin.

            s_packing_list-transf_bin = 'X'.
            s_packing_list-head_start = 1.
            s_packing_list-head_num   = 1.
            s_packing_list-body_start = 1.

            DESCRIBE TABLE t_data_char
            LINES s_packing_list-body_num.
            s_packing_list-doc_type   =  k_type_pdf.
            s_packing_list-obj_name = 'NAME'.
           s_packing_list-obj_descr = 'DESC'.
           s_packing_list-doc_size  = w_output_length.
          APPEND s_packing_list TO t_packing_list.

        CLEAR s_receivers.
        s_receivers-receiver = w_receiver.
        s_receivers-rec_type = 'U'.
        APPEND s_receivers TO t_receivers.

        CALL FUNCTION 'SO_DOCUMENT_SEND_API1' 
        EXPORTING
          document_data              = w_document_data
          sender_address            =  w_sender  
          put_in_outbox              = 'X'
          commit_work                = 'X'
        TABLES
          packing_list               = t_packing_list
          contents_bin               = t_contents_bin     
          contents_txt               = t_corps_mail
          receivers                  = t_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.
endif.

Edited by: Laure VERE on Nov 3, 2009 11:22 AM

Edited by: Laure VERE on Nov 3, 2009 11:22 AM

Read only

0 Likes
960

Buff

The code is unreadable in the forum...

Fortunately I can see it in the mail I received.

I don't see anything wrong. It's strange.

I assume that the constant k_type_pdf has the value 'PDF'

Sorry but I don't have more ideas.

Regards

Jordi

Read only

0 Likes
960

although your code seems to be correct but still i will suggest you to go by the OOPs method. below is the code which i wrote and its working perfectly fine:


* Convert PDF Object to type SOLIX
  CALL METHOD cl_document_bcs=>xstring_to_solix
    EXPORTING
      ip_xstring = wa_pdfobject-pdf
    RECEIVING
      rt_solix   = it_attachment.

* Initialize Mail creation
  TRY.
      obj_send_request = cl_bcs=>create_persistent( ).

* Set sender Email address
      obj_sender =
      cl_cam_address_bcs=>create_internet_address( lc_id_from ).
      obj_send_request->set_sender( i_sender = obj_sender ).

* Get Recipient's Email IDs
     obj_recipient =
           cl_cam_address_bcs=>create_internet_address( lv_recv_id ).
      obj_send_request->add_recipient(
      i_recipient = obj_recipient
      i_express = c_x
      ).


      " create documents
      obj_document = cl_document_bcs=>create_document(
      i_type = lc_type_raw " RAW document format
      i_text = it_contents
      i_subject = lv_subject
      ).
      obj_document->add_attachment(
      i_attachment_type = lc_type_pdf " add PDF attachment
      i_attachment_subject = Subject'
      i_att_content_hex = it_attachment
      ).
      obj_send_request->set_document( obj_document ).
**********************************************************************
      " send email
      obj_send_request->set_send_immediately( c_x ).
      lv_ret = obj_send_request->send( ).                

ags.

Read only

0 Likes
960

Thanks.

I'll try to use the OOPs method.

Edited by: Laure VERE on Nov 3, 2009 1:45 PM

Edited by: Laure VERE on Nov 3, 2009 1:45 PM