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: 

Saving a generated PDF to FTP

Former Member
0 Kudos
2,751

What I'm trying to do is save the PDF output of a Smartform to an FTP server.

So far I've been able to generate a file on the FTP site from my ABAP, but it can't be read as a PDF. So apparently I'm losing information during the transfer.

I've tried both passing the OTF data from the Smartform FM and passing the PDF table from the CONVERT_OTF FM. The file size changes, but it still can't be read.

A part I'm not entirely sure of is the blob_length. In example program x, it's set to the line length of the blob. I've tried the same with the OTF data table and the PDF table... but to no end.

This is the code I'm using:

* Scramble password
  slen = STRLEN( lv_pwd ).

  CALL FUNCTION 'HTTP_SCRAMBLE'
    EXPORTING
      SOURCE      = lv_pwd
      sourcelen   = slen
      key         = key
    IMPORTING
      destination = lv_pwd.

* Connect to FTP server
  CALL FUNCTION 'FTP_CONNECT'
    EXPORTING
      user            = lv_user
      password        = lv_pwd
      host            = lv_host
      rfc_destination = lv_dest
    IMPORTING
      handle          = hdl
    EXCEPTIONS
      not_connected   = 1
      OTHERS          = 2.

  IF sy-subrc EQ 0.

*   Set the remote directory
    CALL FUNCTION 'FTP_COMMAND'
      EXPORTING
        handle        = hdl
        command       = lv_cmd
      TABLES
        data          = result
      EXCEPTIONS
        tcpip_error   = 1
        command_error = 2
        data_error    = 3.

    IF sy-subrc EQ 0.

      LOOP AT tb_pdf INTO ls_pdf.
        ls_blob = ls_pdf.
        APPEND ls_blob TO lt_blob.
      ENDLOOP.

      DESCRIBE TABLE lt_blob LINES lv_lines.
      blob_length = lv_lines * 134.

*     Upload file (existing files will be overwritten)
      CALL FUNCTION 'FTP_R3_TO_SERVER'
        EXPORTING
          handle        = hdl
          fname         = lv_filename
          blob_length   = blob_length
        TABLES
          blob          = lt_blob
        EXCEPTIONS
          tcpip_error   = 1
          command_error = 2
          data_error    = 3
          OTHERS        = 4.

    ENDIF.

*   Close connection
    CALL FUNCTION 'FTP_DISCONNECT'
      EXPORTING
        handle = hdl.

    CALL FUNCTION 'RFC_CONNECTION_CLOSE'
      EXPORTING
        destination = lv_dest
      EXCEPTIONS
        OTHERS      = 1.

  ENDIF.

1 REPLY 1

Former Member
0 Kudos
491

hi,

First set control table's parameters for calling smartform

   st_cntrl-getotf = 'X'.

   st_cntrl-no_dialog = 'X'.

   st_cntrl-preview = ''.


CALL FUNCTION fm_name       "'/1BCDWB/SF00000221'

         EXPORTING

*       ARCHIVE_INDEX              = ARCHIVE_INDEX

*       ARCHIVE_INDEX_TAB          = ARCHIVE_INDEX_TAB

*       ARCHIVE_PARAMETERS         = ARCHIVE_PARAMETERS

      control_parameters         st_cntrl

.

.

.

.

.

IMPORTING

*       DOCUMENT_OUTPUT_INFO       = DOCUMENT_OUTPUT_INFO

        job_output_info            = st_prnout_info

EXCEPTIONS

          formatting_error           = 1

          internal_error             = 2

          send_error                 = 3

          user_canceled              = 4

                 .

otfdata[] = st_prnout_info-otfdata.

  after getting smartform-otfdata table

DATA:    v_len_in LIKE sood-objlen,

               v_len_out LIKE sood-objlen,

               i_tline TYPE TABLE OF tline WITH HEADER LINE,

               filename TYPE string.

       DATA:   l_length TYPE i,"Password length

       l_password TYPE c LENGTH 30,

       l_key TYPE i VALUE 26101957,

       l_ftp_handle TYPE i,"FTP handle

       l_cmd TYPE c LENGTH 200,"FTP command

       l_error TYPE c,"Error flag

       p_ftp_handle TYPE i,

       p_cmnd TYPE c,

       p_boblen TYPE i.

       TYPES: BEGIN OF ty_result,

                 line(150) TYPE c,

             END OF ty_result.

* Table for FTP command results

       DATA: it_result TYPE STANDARD TABLE OF ty_result,

             wa_result TYPE ty_result.

      TYPES: BEGIN OF blob,

               line(80) TYPE x,

               END OF blob.

       DATA :    bindata TYPE TABLE OF blob WITH HEADER LINE.

       DATA:  l_user(30) TYPE c VALUE '     ', "user name of ftp server * to be fill

                l_pwd(30) TYPE c VALUE '     ', "password of ftp server * to be fill

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

                l_dest LIKE rfcdes-rfcdest VALUE 'SAPFTPA'."Background RFC destination*


       DATA: wrk_file TYPE char200.

       DATA: w_hdl TYPE i,

             c_key TYPE i VALUE 26101957, *may change in your case

             l_slen TYPE i.

    

CONCATENATE  '/' filename   '.pdf' INTO wrk_file.*file name to be fill


CALL FUNCTION 'CONVERT_OTF'

        EXPORTING

          format                      = 'PDF'

          max_linewidth               = 132

*   ARCHIVE_INDEX               = ' '

*   COPYNUMBER                  = 0

*   ASCII_BIDI_VIS2LOG          = ' '

*   PDF_DELETE_OTFTAB           = ' '

*   PDF_USERNAME                = ' '

        IMPORTING

          bin_filesize                = v_len_in

          bin_file                    = bin_file

         TABLES

           otf                         = otfdata

           lines                       = i_tline

        EXCEPTIONS

          err_max_linewidth           = 1

          err_format                  = 2

          err_conv_not_possible       = 3

          err_bad_otf                 = 4

          OTHERS                      = 5

                 .

       IF sy-subrc <> 0.

         MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

                 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

       ENDIF.



       p_boblen = v_len_in.

       CALL FUNCTION 'SCMS_XSTRING_TO_BINARY'

         EXPORTING

           buffer                = bin_file

*   APPEND_TO_TABLE       = ' '

* IMPORTING

*   OUTPUT_LENGTH         =

         TABLES

           binary_tab            = bindata

                 .

       SET EXTENDED CHECK OFF.

       l_slen = STRLEN( l_pwd ).

       CALL FUNCTION 'HTTP_SCRAMBLE'

         EXPORTING

           SOURCE      = l_pwd

           sourcelen   = l_slen

           key         = c_key

         IMPORTING

           destination = l_pwd.

* To Connect to the Server using FTP

       CALL FUNCTION 'FTP_CONNECT'

         EXPORTING

           user                   = l_user

           password               = l_pwd

*         ACCOUNT                =

           host                   = l_host

           rfc_destination        = l_dest

*         GATEWAY_USER           =

*         GATEWAY_PASSWORD       =

*         GATEWAY_HOST           =

        IMPORTING

          handle                 w_hdl

        EXCEPTIONS

          not_connected          = 1

          OTHERS                 = 2

                 .

       IF sy-subrc <> 0.

         MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

                 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

       ENDIF.

*FTP_R3_TO_SERVER:used to transfer the internal table data as a file to other system in the character mode.

       CALL FUNCTION 'FTP_R3_TO_SERVER'

         EXPORTING

           handle               = w_hdl

           fname                = wrk_file          "file path of destination system

          blob_length          = p_boblen

*         CHARACTER_MODE       =

        TABLES

          blob                 =     bindata "otf_data

*         TEXT                 =

        EXCEPTIONS

          tcpip_error          = 1

          command_error        = 2

          data_error           = 3

          OTHERS               = 4

                 .

       IF sy-subrc <> 0.

         MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

                 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

       ENDIF.

*FTP_DISCONNECT: This is used to disconnect the connection between SAP and other system.

* To disconnect the FTP

       CALL FUNCTION 'FTP_DISCONNECT'

         EXPORTING

           handle = w_hdl.

*RFC_CONNECTION_CLOSE:This is used to disconnect the RFC connection between SAP and other system.

       CALL FUNCTION 'RFC_CONNECTION_CLOSE'

        EXPORTING

          destination                = l_dest

*   TASKNAME                   =

        EXCEPTIONS

          destination_not_open       = 1

          OTHERS                     = 2

                 .

       IF sy-subrc <> 0.

         MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno

                 WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

       ENDIF.

.

Please revert back if any issue.