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 to application server using FTP

maria_merino
Active Participant
0 Kudos
3,000

Hi experts,

I have this scenario. I have a report that connects to a url via FTP, there we have some files (XML or pdf) corresponding to sales invoices.

For the PDF files (for XML it's now working fine), I get the file, parse the result and then I fill some custom tables. The user also needs the file to be upload to an application server (I can check it in AL11).

I'm having problems while uploading to application server. That's what I do., for example with XML files:

HTTP_SCRAMBLE

FTP_CONNECT

[here I do FTP_COMMAND set passive off, ascii, cd DIR, dir]

FTP_SERVER_TO_R3 -> I get the file in table g_t_xml_tab (g_t_xml_tab TYPE TABLE OF ty_xml INITIAL SIZE 0)

I convert the file using

data: g_xmldata TYPE xstring,

g_str TYPE string.

CONCATENATE LINES OF g_t_xml_tab
INTO g_str SEPARATED BY space.

CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
text = g_str
IMPORTING
buffer = g_xmldata
EXCEPTIONS
failed = 1
OTHERS = 2.

And then to move the file to the server:

data: fichero_out LIKE rlgrap-filename,

resto TYPE i,
n_veces TYPE i,
size TYPE i.

DATA: BEGIN OF tab_bin OCCURS 0.
INCLUDE STRUCTURE sdokcntbin.
DATA: END OF tab_bin.

CLEAR tab_bin.
REFRESH tab_bin.
CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'
EXPORTING
buffer = g_xmldata

IMPORTING
output_length = size

TABLES
binary_tab = tab_bin.
CLEAR carpeta.

SELECT SINGLE dirname INTO carpeta FROM user_dir
WHERE aliass = 'EFACTURASPROV'.
CONCATENATE
carpeta '\' zfe_fracab-cif zfe_fracab-num_fra '.pdf' INTO fichero_out.
OPEN DATASET fichero_out FOR OUTPUT IN BINARY MODE.

IF sy-subrc = 0.
CLEAR: n_veces, resto.
n_veces = size DIV 1022.
resto = size MOD 1022.
LOOP AT tab_bin.
IF sy-tabix <= n_veces.
TRANSFER tab_bin-line TO fichero_out.
ELSE.
EXIT.
ENDIF.
ENDLOOP.
IF resto > 0.
TRANSFER tab_bin-line(resto) TO fichero_out.
ENDIF.
CLOSE DATASET fichero_out.
ENDIF.

I can now see the file in AL11 but it is wrong created. Maybe I'm missing something?

Thanks in advance!

Maria

14 REPLIES 14

Sandra_Rossi
Active Contributor
1,012

You are overcomplexifying. Do everything in binary, because PDF is said to be a "binary" file:

  • FTP_COMMAND: don't ascii
  • FTP_SERVER_TO_R3: use binary (character mode = abap_false, and take BLOB parameter into a variable TABLE OF x255 (255 bytes) for instance)
  • concatenate lines of blob into xstring in byte mode
  • xstring = xstring(bloblength)
  • after the OPEN DATASET IN BINARY MODE, do only one TRANSFER xstring, followed by CLOSE DATASET

0 Kudos
1,012

Thanks for your answer, but I'm not able to reach to the solution.

1,012

I understand that "it doesn't work". What did you try, what did you get, what was incorrect?

1,012

I agree with Sandra that you might be complicating this. Try separating the FTP piece from the rest and it'd be much easier to see what the problem is exactly. Please also post more details.

0 Kudos
1,012

I think I did what you told me (code below), I can see the file in AL11 but when I open I get a wrong file error.

Thanks,

Maria

Sandra_Rossi
Active Contributor
0 Kudos
1,012

How do you know that "it's wrong created" ? Maybe it's correctly created, but you incorrectly read/use it...

maria_merino
Active Participant
1,012

I tried like this (in a new program just to do some tests..) but it doesn't work

TYPES: BEGIN OF text,
line(400) TYPE c,
END OF text.
TYPES: BEGIN OF ty_xml,
raw(4000) TYPE c,
END OF ty_xml.

DATA: user(30) TYPE c ,
pwd(30) TYPE c ,
host(64) TYPE c ,
slen TYPE i,
key TYPE i VALUE 26101957,
hdl TYPE i,
dest TYPE rfcdes-rfcdest,
g_str TYPE string,
g_xmldata TYPE xstring,
l_lin(2048),
extension(100).
DATA: bindata TYPE TABLE OF blob WITH HEADER LINE,
result TYPE TABLE OF text WITH HEADER LINE,
chardata TYPE TABLE OF text WITH HEADER LINE,
g_t_xml_tab TYPE TABLE OF ty_xml INITIAL SIZE 0,
i_fich TYPE TABLE OF epsfili WITH HEADER LINE,
blob_length TYPE i,
l_xstring TYPE xstring,
fichero_out LIKE rlgrap-filename,"(255),
carpeta LIKE user_dir-dirname.

START-OF-SELECTION.

pwd = xxxxxxxxxxx.
user = xxxxxxxxxxxxxxx.
host = xxxxxx.

slen = strlen( pwd ) .
* SAP Application Server -> FTP Server
dest = 'SAPFTPA'.

* La contraseña hay que formatearla
CALL FUNCTION 'HTTP_SCRAMBLE'
EXPORTING
source = pwd
sourcelen = slen
key = key
IMPORTING
destination = pwd.

CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
text = 'Connect to FTP Server'.

* Conectamos al directorio FTP
CALL FUNCTION 'FTP_CONNECT'
EXPORTING
user = user
password = pwd
host = host
rfc_destination = dest
IMPORTING
handle = hdl
EXCEPTIONS
not_connected = 1
OTHERS = 2.

IF sy-subrc = 0.

* Ponemos modo pasivo
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = hdl
command = 'set passive off'
TABLES
data = result
EXCEPTIONS
OTHERS = 1.
CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'
EXPORTING
text = 'Create file on FTP Server'.

CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = hdl
command = 'ascii'
TABLES
data = result
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3.

* Nos movemos a la carpeta DOWNLOAD
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = hdl
command = 'cd DOWNLOAD'
TABLES
data = result
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3.

CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = hdl
command = 'dir'
TABLES
data = result
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3.

LOOP AT result.
IF result CS 'pdf' .
i_fich-name = result+62(50).
APPEND i_fich.
ENDIF.
ENDLOOP.
LOOP AT i_fich.
* Se descarga el archivo del servidor
CALL FUNCTION 'FTP_SERVER_TO_R3'
EXPORTING
handle = hdl
fname = i_fich-name
* character_mode = 'X'
character_mode = abap_false
IMPORTING
blob_length = blob_length
TABLES
blob = bindata
text = g_t_xml_tab
EXCEPTIONS
tcpip_error = 1
command_error = 2
data_error = 3
OTHERS = 4.
LOOP AT bindata.
CONCATENATE bindata-content l_xstring
INTO l_xstring IN BYTE MODE.
ENDLOOP.
SELECT SINGLE dirname INTO carpeta FROM user_dir
WHERE aliass = 'EFACTURASPROV'.
CONCATENATE
carpeta '\' 'XXXX' '.pdf' INTO fichero_out.
OPEN DATASET fichero_out FOR OUTPUT IN BINARY MODE.
TRANSFER l_xstring TO fichero_out.

CLOSE DATASET fichero_out.

ENDLOOP.

ENDIF.

1,012

How do you know that "it's wrong created" ? Maybe it's correctly created, but you incorrectly read/use it...

"it doesn't work" should be banned from language, if you don't explain what you tried, what you got, what is incorrect versus what you expect.

0 Kudos
1,012

Sandra, I think I have explained what I tried (code attached), and "it doesn't work" is perfectly used in this case.

Once the pdf is in R3 it should be displayed from another transaction created for this purpose.

If I upload the file using CG3Z it works ok.

If I do it from my program connecting to FTP I think I'm missing something in my code because I get an error saying the file is damaged or not admitted.

So, I really appreciate your help as long as it is well-intentioned, I only use SCN to ask for some help when I've done my best to solve something and I can't reach to a solution, and that's what I'm doing. Any help will be appreciated, we are not in this community to criticize.

0 Kudos
1,012

Why are you (still) using ASCII FTP command?

And you also forgot to "cut" xstring to the correct length (blob_length) of file as Sandra already told you.

After these two problems I think it might be OK and file readable.

-- Tomas --

0 Kudos
1,012

Thank you for telling about CG3Z and your custom program to display the file, this is very helpful as it answers my question about how you know whether the issue is about writing the file versus reading the file.

Thank you for telling about the file is "damaged or not admitted", because one of the well-known reasons is that the file contains erroneous extra null bytes, and I see that you don't take into account the size of the file transmitted (parameter BLOB_LENGTH). You may use:

TRANSFER l_xstring TO fichero_out LENGTH blob_length.

So, the issue is not about FTP, but how you create the file on the ABAP server.

0 Kudos
1,012

Tomas Buryanek

Even after both steps, it doesn't work...do you have any other solution?

Would be helpful if you can share

0 Kudos
1,012

Hi Maria Merino

I am facing the same issue as you, followed the procedure provided by Sandra Rossi still I get the below error 😞

Did you solve the issue?

Code Snippet:

OPEN DATASET filnm FOR OUTPUT IN BINARY MODE.
TRANSFER l_xstring TO filnm LENGTH blob_length.
CLOSE DATASET filnm.

Error message:

1,012

Hey guys,

Solved it myself.

By adding casting before Xstring byte mode conversion.

Thanks for the above posts, quite helpful.

Cheers,

Kishore