Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Convert SMARTFORM report to PDF and send as an email attachment

Hi

I am using the CONVERT_OTF function module to convert a SMARTFORM report in the "spool" to a PDF file. When I use the DOWNLOAD function module, the resulting file can be opened in acrobat.

I would like to email the report in PDF format as an attachment. I am using the function module SO_NEW_DOCUMENT_ATT_SEND_API1 to create an attachment however the attached file is not being created correctly and When try to open, it gives 'File is damaged and could not be repaired' error message. I believe that somehow I am not using the SO_NEW_DOCUMENT_ATT_SEND_API1 function module correctly.

Please help me to correct this

Thanks

Rohitha

see my code below,

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 132 PLACES.

IF WA_BUFFER IS INITIAL.

EXIT.

ENDIF.

ENDDO.

REFRESH: I_RECLIST,

I_OBJTXT,

I_OBJBIN,

I_OBJPACK.

CLEAR WA_OBJHEAD.

I_OBJBIN[] = I_RECORD[].

I_OBJTXT = '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_to_PDF'.

WA_DOC_CHNG-EXPIRY_DAT = SY-DATUM + 10.

WA_DOC_CHNG-OBJ_DESCR = 'Smartform to PDF'.

WA_DOC_CHNG-SENSITIVTY = 'F'.

WA_DOC_CHNG-DOC_SIZE = STRLEN( I_OBJTXT ) + ( ( V_LINES_TXT - 1 ) * 255 ) .

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.

I_OBJPACK-TRANSF_BIN = 'X'.

I_OBJPACK-HEAD_START = 1.

I_OBJPACK-HEAD_NUM = 1.

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 = 'ATTACHMENT'.

I_OBJPACK-OBJ_DESCR = 'test'.

APPEND I_OBJPACK.

CLEAR I_RECLIST.

I_RECLIST-RECEIVER = 'my email address u2019.

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 ', SY-SUBRC.

ELSE.

WRITE:/ 'Mail sent'.

ENDIF.

Former Member
Former Member replied

Hi rohitha.wijewardena ,

Please find the below code which i followed and succeed ,

CALL FUNCTION 'WFMC_PREPARE_SMART_FORM'
    EXPORTING
      pi_nast       = nast
      pi_country    = lv_dlv-land
      pi_addr_key   = lv_addr_key
      pi_repid      = sy-repid
      pi_screen     = lc_x
    IMPORTING
      pe_returncode = gv_retcode
      pe_itcpo      = lv_itcpo
      pe_device     = lv_device
      pe_recipient  = lv_recipient
      pe_sender     = lv_sender.
  IF gv_retcode = 0.
*moving the data to composer and control parameters for output
    MOVE-CORRESPONDING lv_itcpo TO lv_composer_param.
    lv_control_param-device      = lv_device.
    lv_control_param-no_dialog   = lc_x.
    lv_control_param-preview     = lc_x.
    lv_control_param-getotf      = lv_itcpo-tdgetotf.
    lv_control_param-langu       = nast-spras.
    lv_composer_param-tdnoprint = space.
  ENDIF.

*Getting the Smartform Function module using Standard Function module
  CALL FUNCTION 'SSF_FUNCTION_MODULE_NAME'
    EXPORTING
      formname                 = lv_formname           "Smartform Name
   IMPORTING
     fm_name                  = lv_fm_name
   EXCEPTIONS
     no_form                  = 1
     no_function_module       = 2
     OTHERS                   = 3
            .
  IF sy-subrc <> 0.
    gv_retcode = sy-subrc.
    PERFORM protocol_update.
    PERFORM add_smfrm_prot.
  ENDIF.

**Smartform function module to get the output
  CALL FUNCTION lv_fm_name
    EXPORTING
   control_parameters         = lv_control_param
   mail_recipient             = lv_recipient
   mail_sender                = lv_sender
     output_options             = lv_composer_param
     user_settings              = ' '
      is_bil_invoice             = lv_bil_invoice
      gt_header                  = gt_header
   IMPORTING
     job_output_info            = gv_job_output
    TABLES
      gt_item                    = gt_item
   EXCEPTIONS
     formatting_error           = 1
     internal_error             = 2
     send_error                 = 3
     user_canceled              = 4
     OTHERS                     = 5
            .
  IF sy-subrc <> 0.
    gv_retcode = sy-subrc.
    PERFORM protocol_update.
    PERFORM add_smfrm_prot.
  ENDIF.

  IF nast-nacha = lc_mail.
    gt_otfdata[] = gv_job_output-otfdata[].

******************************************************************
*           Converting Smartform to PDF                          *
******************************************************************
    CALL FUNCTION 'CONVERT_OTF'
      EXPORTING
        format                = lc_pdf
        max_linewidth         = 10
      IMPORTING
        bin_filesize          = gv_binfilesize
        bin_file              = gv_pdf_xstring
      TABLES
        otf                   = gt_otfdata[]
        lines                 = gt_pdftab[]
      EXCEPTIONS
        err_max_linewidth     = 1
        err_format            = 2
        err_conv_not_possible = 3
        err_bad_otf           = 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.

*******************************************************************
*           Sending PDF to Email  using Class                     *
*******************************************************************
    IF NOT lv_email IS INITIAL .
      TRY.
         gv_send_request = cl_bcs=>create_persistent( ).
          gv_pdf_content = cl_document_bcs=>xstring_to_solix( gv_pdf_xstring ).

          gv_document = cl_document_bcs=>create_document(
                i_type    = lc_pdf
                i_hex     = gv_pdf_content
                i_length  = gv_pdf_size
                i_subject = lc_subject ).            "Subject for Email
          gv_send_request->set_document( gv_document ).
         gv_recipient = cl_cam_address_bcs=>create_internet_address( lv_email ).
          gv_send_request->add_recipient( gv_recipient ).
          gv_sent_to_all = gv_send_request->send( i_with_error_screen = lc_x ).
          COMMIT WORK.
          IF gv_sent_to_all IS INITIAL.
            MESSAGE i500(sbcoms) WITH lv_email.
          ELSE.
            MESSAGE s022(so).
          ENDIF.

        CATCH cx_bcs INTO gv_bcs_exception.
          MESSAGE i865(so) WITH gv_bcs_exception->error_type.
      ENDTRY.
    ENDIF.

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question