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

Problem sending attachment as PDF using CL_BCS

Former Member
0 Likes
4,017

Hi experts,

We are sending two PDF attachments via mail from our print program. One attachment is created from spool and opens correctly in the mail. Other attachment is created after reading from the application server. This attachment is not opening correctly. When I view the mail attachment in SOST queue, the error message says "file is damaged and cannot be repaired."

Please find the below code and advice what is the mistake:

OPEN DATASET l_dsn FOR INPUT IN BINARY MODE.

DATA : wa_bin type SOLIX.

     FIELD-SYMBOLS <hex_container> TYPE x.

     DO.

       ASSIGN wa_bin TO <hex_container> CASTING.

       READ DATASET l_dsn INTO <hex_container>.

       IF sy-subrc = 0.

        APPEND wa_bin TO i_instructions.

       ELSE.

         EXIT.

       ENDIF.

     ENDDO.

CLOSE DATASET l_dsn.

** For adding this as attachment **

DESCRIBE TABLE i_instructions LINES l_count.

READ TABLE i_instructions INTO wa_solix INDEX l_count.

l_doc_size = ( l_count - 1 ) * 255

               + XSTRLEN( wa_solix-line ).

   TRY.

       lo_document->add_attachment(

         EXPORTING

           i_attachment_type = 'PDF'

           i_attachment_subject = 'PO Instructions'(t01)

           i_attachment_size  = l_doc_size

           i_att_content_hex = i_instructions ).



Please advice.

1 ACCEPTED SOLUTION
Read only

soumik_de2
Participant
0 Likes
1,955

Hi Prasena........

I Have faced the same problem before and unable to get rply from sdn......

at last i found solution by myself only.........

please wirte the following code maybe it will solve ur issue.......

DATA : wa_bin type SOLIX.

OPEN DATASET l_dsn FOR INPUT IN BINARY MODE.

if sy-subrc eq 0.

do.

***THIS PART IS INPORTANT

if sy-subrc ne 0.

exit.

endif.

Then ur code..............

enddo.

endif.


Hi experts,

We are sending two PDF attachments via mail from our print program. One attachment is created from spool and opens correctly in the mail. Other attachment is created after reading from the application server. This attachment is not opening correctly. When I view the mail attachment in SOST queue, the error message says "file is damaged and cannot be repaired."

Please find the below code and advice what is the mistake:

OPEN DATASET l_dsn FOR INPUT IN BINARY MODE.

DATA : wa_bin type SOLIX.

     FIELD-SYMBOLS <hex_container> TYPE x.

     DO.

       ASSIGN wa_bin TO <hex_container> CASTING.

       READ DATASET l_dsn INTO <hex_container>.

       IF sy-subrc = 0.

        APPEND wa_bin TO i_instructions.

       ELSE.

         EXIT.

       ENDIF.

     ENDDO.

CLOSE DATASET l_dsn.

** For adding this as attachment **

DESCRIBE TABLE i_instructions LINES l_count.

READ TABLE i_instructions INTO wa_solix INDEX l_count.

l_doc_size = ( l_count - 1 ) * 255

               + XSTRLEN( wa_solix-line ).

   TRY.

       lo_document->add_attachment(

         EXPORTING

           i_attachment_type = 'PDF'

           i_attachment_subject = 'PO Instructions'(t01)

           i_attachment_size  = l_doc_size

           i_att_content_hex = i_instructions ).



Please advice.

7 REPLIES 7
Read only

former_member392866
Active Participant
0 Likes
1,955

Hi Praseena,

Please check the program BCS_EXAMPLE_6.

(or)  check below link

http://scn.sap.com/thread/1832037

Regards,

Balaji.

Read only

soumik_de2
Participant
0 Likes
1,956

Hi Prasena........

I Have faced the same problem before and unable to get rply from sdn......

at last i found solution by myself only.........

please wirte the following code maybe it will solve ur issue.......

DATA : wa_bin type SOLIX.

OPEN DATASET l_dsn FOR INPUT IN BINARY MODE.

if sy-subrc eq 0.

do.

***THIS PART IS INPORTANT

if sy-subrc ne 0.

exit.

endif.

Then ur code..............

enddo.

endif.


Read only

SuhaSaha
Product and Topic Expert
Product and Topic Expert
0 Likes
1,955

Hello Praseena,

When I view the mail attachment in SOST queue, the error message says "file is damaged and cannot be repaired."

I think you are getting the error message because the size of the binary content was calculated incorrectly

I don't think you have to write such a complicated code to convert the AS file to the binary content I can do this using the following code as well :

TRY .
     OPEN DATASET gv_as_filepath
     IN BINARY MODE
     FOR INPUT
     MESSAGE gv_os_msg.

     READ DATASET gv_as_filepath INTO gv_bin_data ACTUAL LENGTH gv_file_size.

   CATCH cx_sy_file_open
         cx_sy_codepage_converter_init
         cx_sy_conversion_codepage
         cx_sy_file_authority
         cx_sy_pipes_not_supported
         cx_sy_too_many_files
         cx_sy_file_io
         cx_sy_file_open_mode
         cx_sy_pipe_reopen ##no_handler.

ENDTRY.

* Convert the binary string to the SOLIX table format
gt_pdf_content = cl_document_bcs=>xstring_to_solix( gv_bin_data ).

You can use the gt_pdf_content & gv_file_size to build your attachment.

BR,

Suhas

PS - When the file is opened in Binary mode, the READ DATASET reads the whole file in a single go. This was new to me as well

Read only

0 Likes
1,955

Thanks a lot Suhas. This has solved my Problem.

Read only

0 Likes
1,955

Thx for ur solution.

It was a big headach for me until I saw your solution.

Read only

rosenberg_eitan
Active Contributor
0 Likes
1,955

Hi,

See form "FORM mail_1_prep_5" in this program:

http://scn.sap.com/servlet/JiveServlet/download/2015-3412951-14309154-180712/y_r_eitan_test_10_02.tx...

You might have to change the OPEN DATASET.

*----------------------------------------------------------------------*
FORM mail_1_prep_5
  CHANGING
    ob_document_bcs TYPE REF TO cl_document_bcs .

  DATA: bin_file TYPE string .

* For testing purpose a test file is created.
  IF p_filf EQ abap_true .

    OPEN DATASET p_file FOR OUTPUT IN BINARY MODE .

    DO 1000 TIMES .
      TRANSFER 'This is a test +++++++++++++++++++++++++++++++++++++++' TO p_file .
      TRANSFER cl_abap_char_utilities=>cr_lf TO p_file .
    ENDDO .

    CLOSE DATASET p_file .

  ENDIF .

  DATA mess TYPE string .

  OPEN DATASET p_file FOR INPUT IN LEGACY BINARY MODE CODE PAGE '1800' MESSAGE mess.

  break r_eitan .

  CHECK sy-subrc EQ 0 .

  READ DATASET p_file INTO bin_file .

  CHECK sy-subrc EQ 0 .

  DATA: it_solix TYPE solix_tab .

  CALL METHOD cl_bcs_convert=>string_to_solix
    EXPORTING
      iv_string = bin_file
    IMPORTING
      et_solix  = it_solix.

  DATA: long_filename  TYPE string .
  DATA: pure_filename  TYPE string .
  DATA: pure_extension TYPE string .

  long_filename = p_file .

  PERFORM split_filename
    USING
       long_filename
    CHANGING
       pure_filename
       pure_extension.

  DATA: attachment_subject TYPE so_obj_des .

  CONCATENATE pure_filename space INTO attachment_subject .

  DATA: attachment_type TYPE so_obj_tp .

  attachment_type = pure_extension .

  TRY.
      CALL METHOD ob_document_bcs->add_attachment
        EXPORTING
          i_attachment_type    = attachment_type
          i_attachment_subject = attachment_subject
          i_att_content_hex    = it_solix.
    CATCH cx_document_bcs .
  ENDTRY.

ENDFORM .                    "mail_1_prep_5
*----------------------------------------------------------------------*




Regards.





Read only

0 Likes
1,955

Hi,

This error is because of file content length was calculated wrongly as Suhas said.

Use this FM to convert binary to xstring

SCMS_BINARY_TO_XSTRING

convert the content to hex format from binary format and pass that value as highlighted below

      TRY.

          CALL METHOD v_document->add_attachment

            EXPORTING

              i_attachment_type     = 'XLS'

              i_attachment_subject  = ''

              i_attachment_size     = lv_doc_size

              i_attachment_language = 'E'

              i_att_content_hex     = it_hex1

              i_attachment_header   = it_headd.

        CATCH cx_bcs INTO bcs_exception.

          l_message = bcs_exception->get_text( ).

      ENDTRY.