大概步骤分为以下几步:
- 首先将打印的凭证以pdf格式存到sap文件服务器AL11
- 连接FTP
- 编写FTP命令从AL11中去下载对饮文件
- 关闭连接
- gt_otf为打印产生的结果表
- 以二进制的形式存到AL11
- filename1为AL11路径和文件名
CALL FUNCTION 'CONVERT_OTF'
EXPORTING
format = 'PDF'
max_linewidth = 132
IMPORTING
bin_filesize = w_bin_filesize
TABLES
otf = gt_otf
lines = 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
* WITHsy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
i_pdf[] = tline[].
ENDIF.
CONCATENATE '/usr/sap/trans/' filename INTO filename1.
OPEN DATASET filename1 FOR OUTPUT IN BINARY MODE.
IF sy-subrc = 0.
LOOP AT i_pdf.
TRANSFER i_pdf TO filename1.
ENDLOOP.
ENDIF.
CLOSE DATASET filename1.
* get ftp command
CLEAR t_ftpcomm[].
t_ftpcomm-ftpcomm = 'prompt'.
APPEND t_ftpcomm. CLEAR t_ftpcomm.
t_ftpcomm-ftpcomm = 'bi'.
APPEND t_ftpcomm. CLEAR t_ftpcomm.
* 进入指定文件夹/XX/YYYY/MM/YYYYMMDD/ ,如果没有文件夹则创建
PERFORM frm_goto_folder USING uv_fold CHANGING t_ftpcomm[].
CONCATENATE 'lcd' '/usr/sap/trans/' INTO t_ftpcomm-ftpcomm
SEPARATED BY space.
MOVE t_ftpcomm-ftpcomm TO f_str.
SHIFT f_str RIGHT DELETING TRAILING '/'.
CONDENSE f_str.
MOVE f_str TO t_ftpcomm-ftpcomm.
APPEND t_ftpcomm. CLEAR t_ftpcomm.
CONCATENATE '"' filename '"' INTO filename.
CONCATENATE 'dele' filename INTO t_ftpcomm-ftpcomm SEPARATED BY space. APPEND t_ftpcomm. CLEAR t_ftpcomm.
CONCATENATE 'put' filename INTO t_ftpcomm-ftpcomm SEPARATED BY space. APPEND t_ftpcomm. CLEAR t_ftpcomm.
* end ftp command
* ftp connect
SET EXTENDED CHECK OFF.
slen = strlen( f_pwd ).
CALL FUNCTION 'HTTP_SCRAMBLE'
EXPORTING
source = f_pwd
sourcelen = slen
key = key
IMPORTING
destination = f_pwd1.
CALL FUNCTION 'FTP_CONNECT'
EXPORTING
user = f_user
password = f_pwd1
host = f_host
rfc_destination = f_dest
IMPORTING
handle = hdl.
IF sy-subrc NE 0.
RAISE connect_ftp_error.
ENDIF.
* exec ftp command
CLEAR ftp_result1[].
LOOP AT t_ftpcomm.
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = hdl
command = t_ftpcomm-ftpcomm
compress = compress
TABLES
data = ftp_result1
EXCEPTIONS
command_error = 1
tcpip_error = 2.
ENDLOOP.
CALL FUNCTION 'FTP_DISCONNECT'
EXPORTING
handle = hdl.
CALL FUNCTION 'RFC_CONNECTION_CLOSE'
EXPORTING
destination = 'SAPFTPA'
EXCEPTIONS
OTHERS = 1.
* unix command
REFRESH result.
DELETE DATASET filename1.
FTP创建文件夹
**end add by caijh on 20211214
*&---------------------------------------------------------------------*
*& Form FRM_GOTO_FOLDER
*&---------------------------------------------------------------------*
* 进入夹/XX/YYYY/MM/YYYYMMDD/ ,如果没有文件夹则创建,年月日格式。
*----------------------------------------------------------------------*
FORM frm_goto_folder USING uv_foldername
CHANGING t_ftpcomm TYPE ty_tab_cmd.
DATA lwa_cmd TYPE ty_cmd.
DATA lv_tmp_str TYPE string.
DATA lv_filenum TYPE string.
DATA lv_filesecond TYPE string.
IF uv_foldername <> 'FIVOUCHER'.
SPLIT uv_foldername AT '&' INTO uv_foldername lv_filesecond lv_filenum.
ELSE.
SPLIT uv_foldername AT '&' INTO uv_foldername lv_filenum.
ENDIF.
CONCATENATE 'CWD /' `` INTO lwa_cmd-ftpcomm SEPARATED BY space. APPEND lwa_cmd TO t_ftpcomm.
CONCATENATE 'mkdir ' uv_foldername INTO lwa_cmd-ftpcomm SEPARATED BY space. APPEND lwa_cmd TO t_ftpcomm.
CONCATENATE 'TYPE A' `` INTO lwa_cmd-ftpcomm SEPARATED BY space. APPEND lwa_cmd TO t_ftpcomm.
CONCATENATE 'cd' uv_foldername INTO lwa_cmd-ftpcomm SEPARATED BY space.
APPEND lwa_cmd TO t_ftpcomm.
IF uv_foldername <> 'FIVOUCHER' .
CONCATENATE 'mkdir ' lv_filesecond `/` INTO lwa_cmd-ftpcomm SEPARATED BY space. APPEND lwa_cmd TO t_ftpcomm.
CONCATENATE `/` uv_foldername `/` lv_filesecond `/` INTO lv_tmp_str.
CONCATENATE 'cd' lv_tmp_str INTO lwa_cmd-ftpcomm SEPARATED BY space. APPEND lwa_cmd TO t_ftpcomm.
CONCATENATE uv_foldername '/' lv_filesecond INTO uv_foldername.
ENDIF.
CONCATENATE 'mkdir ' sy-datum+0(4) `/` INTO lwa_cmd-ftpcomm SEPARATED BY space. APPEND lwa_cmd TO t_ftpcomm.
CONCATENATE `/` uv_foldername `/` sy-datum+0(4) `/` INTO lv_tmp_str.
CONCATENATE 'cd' lv_tmp_str INTO lwa_cmd-ftpcomm SEPARATED BY space. APPEND lwa_cmd TO t_ftpcomm.
CONCATENATE 'mkdir ' sy-datum+4(2) INTO lwa_cmd-ftpcomm SEPARATED BY space. APPEND lwa_cmd TO t_ftpcomm.
CONCATENATE `/` uv_foldername `/` sy-datum+0(4) `/` sy-datum+4(2) INTO lv_tmp_str.
CONCATENATE 'cd' lv_tmp_str INTO lwa_cmd-ftpcomm SEPARATED BY space. APPEND lwa_cmd TO t_ftpcomm.
* IF uv_foldername = 'FIVOUCHER' OR uv_foldername = 'CHECKVOUCHER'.
CONCATENATE 'mkdir ' sy-datum+6(2) INTO lwa_cmd-ftpcomm SEPARATED BY space. APPEND lwa_cmd TO t_ftpcomm.
CONCATENATE `/` uv_foldername `/` sy-datum+0(4) `/` sy-datum+4(2) `/` sy-datum+6(2) INTO lv_tmp_str.
CONCATENATE 'cd' lv_tmp_str INTO lwa_cmd-ftpcomm SEPARATED BY space. APPEND lwa_cmd TO t_ftpcomm.
* ENDIF.
CONCATENATE 'mkdir ' lv_filenum INTO lwa_cmd-ftpcomm SEPARATED BY space. APPEND lwa_cmd TO t_ftpcomm.
* IF uv_foldername = 'FIVOUCHER' OR uv_foldername = 'CHECKVOUCHER'. .
CONCATENATE `/` uv_foldername `/` sy-datum+0(4) `/` sy-datum+4(2) `/` sy-datum+6(2) `/` lv_filenum INTO lv_tmp_str.
* ELSE.
* CONCATENATE `/` uv_foldername `/` sy-datum+0(4) `/` sy-datum+4(2) `/` lv_filenum INTO lv_tmp_str.
* ENDIF.
CONCATENATE 'cd' lv_tmp_str INTO lwa_cmd-ftpcomm SEPARATED BY space. APPEND lwa_cmd TO t_ftpcomm.
ENDFORM.