Skip to Content
0
Former Member
Aug 15, 2005 at 01:01 PM

email PDF

21 Views

Hi,

I'm trying to email an PDF file to an internet address.

Upon opening the PDF document I get the following error

"There was an error opening this document. The file is damaged and could not be repaired."

When downloading the same document to my desktop with GUI_DOWNLOAD it looks fine.

This is my sample code (more or less SAP's BCS_EXAMPLE_5).

Regards,

Vibeke


REPORT ztest.

* This example shows how to send
*   - a simple text provided in an internal table of text lines
*   - and an attached MS word document provided in internal table
*   - to some internet email address.
*
* All activities done via facade CL_BCS!

DATA: send_request       TYPE REF TO cl_bcs.
DATA: text               TYPE bcsy_text.
DATA: binary_content     TYPE solix_tab.
DATA: document           TYPE REF TO cl_document_bcs.
DATA: sender             TYPE REF TO cl_sapuser_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: l_spool LIKE tsp01-rqident VALUE 6509.
DATA: it_pdf LIKE tline OCCURS 0 WITH HEADER LINE.

DATA: w_buffer TYPE string.

DATA: BEGIN OF it_buffer OCCURS 0.
DATA: line(255) TYPE c.
DATA: END OF it_buffer.

DATA: xx_pdf TYPE soli_tab.

START-OF-SELECTION.

  CALL FUNCTION 'CONVERT_OTFSPOOLJOB_2_PDF'
    EXPORTING
      src_spoolid              = l_spool
    TABLES
      pdf                      = it_pdf
    EXCEPTIONS
      err_no_otf_spooljob      = 1
      err_no_spooljob          = 2
      err_no_permission        = 3
      err_conv_not_possible    = 4
      err_bad_dstdevice        = 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.

  CALL FUNCTION 'GUI_DOWNLOAD'
    EXPORTING
      filename = 'C:ProfilerlpwDesktopfile.pdf'
      filetype = 'BIN'
    TABLES
      data_tab = it_pdf.

  LOOP AT it_pdf.
    TRANSLATE it_pdf USING '~'.
    CONCATENATE w_buffer it_pdf INTO w_buffer.
  ENDLOOP.

  TRANSLATE w_buffer USING '~'.

  DO.
    it_buffer = w_buffer.
    APPEND it_buffer.
    SHIFT w_buffer LEFT BY 255 PLACES .
    IF w_buffer IS INITIAL.
      EXIT.
    ENDIF.
  ENDDO.

  xx_pdf[] = it_buffer[].
  binary_content[] = it_buffer[].

  PERFORM main.


*---------------------------------------------------------------------*
*       FORM main                                                     *
*---------------------------------------------------------------------*
FORM main.

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

*     -------- create and set document with attachment ---------------
*     create document from internal table with text
      APPEND 'Hello world!' TO text.
      document = cl_document_bcs=>create_document(
                      i_type    = 'RAW'
                      i_text    = text
                      i_length  = '12'
                      i_subject = 'test created by Vibeke' ).

*     add attachment to document
*     BCS expects document content here e.g. from document upload
*     binary_content = ...
      CALL METHOD document->add_attachment
        EXPORTING
          i_attachment_type    = 'PDF'
          i_attachment_subject = 'My PDF attachment'
          i_att_content_text   = xx_pdf.
*          i_att_content_hex    = binary_content.

*     add document to send request
      CALL METHOD send_request->set_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( sy-uname ).
      CALL METHOD send_request->set_sender
        EXPORTING
          i_sender = sender.

*     --------- add recipient (e-mail address) -----------------------
*     create recipient - please replace e-mail address !!!
      recipient = cl_cam_address_bcs=>create_internet_address(
                              'xxxxx@xxxx.dk' ).

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

*     ---------- send document ---------------------------------------
      CALL METHOD send_request->send(
        EXPORTING
          i_with_error_screen = 'X'
        RECEIVING
          result              = sent_to_all ).
      IF sent_to_all = 'X'.
        WRITE text-003.
      ENDIF.

      COMMIT WORK.


* -----------------------------------------------------------
* *                     exception handling
* -----------------------------------------------------------
* * replace this very rudimentary exception handling
* * with your own one !!!
* -----------------------------------------------------------
    CATCH cx_bcs INTO bcs_exception.
      WRITE: 'Fehler aufgetreten.'(001).
      WRITE: 'Fehlertyp:'(002), bcs_exception->error_type.
      EXIT.

  ENDTRY.

ENDFORM.                    "main

Message was edited by: Vibeke Skov Baird