Application Development 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: 

PDF attachment failed - File is damaged or could not be repaired.

Former Member
0 Kudos

Hi,

I have a requirement to send a PDF attachment as an email. I am getting the error "File is damaged or could not be repaired". I have used CL_BCS  for this purpose. Not sure what caused the issue. I tried other posts but unable to get the exact reason.

Below is my code for sending email attachment functionality. Appreciate help on this.

TYPES:      t_pdf            TYPE  tline.

TYPES:      t_attachment     TYPE solisti1.

DATA:   pdf_bytecount TYPE i.

DATA: pdf LIKE tline OCCURS 0 WITH HEADER LINE.

  DATA:  lo_send_request TYPE REF TO cl_bcs,

        lo_document     TYPE REF TO cl_document_bcs,

        lo_sender       TYPE REF TO if_sender_bcs,

        lo_recipient    TYPE REF TO if_recipient_bcs,      ,

        lt_message_body TYPE bcsy_text,

        lx_document_bcs TYPE REF TO cx_document_bcs,

        lv_send         TYPE ad_smtpadr,

        lv_sent_to_all  TYPE os_boolean,

        lt_att_content_hex TYPE solix_tab.

  TYPES: BEGIN OF ty_file,

        records(1023) TYPE c,

         END OF ty_file.

  DATA: lit_file TYPE STANDARD TABLE OF ty_file,

        lwa_file TYPE ty_file,

         lv_receiver       TYPE so_recname.

  DATA: it_attachment TYPE STANDARD TABLE OF solisti1 ,

        wa_attachment   TYPE solisti1,

       it_contents_hex LIKE solix OCCURS 0 WITH HEADER LINE,

       it_packing_list TYPE STANDARD TABLE OF sopcklsti1,

       wa_packing_list   TYPE sopcklsti1,

       it_body_msg TYPE STANDARD TABLE OF solisti1,

       wa_body_msg       TYPE solisti1,

       lwa_attach   TYPE solisti1,

       lv_sender         TYPE soextreci1-receiver,

       cl_conv TYPE REF TO cl_abap_conv_out_ce,

       lv_buffer    TYPE xstring,

       t_mailhex TYPE solix_tab WITH HEADER LINE,

       lv_format TYPE so_obj_tp,

       lv_lines TYPE sy-tabix,

       lv_hexa(510) TYPE x,

       wa_document_data  TYPE sodocchgi1,

       it_receivers TYPE STANDARD TABLE OF somlreci1,

       wa_receivers      TYPE somlreci1,

       tab_lines TYPE sy-tabix.

  DATA: i_spool TYPE rspoid,

        numbytes TYPE i,

        file_length TYPE int4.

  DATA :

  w_arcpar      TYPE arc_params,

  w_pripar TYPE pri_params,

  g_val         TYPE c,

       l_pdf_spoolid LIKE tsp01-rqident.

  DATA: lt_pdf           TYPE soli_tab,

        ls_pdf           TYPE soli_tab,

        lt_pdfbin        TYPE solix_tab.

  DATA: ls_soli TYPE soli,

        ls_solix TYPE solix.

  CALL FUNCTION 'GET_PRINT_PARAMETERS'

    EXPORTING

      in_archive_parameters  = w_arcpar

      in_parameters          = w_pripar

      layout                 = 'X_65_132'

      line_count             = 65

      line_size              = 132

      no_dialog              = 'X'

    IMPORTING

      out_archive_parameters = w_arcpar

      out_parameters         = w_pripar

      valid                  = g_val.

  IF g_val  NE space AND sy-subrc = 0.

    w_pripar-prrel = space.

    w_pripar-primm = space.


*** Below code contains two internal tables Success & Failure which are written to the spool****

    NEW-PAGE PRINT ON  NEW-SECTION PARAMETERS w_pripar ARCHIVE PARAMETERS w_arcpar NO DIALOG.

  ENDIF.

  Write: 'Success Records:'  COLOR 5.

    write: sy-uline.

  LOOP AT gt_exl_success.

    WRITE: gt_exl_success-field2, 20 gt_exl_success-success.

  ENDLOOP.

  Write: 'Failed Records:'  COLOR 7.

  write: sy-uline.

  LOOP AT gt_exl_error.

    WRITE: gt_exl_error-field2, 20 gt_exl_error-success.

  ENDLOOP.

  NEW-PAGE PRINT OFF.

  CALL FUNCTION 'ABAP4_COMMIT_WORK'.

  i_spool = sy-spono.

  CALL FUNCTION 'CONVERT_ABAPSPOOLJOB_2_PDF'

         EXPORTING

           src_spoolid   = i_spool

           no_dialog     = ' '

         IMPORTING

           pdf_bytecount = numbytes

           pdf_spoolid   = l_pdf_spoolid

         TABLES

           pdf           = pdf.

  CALL FUNCTION 'SX_TABLE_LINE_WIDTH_CHANGE'

    EXPORTING

      line_width_src              = 132

      line_width_dst              = 255

      transfer_bin                = 'X'

    TABLES

      content_in                  = pdf

      content_out                 = lt_pdf

    EXCEPTIONS

err_line_width_src_too_long = 1

err_line_width_dst_too_long = 2

      err_conv_failed             = 3

      OTHERS                      = 4.

  CALL FUNCTION 'SO_SOLITAB_TO_SOLIXTAB'

    EXPORTING

      ip_solitab  = lt_pdf

    IMPORTING

      ep_solixtab = lt_pdfbin.

  "create send request

  lo_send_request = cl_bcs=>create_persistent( ).

  "create message body and subject

  APPEND 'Hi,' TO lt_message_body.

  APPEND INITIAL LINE TO lt_message_body.

  APPEND 'Please find the attached report .' TO lt_message_body.

  APPEND INITIAL LINE TO lt_message_body.

  APPEND 'Thank You,' TO lt_message_body.

  Append sy-uname TO lt_message_body.

  "put your text into the document

  lo_document = cl_document_bcs=>create_document(

                   i_type = 'RAW'

                   i_text = lt_message_body

                   i_subject = 'Treasury Report' ).

Concatenate sy-uname '@XXX.com' into lv_send.

  TRY.

      CALL METHOD lo_document->add_attachment

        EXPORTING

i_attachment_type    = 'PDF'

i_attachment_subject = 'Treasury Report'

i_att_content_hex    = lt_pdfbin.

    CATCH cx_document_bcs INTO lx_document_bcs.

  ENDTRY.

* Add attachment

* Pass the document to send request

lo_send_request->set_document( lo_document ).

  "Create sender

  lo_sender = cl_cam_address_bcs=>create_internet_address( lv_send ).

  "Set sender

lo_send_request->set_sender( lo_sender ).

  "Create recipient

  lo_recipient = cl_sapuser_bcs=>create( sy-uname ).

*Set recipient

lo_send_request->add_recipient(

       EXPORTING

         i_recipient = lo_recipient i_express = 'X' ).

  lo_send_request->add_recipient( lo_recipient ).

* Send email

  lo_send_request->send(

    EXPORTING

      i_with_error_screen = 'X'

    RECEIVING

      result = lv_sent_to_all ).

  COMMIT WORK.

Thanks,

Pavan

3 REPLIES 3

Private_Member_7726
Active Contributor
0 Kudos

Hi,

try passing the I_ATTACHMENT_SIZE = numbytes to lo_document->add_attachment( ).

cheers

Jānis

0 Kudos

Also there is a chance that the PDF is large.

Try converting it to binary format before attaching.

convert the lines using SX_TABLE_LINE_WIDTH_CHANGE and move the result to a binary table (SOLIX_TAB based) before attaching

0 Kudos

that's what the code (the SO_SOLITAB_TO_SOLIXTAB call) does already, I believe...

cheers

Jānis