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: 

Downloading PDF file using FTP

Former Member
0 Kudos

Hi

Just Wondering is it possible to download a PDF file ( which will be created using a spool request) using ftp , if so please help me in this any sample doc or example.

Any configration required to be done for FTP......as not done it before .

thanks in advance

12 REPLIES 12

Former Member
0 Kudos

Pawan try this program, you will be able to download the spool in PDF format to your local m/c.

RSTXPDFT4

Former Member
0 Kudos

Hi,

Check the below sample code:

Save Report Output to a PDF File

This report takes another report as input, and captures the output of that report. The output is then converted to PDF and saved to a local file. This shows how to use some of the PDF function modules, as well as an easy way to create PDF files.

Source Code Listing

report zabap_2_pdf.

*-- Enhancements: only allow to be run with variant. Then called

*-- program will be transparent to users

*-- TABLES

tables:

tsp01.

*-- STRUCTURES

data:

mstr_print_parms like pri_params,

mc_valid(1) type c,

mi_bytecount type i,

mi_length type i,

mi_rqident like tsp01-rqident.

*-- INTERNAL TABLES

data:

mtab_pdf like tline occurs 0 with header line,

mc_filename like rlgrap-filename.

*-- SELECTION SCREEN

parameters:

p_repid like sy-repid, " Report to execute

p_linsz like sy-linsz default 132, " Line size

p_paart like sy-paart default 'X_65_132'. " Paper Format

start-of-selection.

concatenate 'c:\'

p_repid

'.pdf'

into mc_filename.

*-- Setup the Print Parmaters

call function 'GET_PRINT_PARAMETERS'

exporting

authority= space

copies = '1'

cover_page = space

data_set = space

department = space

destination = space

expiration = '1'

immediately = space

in_archive_parameters = space

in_parameters = space

layout = space

mode = space

new_list_id = 'X'

no_dialog= 'X'

user = sy-uname

importing

out_parameters = mstr_print_parms

valid = mc_valid

exceptions

archive_info_not_found = 1

invalid_print_params = 2

invalid_archive_params = 3

others = 4.

*-- Make sure that a printer destination has been set up

*-- If this is not done the PDF function module ABENDS

if mstr_print_parms-pdest = space.

mstr_print_parms-pdest = 'LOCL'.

endif.

*-- Explicitly set line width, and output format so that

*-- the PDF conversion comes out OK

mstr_print_parms-linsz = p_linsz.

mstr_print_parms-paart = p_paart.

submit (p_repid) to sap-spool without spool dynpro

spool parameters mstr_print_parms

via selection-screen

and return.

*-- Find out what the spool number is that was just created

perform get_spool_number using sy-repid

sy-uname

changing mi_rqident.

*-- Convert Spool to PDF

call function 'CONVERT_ABAPSPOOLJOB_2_PDF'

exporting

src_spoolid= mi_rqident

no_dialog = space

dst_device = mstr_print_parms-pdest

importing

pdf_bytecount = mi_bytecount

tables

pdf = mtab_pdf

exceptions

err_no_abap_spooljob = 1

err_no_spooljob = 2

err_no_permission = 3

err_conv_not_possible = 4

err_bad_destdevice = 5

user_cancelled = 6

err_spoolerror = 7

err_temseerror = 8

err_btcjob_open_failed = 9

err_btcjob_submit_failed = 10

err_btcjob_close_failed = 11

others = 12.

call function 'DOWNLOAD'

exporting

bin_filesize = mi_bytecount

filename = mc_filename

filetype = 'BIN'

importing

act_filename = mc_filename

tables

data_tab = mtab_pdf.

----


  • FORM get_spool_number *

----


  • Get the most recent spool created by user/report *

----


  • --> F_REPID *

  • --> F_UNAME *

  • --> F_RQIDENT *

----


form get_spool_number using f_repid

f_uname

changing f_rqident.

data:

lc_rq2name like tsp01-rq2name.

concatenate f_repid+0(8)

f_uname+0(3)

into lc_rq2name separated by '_'.

select * from tsp01 where rq2name = lc_rq2name

order by rqcretime descending.

f_rqident = tsp01-rqident.

exit.

endselect.

if sy-subrc ne 0.

clear f_rqident.

endif.

endform." get_spool_number

Also refer OSS Note 383718 and have a look at standard program RSTXPDFT5.

Hope this helps you.

Regards,

Anjali

0 Kudos

Hi Anjali ,

Thanks for the help , but my requirement is after downlaoding the PDF file i want to send the data via ftp to the print/ mail vendor so can u also help me in this .....

thanks once again for the help.

regards,

Pawan

0 Kudos

Hi,

Im not very sure abt sending it via ftp.

But chek these links if they provide with u any information reg this:

https://www.sdn.sap.com/sdn/search.sdn?contenttype=url&content=https%3A//forums.sdn.sap.com/search%2...

See also

https://www.sdn.sap.com/sdn/collaboration.sdn?node=linkFnode6-1&contenttype=url&content=https://

There is also this excellent weblog by Thomas Jung.

/people/thomas.jung3/blog/2004/09/08/sending-e-mail-from-abap--version-610-and-higher--bcs-interface

Regards,

Anjali

0 Kudos

Hi Anjali,

One more thing i want to run the whole process in Background, so from getting spool number to creating it into pdf format in background and making one directory and sending this pdf file to that directory ..........

Can u help me in this.

Thanks in Advance.

Regards,

Pawan.

0 Kudos

Hi,

You can use the FM:

CONVERT_ABAPSPOOLJOB_2_PDF to run the process in Background.

Please go through this thread...

Hope this helps you.

Regards,

Anjali

VijayasekarK
Active Participant
0 Kudos

Hi Pawan ,

I hope u might know how to convert the spool to pdf. You can do FTP from SAP itself . Check the function modules available under the function group SFTP.

FTP_CLIENT_TO_R3

FTP_COMMAND

FTP_COMMAND_LIST

FTP_CONNECT

FTP_COPY

FTP_DISCONNECT

FTP_R3_TO_CLIENT

FTP_R3_TO_SERVER

FTP_SERVER_TO_R3

FTP_START_REG_SERVER

FTP_START_SAPFTP

FTP_STOP_REG_SERVER

FTP_VERSION

  • First store this pdf file in application file and thru FTP you can move where ever u want.

Hope the above help.....

0 Kudos

Hi Pawan:

Use this code to FTP your PDF File.

l_filespec - Local file path

r_filespec - FTP Server file path

In your code, use the RFC Destination 'sapftp' and it will be generated automatically when it establishes the first connection.Also make sure FTP Service is running in your FTP Server and you use the right user id / password / domain for logon and you have read/write access to the folder from which the file needs to be fetched.

*********************************************************

FUNCTION Z_EXEC_SIMPLE_FTP.

*"----


""Local interface:

*" IMPORTING

*" VALUE(I_REMOTE_HOST) TYPE C

*" VALUE(I_USERID) TYPE C DEFAULT SPACE

*" VALUE(I_PASSWORD) TYPE C DEFAULT SPACE

*" VALUE(I_LOCAL_PATH) TYPE C DEFAULT SPACE

*" VALUE(I_LOCAL_FILE) TYPE C DEFAULT SPACE

*" VALUE(I_REMOTE_PATH) TYPE C DEFAULT SPACE

*" VALUE(I_REMOTE_FILE) TYPE C DEFAULT SPACE

*" VALUE(I_PUT_TO_REMOTE_HOST) TYPE C DEFAULT SPACE

*" VALUE(I_DESTINATION) LIKE RFCDES-RFCDEST DEFAULT 'SAPFTP'

*" TABLES

*" DATA

*" EXCEPTIONS

*" NOT_CONNECTED

*" TCPIP_ERROR

*" COMMAND_ERROR

*" DATA_ERROR

*"----


DATA: MC_PASSWORD(20) TYPE C,

MI_KEY TYPE I VALUE 26101957,

MI_PWD_LEN TYPE I.

MC_PASSWORD = I_PASSWORD.

DESCRIBE FIELD MC_PASSWORD LENGTH MI_PWD_LEN.

*-- FTP_CONNECT requires an encrypted password to work

CALL 'AB_RFC_X_SCRAMBLE_STRING'

ID 'SOURCE' FIELD MC_PASSWORD ID 'KEY' FIELD MI_KEY

ID 'SCR' FIELD 'X' ID 'DESTINATION' FIELD MC_PASSWORD

ID 'DSTLEN' FIELD MI_PWD_LEN.

*Set FTP File Paths

CONCATENATE i_local_path i_local_file INTO l_filespec.

CONCATENATE i_remote_path i_remote_file INTO r_filespec.

----


*FTP Connect

----


CALL FUNCTION 'FTP_CONNECT'

EXPORTING

USER = I_USERID

PASSWORD = MC_PASSWORD

ACCOUNT = '<Your Domain Name>'

HOST = I_REMOTE_HOST

RFC_DESTINATION = I_DESTINATION

IMPORTING

HANDLE = FTP_HANDLE

EXCEPTIONS

NOT_CONNECTED = 1

OTHERS = 2.

IF SY-SUBRC <> 0.

MESSAGE ID SY-MSGID TYPE 'S' NUMBER SY-MSGNO WITH

SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4

RAISING NOT_CONNECTED.

ENDIF.

----


----


*FTP Command

----


  • Get without semaphore...

CONCATENATE 'put' l_filespec r_filespec

INTO l_command SEPARATED BY space.

CALL FUNCTION 'FTP_COMMAND'

EXPORTING

handle = ftp_handle

command = l_command

TABLES

data = T_FTP_RESPONSE

EXCEPTIONS

tcpip_error = 1

command_error = 2

data_error = 3

OTHERS = 4.

----


*FTP Close

----


  • Close Connection

CALL FUNCTION 'FTP_DISCONNECT'

EXPORTING

handle = ftp_handle

EXCEPTIONS

OTHERS = 1.

ENDFUNCTION.

Former Member
0 Kudos

Hi ,

I have done a similar project where i convert spool number to PDF and then FTP the file to the Client Web Server .If this is what you needed please let me know i will give you the code.

Thank you

0 Kudos

Dear Aussie,

Will you please share the code where the spool number is converted to PDF and then FTP the file to the client web server.

Thanks,

Jaya.

0 Kudos

Hello Jaya

The answer is like this:

Step 1- First convert the spool to PDF using the report as suggested above

select the spool from the nast table and then do a

SUBMIT into the report RSTXPDFT4

Step2- The path you specify will be used to do the FTP call to transfer the file!!

So you need to use the Ftp_connect and ftp_command to do this

make sure the path is the same as you described in the submit


0 Kudos

Hi Jaya ,

Please find the code below . It converts the spool to PDF and then uses FTP to transfer the file .For this you need a Username and Password to connect to the server where you want the PDF to reside .

START-OF-SELECTION.

   IF ftp EQ 'X'.

*subroutine for ftp transfer

     PERFORM : spool_pdf_conversion.

*              ftp_transfer .

   ELSE.

*subroutine for local transfer

     PERFORM : spool_pdf_conversion,

               local_transfer .

   ENDIF.

*&---------------------------------------------------------------------*

*&      Form  spool_pdf_conversion

*&---------------------------------------------------------------------*

*       text

*----------------------------------------------------------------------*

FORM spool_pdf_conversion.

***convert spool to pdf

  CALL FUNCTION 'CONVERT_OTFSPOOLJOB_2_PDF'

    EXPORTING

      src_spoolid              = spoolno

      no_dialog                = 'X'

      pdf_destination          = 'X'

      no_background            = 'X'

    IMPORTING

      pdf_bytecount            = bin_size

      bin_file                 = pdf_xstring

    EXCEPTIONS

      err_no_otf_spooljob      = 1

      err_no_spooljob          = 2

      err_no_permission        = 3

      err_conv_not_possible    = 4

      err_bad_dstdevice        = 5

      user_cancelled           = 6

      err_spoolerror           = 7

      err_temseerror           = 8

      err_btcjob_open_failed   = 9

      err_btcjob_submit_failed = 10

      err_btcjob_close_failed  = 11

      OTHERS                   = 12.

  IF sy-subrc <> 0.

    MESSAGE e712(po) WITH sy-subrc 'CONVERT_OTFSPOOLJOB_2_PDF'.

  ENDIF.

***converting xstring to the binary format

  CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'

    EXPORTING

      buffer                = pdf_xstring

*       APPEND_TO_TABLE       = ' '

*     IMPORTING

*       OUTPUT_LENGTH         =

    TABLES

      binary_tab            = bindata

            .

ENDFORM.                    "spool_pdf_conversion

*&---------------------------------------------------------------------*

*&      Form  Desktop_transfer

*&---------------------------------------------------------------------*

*       text

*----------------------------------------------------------------------*

FORM LOCAL_transfer.

  CALL FUNCTION 'FTP_R3_TO_CLIENT'

EXPORTING

   fname                 = 'C:\Documents and Settings\GSS\Desktop\Script_d.PDF'

   rfc_destination       = 'SAPFTP'

  blob_length           =  bin_size

*     CHARACTER_MODE        = 'X'

TABLES

   blob                = bindata

*    TEXT                  =  datatab

EXCEPTIONS

  command_error         = 1

  data_error            = 2

  OTHERS                = 3.

ENDFORM.                    "Desktop_transfer

*&---------------------------------------------------------------------*

*&      Form  ftp_trasfer

*&---------------------------------------------------------------------*

*       text

*----------------------------------------------------------------------*

FORM ftp_transfer.

***ftp connection logic

  slen = STRLEN( l_pwd ).

  CALL FUNCTION 'HTTP_SCRAMBLE'

    EXPORTING

      SOURCE      = l_pwd

      sourcelen   = slen

      key         = key

    IMPORTING

      destination = l_pwd.

  CALL FUNCTION 'SAPGUI_PROGRESS_INDICATOR'

    EXPORTING

      text = 'Connect to FTP Server'.

  CALL FUNCTION 'FTP_CONNECT'

    EXPORTING

      user            = l_user

      password        = l_pwd

      host            = l_host

      rfc_destination = 'SAPFTPA'

    IMPORTING

      handle          = hdl.

  IF NOT pasv IS INITIAL.

    REFRESH result.

    CALL FUNCTION 'FTP_COMMAND'

      EXPORTING

        handle        = hdl

        command       = 'set passive on'

      TABLES

        data          = result

      EXCEPTIONS

        tcpip_error   = 1

        command_error = 2

        data_error    = 3.

    IF sy-subrc EQ 0.

      WRITE: / 'Set passive mode'.

      SKIP 1.

    ENDIF.

  ENDIF.

***passing the file name

  docid = 'Script.pdf'.

***connecting to the ftp server

  CALL FUNCTION 'FTP_R3_TO_SERVER'

    EXPORTING

      handle        = hdl

      fname         = docid

      blob_length   = bin_size

    TABLES

      blob          = bindata

    EXCEPTIONS

      tcpip_error   = 1

      command_error = 2

      data_error    = 3

      OTHERS        = 4.

***ftp disconnection

  CALL FUNCTION 'FTP_DISCONNECT'

    EXPORTING

      handle = hdl.

  CALL FUNCTION 'RFC_CONNECTION_CLOSE'

    EXPORTING

      destination = dest

    EXCEPTIONS

      OTHERS      = 1.

ENDFORM.                    "

******* Declarations.

TYPES: BEGIN OF blob,

       line(80) TYPE x,

       END OF blob.

*** PDF conversion Declaration .

DATA: pdf_size      TYPE so_obj_len,

      pdf_content   TYPE solix_tab,

      pdf_xstring   TYPE xstring,

      v_xstring     TYPE xstring,

      v_text      TYPE localfile,

      rq       TYPE tsp01,

      bin_size TYPE i,

      dummy    TYPE TABLE OF rspoattr,

      otf LIKE itcoo OCCURS 100 WITH HEADER LINE,

      cancel,

      pdf LIKE tline OCCURS 100 WITH HEADER LINE,

      doctab LIKE docs OCCURS 1 WITH HEADER LINE,

      numbytes TYPE i,

      arc_idx LIKE toa_dara,

      pdfspoolid LIKE tsp01-rqident,

      jobname LIKE tbtcjob-jobname,

      jobcount LIKE tbtcjob-jobcount,

      is_otf,

      client LIKE tst01-dclient,

      name LIKE tst01-dname,

      objtype LIKE rststype-type,

      type LIKE rststype-type,

      get_size_from_format,

      bindata TYPE TABLE OF blob WITH HEADER LINE,

      result TYPE TABLE OF text WITH HEADER LINE,

      filesize TYPE i,

      convcount TYPE i,

      lines LIKE tline OCCURS 100 WITH HEADER LINE,

      textlines LIKE tline OCCURS 100 WITH HEADER LINE,

***FTP declarations

      v_file     TYPE char40,

      w_hdl      TYPE i,

      c_key      TYPE i VALUE 26101957,

      l_slen     TYPE i,

      l_user(30) TYPE c VALUE 'USERNAME',                    "user name of ftp server

      l_pwd(30)  TYPE c VALUE 'PASSWORD',              "password of ftp server

      l_host(64) TYPE c VALUE 'IP_ADDRESS',      "ip address of FTP server

      pasv,

      key TYPE i VALUE 26101957,

      hdl TYPE i,

      slen TYPE i,

      x TYPE i,

      docid LIKE sysuuid-c,

      cmd(120),

      error,

      bline(120) TYPE x,

      dest LIKE rfcdes-rfcdest.

***Selection Screen

PARAMETERS: spoolno LIKE tsp01-rqident.

SELECTION-SCREEN BEGIN OF LINE .

SELECTION-SCREEN POSITION 2.

PARAMETERS: ftp RADIOBUTTON GROUP rb.

SELECTION-SCREEN COMMENT 5(45) text-005.

SELECTION-SCREEN END OF LINE.

SELECTION-SCREEN BEGIN OF LINE .

SELECTION-SCREEN POSITION 2.

PARAMETERS: local RADIOBUTTON GROUP rb .

SELECTION-SCREEN COMMENT 5(45) text-006.

SELECTION-SCREEN END OF LINE.