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: 

I can't open pdf file .

trabelsioussa
Explorer
0 Kudos
1,526

Hello Experts,

I sent email contains attachment file type pdf but when I click on this file I can't display it. It displays pop up: We cannot open this file An error has occurred.

.... some code

DATA : gr_table type ref to cl_salv_table.

try.
cl_salv_table=>factory(
importing
r_salv_table = gr_table
changing
t_table = IT_FIRST ).
catch cx_salv_msg.
endtry.
"gr_table->display( ).
DATA : gv_xstring type xstring,
gv_xlen type int4,
gt_binary_table type solix_tab,
gr_request type ref to cl_bcs,
gv_body_text type bcsy_text,
gv_subject type so_obj_des,
gr_recipient type ref to if_recipient_bcs,
gr_document type ref to cl_document_bcs,
gv_size type so_obj_len.
* gv_sender_email TYPE string,
* gv_sender_name TYPE string.




try.
gv_xstring = gr_table->to_xml( if_salv_bs_xml=>c_type_xlsx ).

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
BUFFER = gv_xstring
* APPEND_TO_TABLE = ' '
IMPORTING
OUTPUT_LENGTH = gv_xlen
TABLES
BINARY_TAB = gt_binary_table
.

gr_request = cl_bcs=>create_persistent( ).

append 'sample body text' to gv_body_text.
gv_subject = 'data19tes'.

gr_document = cl_document_bcs=>create_document(
i_type ='RAW'
i_text = gv_body_text
i_subject = gv_subject ).

gv_size = gv_xlen.

TRY.
CALL METHOD GR_DOCUMENT->ADD_ATTACHMENT
EXPORTING
I_ATTACHMENT_TYPE = 'PDF'
I_ATTACHMENT_SUBJECT = gv_subject && '.pdf'
i_attachment_size = gv_size
I_ATT_CONTENT_HEX = gt_binary_table[]

.
CATCH CX_DOCUMENT_BCS .
ENDTRY.

gr_request->set_document( gr_document ).
gr_recipient = cl_cam_address_bcs=>create_internet_address( 'recipentMail@gmail.com' ).
gr_request->add_recipient( gr_recipient ).
gr_request->send( ).
commit work.
MESSAGE 'E-mail sent' type 'S'.

catch cx_bcs.
MESSAGE 'E-mail sent' type 'A'.
endtry.

regards

8 REPLIES 8

xiswanto
Active Participant
0 Kudos
1,435

have you tried modify your code bit by bit, such as removing the '.pdf' word from the attachment subject, or recheck your attachment size is correct (or not passing the attachment size at all)?

based on the information, I can only assume that it might be either two of what I mentioned above, or there is problem with the binary value for the pdf file itself, which you can make sure by downloading it into local pdf file.

trabelsioussa
Explorer
0 Kudos
1,435

thanks xiswanto for reply.

yes i removed '.pdf' word from the attachment subject . and comment i_attachment_size = gv_size . but same error. but when send excel file it's work . like that :

gr_document->add_attachment(

i_attachment_type ='EXT'

i_attachment_subject = gv_subject && '.xlsx'

i_attachment_size = gv_size

i_att_content_hex = gt_binary_table ).

matt
Active Contributor
1,435

Please use tags carefully. This has nothing to do with:

  1. ABAP RESTful Application Programming Model
  2. ABAP Connectivity
  3. NW ABAP Internet Communication Framework

Also use the CODE button in the editor if your pasting code. (And use paste as text).

xiswanto
Active Participant
0 Kudos
1,435

How about changing the imported xml code from this

gv_xstring = gr_table->to_xml( if_salv_bs_xml=>c_type_xlsx ).

into this

gv_xstring = gr_table->to_xml( if_salv_bs_xml=>c_type_pdf ).

Ulrich_Schmidt
Product and Topic Expert
Product and Topic Expert
1,435

If the PDF is ok, when saved into a local file, then the root cause for the problem most probably is a mail relay server somewhere between the sender and the receiver, which cannot handle 8-bit data. (Yes, even in the year 2023 there are still mail relay servers, which can handle only 7-bit US-ASCII data...!!)

I'm not sure, how the ABAP classes CL_BCS & CL_DOCUMENT_BCS work or whether they support the following, but in order to solve the problem, you would need to do the following (talking in terms of a general SMTP Client):

  • Technically, a mail with attachment is a multipart MIME message. Each attachment is one body-part of the message and has a "Content-Transfer-Encoding" header. The default for this body-part header is
    Content-Transfer-Encoding=7bit
    And this will break in case of binary data, depending on the relay servers along the route...
  • One can override this header as
    Content-Transfer-Encoding=binary
    but in my experience, this doesn't help...
  • The only foolproof way is to Base64-encode the binary data (which converts it from 8-bit Octets to 7-bit ASCII data) and then set the encoding header to
    Content-Transfer-Encoding=base64

    Note: some SMTP Clients automatically convert a binary object to Base64, if the encoding header for this body-part is set to base64. But others don't, and then it is your responsibility before adding the content to the MIME message...

So what you need to do to fix this:

  1. Find out, whether the CL_DOCUMENT_BCS class has some method to set the value of the Content-Transfer-Encoding header. If not: well, bad luck...
  2. If yes, set it to base64, and then find out, whether CL_DOCUMENT_BCS does the Base64 conversion for you automatically or not.
  3. If yes, you are done, just add the content for the PDF file like you do now from gt_binary_table.
    If not, you need to find an ABAP class that performs a Base64 conversion, convert the contents from gt_binary_table to Base64 and use the result from that in the gr_document->add_attachment() call.

Any mail program like Microsoft Outlook etc. will automatically recognize the Content-Transfer-Encoding=base64 header, do the "reverse conversion" of the data (7-bit Base64 back to 8-bit binary) and display the PDF file correctly.

0 Kudos
1,435

thanks ulrich.schmidt for a lot of information. I will try it .

trabelsioussa
Explorer
0 Kudos
1,435

thanks @Siswanto Tan but same error

Sandra_Rossi
Active Contributor
1,435

trabelsioussa menu Actions > Edit to do what Matthew suggested.

NB: you MUST pass the RIGHT attachment length, no question about that. You have SAP demo programs which show the values for each parameter. I_ATTACHMENT_TYPE = 'EXT' is good ('PDF' is not valid here).