Skip to Content

PDF to application server using FTP

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • avatar image
    Former Member
    Apr 03 at 02:28 AM

    Hey guys,

    Solved it myself.

    By adding casting before Xstring byte mode conversion.

    Thanks for the above posts, quite helpful.

    Cheers,

    Kishore

    Add comment
    10|10000 characters needed characters exceeded

  • Nov 28, 2017 at 06:09 PM

    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
    Add comment
    10|10000 characters needed characters exceeded

  • Dec 29, 2017 at 09:04 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Apr 02 at 12:05 PM

    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:

    Add comment
    10|10000 characters needed characters exceeded