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

email PDF

Former Member
0 Likes
425

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(
                              '[email protected]' ).

*     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

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(
                              '[email protected]' ).

*     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

1 REPLY 1
Read only

Former Member
0 Likes
375

Vibeke,

Hi. The way you are creating the xx_pdf itab is flawed.

The below code works

Cheers

Rob

refresh tab_pdf.

loop at lt_pdf into lw_pdf_lines.

move lw_pdf_lines-tdformat to lw_charline.

move lw_pdf_lines-tdline(132) to lw_charline+2(132).

append lw_charline to tab_pdf.

endloop.

perform tab_to_255 tables tab_pdf

tab_soli.

call method document->add_attachment

exporting

i_attachment_type = 'PDF'

i_attachment_subject = l_subject

" i_attachment_size = l_att_size

i_att_content_text = tab_soli[].

form tab_to_255 tables tab_pdf

tab_255.

data: l_pdf type gtypes_pdf,

l_attachment_long type string,

l_attachment type soli.

check not ( tab_pdf[] is initial ).

  • Convert pdf itab to 255 line itab.

data :lv_counter type i.

data :lv_from type i.

loop at tab_pdf into l_pdf.

translate l_pdf using ' ~' .

concatenate l_attachment_long l_pdf into l_attachment_long.

endloop.

translate l_attachment_long using '~ ' .

clear : lv_counter.

do.

lv_counter = strlen( l_attachment_long ).

if lv_counter ge 255.

l_attachment = l_attachment_long(255).

append l_attachment to tab_255.

shift l_attachment_long by 255 places.

else.

l_attachment = l_attachment_long(lv_counter).

append l_attachment to tab_255.

exit.

endif.

enddo.

endform. "tab_to_255