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: 

Error when sending pdf by mail in abap

0 Kudos

Good day

I'm starting with abap, I have a problem opening a PDF file received by mail.

I'm reading a pdf file from the server to send it by mail, to read it I'm using DATASET.

This is the error that I get when trying to open the PDF file “Error opening the document. The file is damaged and cannot be recovered”

This is the code used:

DATA: st_pdf TYPE solix,
ti_pdf TYPE TABLE OF solix,
lv_filename TYPE c LENGTH 50.
lv_filenameConst = '/usr/sap/tmp/filepdf.pdf'.
TRY .
OPEN DATASET lv_filename FOR INPUT IN BINARY MODE.
DO.
READ DATASET lv_filename INTO st_pdf.
IF sy-subrc NE 0.
EXIT.
ENDIF.
APPEND st_pdf to ti_pdf.
ENDDO.
CLOSE DATASET lv_filename.
CATCH cx_root INTO DATA(e_txt).
CLOSE DATASET lv_filename.
ENDTRY.

Can anyone see if I'm doing something wrong with the use of the DATASET or if there is some abap function that allows me to read the file from the server?

Thanks in advance

5 REPLIES 5

Sandra_Rossi
Active Contributor

Please edit your question (Actions > Edit), select your code and press the button [CODE], which makes the code appear colored/indented, it will be easier for people to look at it. Thank you!

raymond_giuseppi
Active Contributor

With BINARY MODE, remove the DO/ENDDO wrapping of the read dataset (check sample of online help)

But I don't think your code is related to your problem, it would be better to post the code that attaches the document to the email (wrong data length is a frequent error, so look for this READ DATASET option: [ACTUAL] LENGTH alen and use alen when attaching the mail.

OPEN DATASET dset FOR INPUT in BINARY MODE.
READ DATASET dset INTO xstr ACTUAL LENGTH FINAL(bytes).
CLOSE DATASET dset.

Also, hHow was the pdf generated

Thank you very much for your support

Your answer worked for me, apparently the way I was reading the PDF didn't get all the file information.

This is how the DATASET was implemented:

DATA: ti_pdf TYPE TABLE OF solix,
      lv_content TYPE xstring,
      lv_filename TYPE c LENGTH 50.
lv_filenameConst = '/usr/sap/tmp/filepdf.pdf'.

OPEN DATASET lv_filename FOR INPUT IN BINARY MODE.
READ DATASET lv_filename INTO lv_content.
IF sy-subrc NE 0.
EXIT.
ENDIF.
CLOSE DATASET lv_filename.

CALL FUNCTION 'SCMS_XSTRING_TO_BINARY' EXPORTING buffer = lv_content IMPORTING output_length = bytes TABLES binary_tab = ti_pdf.


I hope it will be you useful

Sandra_Rossi
Active Contributor

You forgot to mention:

DATA(bytes) = xstrlen( lv_content ).

Sandra_Rossi
Active Contributor

You can post it as an answer (Actions > Convert to answer)